CodeItBetter Programming Another VB Programming Blog

How to close the child windows in an external application.

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'System & API - How to close the child windows in an external application.
'We need to use FindWindow, FindWindowEx, SendMessage API's

Option Explicit
 
'API Declarations to Find Parent and Child windows
'To retrieve a handle to the top-level window whose class name and 
'window name match the specified strings.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
'To retrieve a handle to a window whose class name and window name match the specified strings
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
 
'API Declarations to send messages to Parent and Child windows
'To send the specified message to a window or windows.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
 
'To terminate a window or an application
Private Const WM_CLOSE = &H10
 
Sub Main()
    Dim hMain As Long, hChild As Long
    'To Find the Main Window
    hMain = FindWindow("CLASS_NAME_OF_MAIN_APPLICATION", vbNullString)
    'To find the MDI Client window in window
    hChild = FindWindowEx(hMain, 0, "CLASS_NAME_OF_MDI_Client", vbNullString)
    'To find the CHILD window in window
    hChild = FindWindowEx(hChild, 0, "CLASS_NAME_OF_CHILD_WINDOW", vbNullString)
    'If found then
    If hChild <> 0 Then
        'if the CHILD window found then Close it.
        Call SendMessage(hChild, WM_CLOSE, 0, 0)
    End If
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.