MistakesWereMade
Board Regular
- Joined
- May 22, 2019
- Messages
- 103
I have a code that determines if a window is open or not based on input. It works well for all other programs, but for some reason does not detect when osk.exe is running... Any ideas? I basically put anything into the msgbox in my code between the quotation marks and it will search for a similarly named window that is open.
Code:
Code:
Code:
Option Explicit
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function GetForegroundWindow Lib "user32.dll" () As Long
Private Const GW_HWNDNEXT = 2
Private 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
'*******************************************
Private Sub CommandButton1_Click()
MsgBox FindWindowLike("adobe")
End Sub
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