Results 1 to 8 of 8

Thread: How to close a message box using code?

  1. #1

    Thread Starter
    Addicted Member rbnwares's Avatar
    Join Date
    Sep 2000
    Location
    Philippines
    Posts
    142

    Question

    I have a problem. When I use a particular activeX a msgbox always appear. Can I close the msgbox by code? Any kind of help is appreciated.
    Stupidity is better than cure.

    VB6 SP5 Enterprise Ed.
    C, Pascal, VC++ 6.0


    Running Win98 SE and Win2000 Prof. Ed.


    Email me at : [email protected]

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    If you created the message box yourself then its difficult but possible. See http://www.mvps.org/vbvision and look in the code samples.
    If you didn't create the message box then it's going to be very complex...
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Actually it's not that easy as just closing it, because the MsgBox is Modal for your program (in other words, you can't do anything with your prog until the MsgBox is closed )

    But I wrote this Sub that closes the MsgBox:

    Code:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Const WM_CLOSE = &H10
    Public Sub CloseBox()
    Dim BoxH&
       BoxH = FindWindow(vbNullString, "CLOSEME") 'find the handle of the msgbox
       PostMessage BoxH, WM_CLOSE, 0&, 0& 'Close it!
    End Sub
    You could put it in a another program and shell it, but that would be real weak , so now the actual trick is to create another ASynchronous thread, so it can be executed while the MsgBox is open

    I wrote this code for ya, put it in a form.
    Code:
    Private hThread As Long, hThreadID As Long
    
    Private Sub Form_Activate()
        hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf CloseBox, ByVal 0&, ByVal 0&, hThreadID) 'create the thread
    
    'show the msgbox (with the caption CLOSEME, that's important
    'because you find the window by the caption (in this case))
    MsgBox "hey", , "CLOSEME" 
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
        CloseHandle hThread 'Close the thread
    End Sub
    Cool huh
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4
    Junior Member
    Join Date
    Sep 2000
    Location
    Jacksonville
    Posts
    23

    Jop, What is "AddressOf CloseBox" argument?
    Thanks!

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    It gets the Memory Adress of the Callback routine we're calling (CloseBox)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  6. #6
    Junior Member
    Join Date
    Sep 2000
    Location
    Jacksonville
    Posts
    23

    Smile

    I tried to do it. It would close any other app with 'CLOSEME' caption but couldn't close the message box from the same app. Could you please let me know what could I be missing? Or send me a sample app. My e-mail is
    [email protected]
    Thanks a bunch!

  7. #7
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    I can't distribute the source as it's not mine, but heres the link to Brian Staffords site (VB God) BTW: this code is fairly complex.

    http://www.mvps.org/vbvision/_sample...geBox_Demo.zip
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  8. #8
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Look, it finds the window by it's caption, in the case of the Example it's CLOSEME, now when you want to change the caption, be sure to change it in the Sub CloseBox (in the module) too.

    And the code in the Form is very important because it starts a new thread, so VB can close the msgbox (usally when you open a MsgBox, the program is paused until you close the box)

    Now let's say you want a MsgBox with the caption Are You Sure?

    in the form you would type

    Code:
    Private hThread As Long, hThreadID As Long
    
    Private Sub Form_Activate()
        hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf CloseBox, ByVal 0&, ByVal 0&, hThreadID) 'create the thread
    
    'show the msgbox (with the caption Are You Sure?, that's important
    'because you find the window by the caption (in this case))
    MsgBox "hey", , "Are You Sure?" 
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
        CloseHandle hThread 'Close the thread
    End Sub
    and in the module
    Code:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Const WM_CLOSE = &H10
    Public Sub CloseBox()
    Dim BoxH&
       BoxH = FindWindow(vbNullString, "Are You Sure?") 'find the handle of the msgbox
       PostMessage BoxH, WM_CLOSE, 0&, 0& 'Close it!
    End Sub
    Hope it helps
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

Posting Permissions

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



Click Here to Expand Forum to Full Width