VB如何获得相应窗体的句柄和按钮
有没有用VB获得相应窗体的句柄和按钮的文字教程呢?如果有视频教程那最好了。因为我是新手,所以视频教程会更直观。
如果有的话,我会追分的。。。
参考答案:方法1:
窗体名句柄 如:frmStu.hwnd
按钮标题: Command1.Caption
方法2:
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