dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Closing a Yes/No message box via code.

Threaded View

  1. #1
    Join Date
    Oct 2002
    Location
    USA, California
    Posts
    58

    Closing a Yes/No message box via code.

    I need the ability to close message boxes. I've found a few code examples explaining how to do this, but none of them work for message boxes set to vbYesNo. I've tried sending the following messages to it:
    - SendMessage hWndOfMsgBox, WM_CLOSE, 0, ByVal 0&
    - PostMessage hWndOfMsgBox, WM_CLOSE, 0, ByVal 0&
    - SendMessage hWndOfMsgBox, WM_DESTROY, 0, ByVal 0&
    - SendMessage hWndOfMsgBox, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&

    None of these will work. It seems that the message box steals these commands. I've also tried using SendKeys to do...
    - {ESC}
    - %{F4}
    These won't work either. (Sending ALT-N is not an option because my app doesn't know if it's a vbYesNo message box. Unless there is a way to identify the type of message box that is open.)

    This is very frustrating!
    I hope you all can find a solution, because I think I'm at a wit's end.
    (Note: Creating my own message boxes via forms is not an option, because I need the ability to close message boxes not owned by my program. Such as those owned by common dialogs and 3rd party tools.)

    Here is my test code if you want to take a crack at it. The timer in this code will only trigger on the compiled EXE, not when running it from the IDE.
    Code:
    Option Explicit
    
    ' Define win32 callbacks.
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    ' Define PostMessage constants.
    Private Const WM_DESTROY As Long = &H2
    Private Const WM_CLOSE As Long = &H10
    Private Const WM_SYSCOMMAND As Long = &H112
    Private Const SC_CLOSE As Long = &HF060
    
    ' Called when a command button named "cmdTest" is clicked.
    Private Sub cmdTest_Click()
       timTest.Interval = 1000
       timTest.Enabled = True
       MsgBox "This is a test!", vbYesNo, "The Test"
    End Sub
    
    ' Called when a timer control named "timTest" is triggered.
    Private Sub timTest_Timer()
       Dim hWnd As Long
    
       timTest.Enabled = False
       hWnd = FindWindow(vbNullString, "The Test")
       If (hWnd <> 0) Then
          SendMessage hWnd, WM_CLOSE, 0, ByVal 0&
    '      SendMessage hWnd, WM_DESTROY, 0, ByVal 0&
    '      SendMessage hWnd, WM_SYSCOMMAND, SC_CLOSE, 0&
       Else
          MsgBox "Error finding message box!", vbOKOnly
       End If
    End Sub
    Last edited by JQuick; June 14th, 2003 at 08:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)