首先聲明一下API
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的廣南網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
在你窗口最頂?shù)奈恢蒙蠈?xiě)上
Private Declare Function 取窗口句柄 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'由于VB支持中文的 我就定義成 取窗口句柄 這個(gè)了
'在窗體中添加一個(gè)按鈕里面放進(jìn)下面代碼就OK了
Private Sub Command1_Click()
Dim 窗口句柄 As Long '聲明窗口句柄變量存儲(chǔ)
窗口句柄 = 取窗口句柄(vbNullString, "新建 文本文檔.txt - 記事本") '取窗口標(biāo)題的句柄 把得到窗口標(biāo)題句柄賦予給剛才 窗口句柄 這變量里
If 窗口句柄 = 0 Then '判斷窗口句柄是否等于0 如果等于0表示無(wú)法找到窗口句柄
MsgBox "無(wú)法找到需要的窗口句柄"
Else
MsgBox "已找到窗口句柄 句柄數(shù)為:" 窗口句柄
End If
End Sub
'創(chuàng)建一個(gè)列表框和一個(gè)按鈕
'API函數(shù)定義
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Private Sub Command1_Click()
Dim s As Long
Dim a As String
a = Space(255)
s = GetWindow(Me.hwnd, GW_HWNDFIRST) '獲得句柄
GetWindowText s, a, 255 '獲得標(biāo)題
Dim ab As String * 255
GetClassName s, ab, 255 '獲得類(lèi)名
Dim ClassName As String
ClassName = StripTerFlag(ab) 'StripTerFlag是一個(gè)自定義函數(shù),后面有代碼,用于去處結(jié)束符
If UCase(ClassName) = "NOTEPAD" Then
List1.AddItem a
List1.ItemData(List1.ListCount - 1) = s
End If
Do While s
DoEvents
s = GetWindow(s, GW_HWNDNEXT) '獲得句柄
GetWindowText s, a, 255 '獲得標(biāo)題
GetClassName s, ab, 255 '獲得類(lèi)名
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
FindWindowEx
VB聲明
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
說(shuō)明
在窗口列表中尋找與指定條件相符的第一個(gè)子窗口
返回值
Long,找到的窗口的句柄。如未找到相符窗口,則返回零。會(huì)設(shè)置GetLastError
參數(shù)表
參數(shù)
類(lèi)型及說(shuō)明
hWnd1
Long,在其中查找子的父窗口。如設(shè)為零,表示使用桌面窗口(通常說(shuō)的頂級(jí)窗口都被認(rèn)為是桌面的子窗口,所以也會(huì)對(duì)它們進(jìn)行查找)
hWnd2
Long,從這個(gè)窗口后開(kāi)始查找。這樣便可利用對(duì)FindWindowEx的多次調(diào)用找到符合條件的所有子窗口。如設(shè)為零,表示從第一個(gè)子窗口開(kāi)始搜索
lpsz1
String,欲搜索的類(lèi)名。零表示忽略
lpsz2
String,欲搜索的類(lèi)名。零表示忽略