Use SendMessage to clear a pop up message box

Posted on 2009-12-20
Medium Priority
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

Author Comment

ID: 26093648
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.


Accepted Solution

bhagwantsingh earned 2000 total points
ID: 26094657
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 26095934
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 86

Expert Comment

by:Mike Tomlinson
ID: 26096083
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

ID: 26101620
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.

Expert Comment

ID: 26102347
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

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.

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 …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 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