VB API如何获取窗口内控件的句柄(急急急!)

是指定的窗口

用FindWindowEx
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Dim x
Private Sub Command1_Click()
AppActivate x
Dim NotepadHwnd As Long, hwnd As Long
NotepadHwnd = FindWindow("notepad", vbNullString)
hwnd = FindWindowEx(NotepadHwnd, 0, "Edit", vbNullString) '得到窗口类名为Edit的窗口句柄
a = PostMessage(hwnd, WM_KEYDOWN, &HBB, 0&)
' a = PostMessage(hwnd, WM_KEYUP, &HBB, 0&)
End Sub

Private Sub Form_Load()
x = Shell("notepad.exe", vbNormalFocus)
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-14
用这个 API 声明:
Private Declare Function GetDlgItem Lib "user32" Alias "GetDlgItem" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

然后,
Dim hWnd As Long
hWnd = GetDlgItem ( hDlg , nIDDlgItem )

hDlg是窗口的句柄,nIDDlgItem是hDlg窗口内的控件的ID号,返回值hWnd就是控件的句柄。
第2个回答  推荐于2018-12-04
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim n As POINTAPI
Dim a As Long
Private Sub Form_Load()
Timer1.Interval = 100
Label1.Caption = "移动鼠标指针"
End Sub

Private Sub Timer1_Timer()
GetCursorPos n
a = WindowFromPoint(n.x, n.y)
If a <> 0 Then
Dim s As String
s = String(100, Chr(0))
GetWindowText a, s, 100
Label1.Caption = "目标标题或文本: " & Trim(s)
Label2.Caption = "目标句柄为 " & a
End If
End Sub本回答被网友采纳

VB 如何得到窗体内控件的句柄
1、findwindow 找到指定标题的窗口 如果这个窗口标题你不知道,那么一个一个遍历吧,或者枚举所有进程(进程名字你总是知道的吧?)然后在遍历整个进程中所有的窗口。通过以上的方法,可以定位到窗口 2、GetWindow 到这个窗口里找控件 使用 GW_CHILD 常数,这样找到的就是这个窗体的子窗体(控件)的句柄了...

VB查找窗口句柄有哪些方法
我以前写辅助的时候弄过使用的API函数是findWindow首先声明一下API在你窗口最顶的位置上写上Private Declare Function 取窗口句柄 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long'由于VB支持中文的 我就定义成 取窗口句柄 这个了 '在窗体中...

怎样用VB抓取窗口的句柄?
找窗体的句柄得用到API了,最常用的是:FindWindow(一般只找父窗口句柄),FindWindowEx(可找子窗口的句柄),给个例子看看,只用到一个command控件,希望可以帮到你,不了解可以再咨询:Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As St...

vb怎么获取控件句柄和内容
先找到窗口的句柄,再来查找按钮的句柄,如果按钮有文本内容,那就好办,如果没有,那建议你利用SPY++先来察看一下按钮的类型,以此类型为查找参数多次调用FindWindowEx来查找,直到找到的的句柄和Spy++相同,那么这个按钮就找到了。按钮类名同样,但是你去遍历的时候它的次序始终是固定的。通过id不可靠,有...

VB获取窗口下,鼠标光标位置所在的控件句柄
API:GetForegroundWindow 可以获取到有焦点的顶层窗口。获取有焦点的子窗口只能用GetFocus,但是如果当前顶层窗口属于其他线程,GetFocus将返回NULL。没有好的办法解决,除非AttachThreadInput或者用钩子(HOOK)。

VB通过进程名获取窗口句柄?求例子
用API获取,GetWindowThreadProcessId ihwnd, pid 得到进程IDhProcess = OpenProcess(&H1F0FFF, False, pid) 获得进程句柄 例子:Option ExplicitPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, By...

vb 获取窗口句柄
'获得类名 ClassName = StripTerFlag(ab)If UCase(ClassName) = "NOTEPAD" Then List1.AddItem a List1.ItemData(List1.ListCount - 1) = s End If Loop End Sub Private Sub List1_Click()MsgBox List1.ItemData(List1.ListIndex) '单击列表框获得句柄 End Sub ...

VB获取窗体句柄后如何获取里面指定控件的句柄?
Private Sub Command1_Click() Dim x, y, z As Long x = FindWindow(vbNullString, "form1") Label1.Caption = x y = FindWindowEx(x, 0, "ThunderTextBox", "") Label2.Caption = y End Sub 上述代码中label1显示窗体的句柄,label2显示窗体中的一个文本框的句柄 ...

VB获取指定窗口的子程序句柄
0, 0&)End Sub 然后先运行 事先生成好的12345.exe 再运行后建的那个 你单击确定 就会使12345.exe的确定按钮被单击 返回它的hwnd 我的方法是先找到12345.exe的主窗口 然后根据主窗口的句柄获得其确定按钮的句柄 再使用sendmessage 发送模拟鼠标左键单击 VB控件的句柄 一般可用me.hwnd 方法获得 ...

vb如何获取到窗口的句柄
获取句柄不一定要用 FindWindow ,还可以枚举:Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As ...

相似回答