findwindowlike.txt

来自「一部分关于VB编程的小技巧」· 文本 代码 · 共 39 行

TXT
39
字号
Option Explicit
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetForegroundWindow Lib "user32.dll" () As Long

Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5

'********************************************
'*Give it part of the window text your looking for
'*it will give you the hWnd
'*usefull for windows that text is like "[project] - microsoft visual basic [design]"
'*usage:
'*Msgbox FindWindowLike("visual basic")
'*Returns 0 if not found
'*******************************************

Function FindWindowLike(strPartOfCaption As String) As Long
Dim hWnd As Long
Dim strCurrentWindowText As String
Dim r As Integer

hWnd = GetForegroundWindow

Do Until hWnd = 0
        strCurrentWindowText = Space$(255)
        r = GetWindowText(hWnd, strCurrentWindowText, 255)
        strCurrentWindowText = Left$(strCurrentWindowText, r)
        'hWnd = GetWindow(hWnd, GW_CHILD)
        If InStr(1, LCase(strCurrentWindowText), LCase(strPartOfCaption)) <> 0 Then GoTo Found
        hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop

Exit Function
Found:
FindWindowLike = hWnd
End Function

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?