Use SendMessage to clear a pop up message box

Posted on 2009-12-20
Last Modified: 2012-05-08
Hi Experts,

The attached code will pop a three button message box.

I would like to write an application that will detect this message box and click one of the buttons of my choosing. I know SendMessage is the function I need but I can not find usable examples to follow.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MessageBox.Show("Message Box Text", "Message Box Title", MessageBoxButtons.AbortRetryIgnore)

    End Sub

End Class

Open in new window

Question by:DColin
    LVL 13

    Expert Comment


    Author Comment

    Hi SameerJagdale,

    This code uses WM_NCDESTROY to close the message box I would like to click one of the buttons (Abort, Retry, Cancel)  to close it.

    LVL 5

    Accepted Solution

    It should work
    Private Sub ClickButton()
    Dim hwndDialog As Long
    Dim hwndButton As Long
    Dim retval As Long
    hwndDialog = FindWindow(CLng(0), "Notepad")
    If hwndDialog = 0 Then Exit Sub
    hwndButton = FindWindowEx(hwndDialog, 0, CLng(0), "&Yes")
    retval = SetActiveWindow(hwndDialog)
    retval = SendMessage(hwndButton, BM_CLICK, ByVal CLng(0), ByVal CLng(0))
    retval = SendMessage(hwndButton, BM_CLICK, ByVal CLng(0), ByVal CLng(0))
    End Sub

    Open in new window


    Author Comment

    Hi bhagwantsingh:,

    I am assuming that "Notepad" in the line:

    hwndDialog = FindWindow(CLng(0), "Notepad")

    refers to the ParentClass. How do I go about finding the ParentClass of an application?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    No...the second parameter is used to match the CAPTION of the window.  If you want to search by classname then you use the first parameter.

    You could also try this approach:

    Author Comment

    I have replaced the FindWindowX Class arg. with vbNullString from CLng(0) and this code is working but can somone tell me where I can find a list of the dialog button codes. &Yes was used in the example.
    LVL 5

    Expert Comment

    You can use GetParent API. The GetParent function retrieves the handle of the specified child windows parent window and than GetClassName function to get class name of the parent window.

    Hope this will help you
    Private Sub Form_Load()
        Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
        'Ask for a Window title
        Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
        'Search the window
        WinWnd = FindWindow(vbNullString, Ret)
        If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
        'Show the window
        ShowWindow WinWnd, SW_SHOWNORMAL
        'Create a buffer
        lpClassName = Space(256)
        'retrieve the class name
        RetVal = GetClassName(WinWnd, lpClassName, 256)
        'Show the classname
        MsgBox "Classname: " + Left$(lpClassName, RetVal)
        'Post a message to the window to close itself
        PostMessage WinWnd, WM_CLOSE, 0&, 0&
    End Sub

    Open in new window


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now