VB.net中通过窗口名称取得后台窗口句柄
下面是VB.net的代码
和VB的区别在于数据类型
vb中long是4byte,
vb.net中间Integer是4byte
用的时候写法要互相转换一下
不放心的话可以都写成Int32
1 Public Class Form1 2 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer 3 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 Integer 4 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32 5 6 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 7 Dim myhwnd As Integer 8 Dim subHwnd As Integer 9 myhwnd = FindWindow("Notepad", "test.txt - メモ帳") 10 subHwnd = FindWindowEx(myhwnd, 0, "Edit", 0) 11 SendMessage(subHwnd, &H302, 0, 0) 12 'MsgBox(subHwnd) 13 End Sub 14 End Class
myhwnd = FindWindow("Notepad", "test.txt - メモ帳")
FindWindow查找窗口句柄
两个参数,第一个是类名,第二个是窗口的标题,返回值是窗口句柄
不清楚的情况下,两个参数中间的一个可以设成0
subHwnd = FindWindowEx(myhwnd, 0, "Edit", 0)
FindWindowEx查找子窗口句柄 4个参数, 第一个是父窗口句柄, 第二个是子窗口次序(父窗口下第几个子窗口), 第三个是子窗口类型, 第四个是子窗口名字
SendMessage(subHwnd, &H302, 0, 0)
SendMessage将指定的消息发送到一个或多个窗口 此函数太过于强大,不多做解释
总之消息不是普通的字符串, 而是包括鼠标,键盘等一切消息 这里&H302消息为粘贴到指定句柄

浙公网安备 33010602011771号