PDA

View Full Version : Check if a window is open



Zaund
11-12-2005, 02:50 PM
what is the command to check if the window is open and then display an error if it is closed?

Twix
11-12-2005, 02:55 PM
In what language, and you can just check to see if the process is open like making a hack.

Dyndrilliac
11-12-2005, 02:59 PM
BOOL IsWindowOpen(LPCTSTR szWindowTitle) {
if (FindWindow(NULL, szWindowTitle) != NULL) {
return TRUE;
}

return FALSE;
}

LCS
11-12-2005, 03:26 PM
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
Dim hwnd As Long
Dim strWindow as String
strWindow = ""
hwnd = FindWindow(vbNullString, strWindow)
If (hwnd = 0) Then
MsgBox "Window not found!"
End If
End Sub

LCS
11-12-2005, 03:36 PM
And as a function:

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function IsWindowOpen(ByVal WindowName As String) As Boolean
Dim hwnd As Long
hwnd = FindWindow(vbNullString, WindowName)
If (hwnd = 0) Then
MsgBox "Window not found!"
IsWindowOpen = False
Else
MsgBox "Window found!"
IsWindowOpen = True
End If
End Function

To use the function you would type something like this:

Private Sub Form_Load()
IsWindowOpen "Untitled - Notepad"
End Sub
And the output will be "Window found!" if the window is open and "Window not found!" if it isn't open.


Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function IsWindowOpen(ByVal WindowName As String) As Boolean
Dim hwnd As Long
hwnd = FindWindow(vbNullString, WindowName)
If (hwnd = 0) Then
MsgBox "Window not found!"
IsWindowOpen = False
Else
MsgBox "Window found!"
IsWindowOpen = True
End If
End Function

Private Sub Form_Load()
IsWindowOpen "Untitled - Notepad"
End Sub

Dyndrilliac
11-12-2005, 10:33 PM
Heh, notice how the C code is shorter :P