一、从标题获取窗口句柄:

Option Explicit

'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
Dim strCaption As String, lhWnd As Long

'Exact caption of the window:
strCaption = "Untitled - Notepad"
lhWnd = FindWindow(vbNullString, strCaption)

'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
MsgBox "Notepad found: " & lhWnd
End If
End Sub

二、从窗口类名获取窗口句柄

Option Explicit

'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
Dim strClassName As String, lhWnd As Long

'Class Name of the window:
strClassName = "Notepad"
lhWnd = FindWindow(strClassName, vbNullString)

'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
MsgBox "Notepad found: " & lhWnd
End If
End Sub
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。