PDA

Click to See Complete Forum and Search --> : If window open then close it


rino_2
Aug 10th, 2000, 01:29 PM
Hi,

Does anybody know how I can detect if there is a window open with the name “window” and then close that window if it is open? I’m quite sure this can be done with the windows APIs but I don’t have a clue how? Thanks

Aug 10th, 2000, 01:42 PM
Try this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Const WM_DESTROY = &H2

Private Sub Command1_Click()

'Get the hWnd of the Window named Calculator
MyHwnd = FindWindow(vbNullString, "Calculator")
'If it's found then close it
If MyHwnd <> 0 Then
SendMessage MyHwnd, WM_CLOSE, 0&, 0&
SendMessage MyHwnd, WM_DESTROY, 0&, 0&
End If

End Sub

rino_2
Aug 10th, 2000, 02:17 PM
Thanks for the help Megatron!

Aug 10th, 2000, 03:03 PM
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Const WM_DESTROY = &H2

Private Sub Command1_Click()

'Get the hWnd of the Window named Calculator
MyHwnd = FindWindow(vbNullString, "Calculator")
'If it's found then close it
If MyHwnd <> 0 Then
SendMessage MyHwnd, WM_CLOSE, 0&, 0&
SendMessage MyHwnd, WM_DESTROY, 0&, 0&
End If

End Sub




a few days ago I tried this with IE,
I used the class IEFrame, and all it did was disable IE, and it wouldnt allow me to close it normally i had to end-task it.