How do I close a child window with its handle?

Posted on 2008-10-10
Last Modified: 2010-04-21
I want to close a window after a match a specific pattern.  What's the best way to close the window?

In the below example if the WindowText has "News" within it I want to close that window.

Thanks for you help.


  Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String
        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)
        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))
        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

        If sSave.IndexOf("News") >= 0 Then
               If (sSave.Length <> 0) Then

               End If
            End If
        End If
        If sSave.Trim <> "" Then
        End If
        Return 1 'continue enumeration        
    End Function
Question by:vaultworld
LVL 10

Expert Comment

ID: 22689728
here is how to get the window handle


to close a window you can use

unload me

among a lot other ways to do so.

waiting for your reply
LVL 11

Expert Comment

ID: 22689764
You need to send the WM_CLOSE message to the Window.

 Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As UInt32) As Long
Public Const WM_CLOSE = &H10

Open in new window

LVL 11

Expert Comment

ID: 22689775
A sample call might be:

Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As UInt32) As Long
Public Const WM_CLOSE = &H10
SendMessage(windowHandle, WM_CLOSE,0,0)
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 22689797
Get the "root" window using GetAncestor() with GA_ROOT.  Then send a WM_CLOSE directive with SendMessage():

Public Const GA_ROOT As Integer = 2
Public Declare Function GetAncestor Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal gaFlags As Integer) As IntPtr
Public Const WM_CLOSE As Integer = &H10
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Dim rootWindow As IntPtr = GetAncestor(hwnd, GA_ROOT)
    SendMessage(rootWindow, WM_CLOSE, 0, 0)

Open in new window

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22689809
@ladarling...that's a VB6 declaration with "Long" in it...  =\
LVL 11

Expert Comment

ID: 22689932
My bad.

Author Closing Comment

ID: 31505124
Thanks again,

Now that i have the size and position of the window.

I did have another idea on closing the application simply send a mouse click to the "X" in the upper right to close it.


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question