Solved

How do I close a child window with its handle?

Posted on 2008-10-10
7
805 Views
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.

CODE:

  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
            'WANT TO CLOSE WINDOW
        Else
               If (sSave.Length <> 0) Then
 
                   Lst.Items.Add(sSave)

               End If
            End If
        End If
        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If
 
        Return 1 'continue enumeration        
    End Function
0
Comment
Question by:vaultworld
7 Comments
 
LVL 10

Expert Comment

by:Smart_Man
ID: 22689728
here is how to get the window handle

me.handle

to close a window you can use

unload me

among a lot other ways to do so.

waiting for your reply
0
 
LVL 11

Expert Comment

by:ladarling
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

0
 
LVL 11

Expert Comment

by:ladarling
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)
 
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 85

Accepted Solution

by:
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

0
 
LVL 85

Expert Comment

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

Expert Comment

by:ladarling
ID: 22689932
My bad.
 
0
 
LVL 1

Author Closing Comment

by:vaultworld
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.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

837 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