[VB]我只知道该窗口的句柄,该怎么获取窗体上的控件的句柄

2024-12-30 02:35:54
推荐回答(1个)
回答1:

下面一段代码, 你使用前把API函数阅览器打开, 看看有什么效果.

Dim hwnd As Long
Dim str1 As String, len1 As Long

'hwnd = FindWindowa("ThunderRT6FormDC", "Form1") '编译成EXE用这句,
hwnd = FindWindowa("ThunderFormDC", "Form1") '在调试的时候用这句.
str1 = Space(255) '定义接收字串.
GetWindowText hwnd, str1, 1024
Do While hwnd <> 0
hwnd = GetNextWindow(hwnd, 2) '只有2才表示找下一个窗口
len1 = GetWindowText(hwnd, str1, Len(str1))
If (InStr(1, str1, "API", 1) > 0) Then
Exit Do
End If
Loop

SetWindowText hwnd, "我把API阅读器的标题改变了"
hwnd = GetNextWindow(hwnd, 5) '5表示子窗口

Do While hwnd <> 0
hwnd = GetNextWindow(hwnd, 2) '只有2才表示找下一个窗口
GetWindowText hwnd, str1, Len(str1)
SetWindowText hwnd, "我改变了改钮2"
Loop

要在模块里定义:
Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Declare Function FindWindowa Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function GetForegroundWindow Lib "user32" () As Long

Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long