Window handle for a MessageBox


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Window handle for a MessageBox

  1. #1
    Leonardo Bosi Guest

    Window handle for a MessageBox

    Hello,

    From my VB app, I have to show a message box (using the MsgBox native VB
    function, or one of the MessageBox API functions), and be able to remove the
    message box from my code when a Timer event occurs (if the user didn't close
    it until that moment). To achieve this, I thought I could get the handle to
    the message box window, in order to close that window (using either
    CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to get
    the window handle for a message box I showed from my code? I read the
    documentation for the different API functions used to create message boxes
    (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any of
    them a way for me to get that window handle...

    Any help would be appreciated...

    Thanks,


    Leonardo Bosi
    Proximity Soft
    Buenos Aires, Argentina
    --------------------------------------------
    leobosi@proximity.com.ar
    http://www.proximity.com.ar



  2. #2
    Jonathan Wood Guest

    Re: Window handle for a MessageBox

    I don't think you can do this for the simple reason that VB code does not
    run while a message box is displayed.

    Moreover, if it did, this seems like a lot of hassle. I would definitely
    just create a custom form for your message box and implement your timer in
    the code for that form.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    Leonardo Bosi <leobosi@proximity.com.ar> wrote in message
    news:3915eebe@news.devx.com...
    > Hello,
    >
    > From my VB app, I have to show a message box (using the MsgBox native VB
    > function, or one of the MessageBox API functions), and be able to remove

    the
    > message box from my code when a Timer event occurs (if the user didn't

    close
    > it until that moment). To achieve this, I thought I could get the handle

    to
    > the message box window, in order to close that window (using either
    > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to get
    > the window handle for a message box I showed from my code? I read the
    > documentation for the different API functions used to create message boxes
    > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any of
    > them a way for me to get that window handle...
    >
    > Any help would be appreciated...
    >
    > Thanks,
    >
    >
    > Leonardo Bosi
    > Proximity Soft
    > Buenos Aires, Argentina
    > --------------------------------------------
    > leobosi@proximity.com.ar
    > http://www.proximity.com.ar
    >
    >




  3. #3
    Jonathan Wood Guest

    Re: Window handle for a MessageBox

    I don't think you can do this for the simple reason that VB code does not
    run while a message box is displayed.

    Moreover, if it did, this seems like a lot of hassle. I would definitely
    just create a custom form for your message box and implement your timer in
    the code for that form.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    Leonardo Bosi <leobosi@proximity.com.ar> wrote in message
    news:3915eebe@news.devx.com...
    > Hello,
    >
    > From my VB app, I have to show a message box (using the MsgBox native VB
    > function, or one of the MessageBox API functions), and be able to remove

    the
    > message box from my code when a Timer event occurs (if the user didn't

    close
    > it until that moment). To achieve this, I thought I could get the handle

    to
    > the message box window, in order to close that window (using either
    > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to get
    > the window handle for a message box I showed from my code? I read the
    > documentation for the different API functions used to create message boxes
    > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any of
    > them a way for me to get that window handle...
    >
    > Any help would be appreciated...
    >
    > Thanks,
    >
    >
    > Leonardo Bosi
    > Proximity Soft
    > Buenos Aires, Argentina
    > --------------------------------------------
    > leobosi@proximity.com.ar
    > http://www.proximity.com.ar
    >
    >




  4. #4
    Sixto Santos Guest

    Re: Window handle for a MessageBox

    Hello Jonathan!

    Although it is true that an application would stop execution while a MsgBox
    is being displayed, a Timer control would still execute the Timer event but
    only at run-time, so you must compile and run the application to take
    advantage of it.

    Knowing this we can use the FindWindow and the SendMessage API functions to
    find and close the message box.

    To test it, add a Command button and a Timer to a form, add the code bellow
    and compile and run the program. The message box would close one second
    after being displayed.

    ' CODE BEGINS ----------------------
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
    Long) As Long
    Private Const WM_CLOSE = &H10

    Private Sub Command1_Click()
    Timer1.Interval = 1000
    Timer1.Enabled = True
    MsgBox "Close Me", vbExclamation, "MsgBox Caption"

    End Sub

    Private Sub Timer1_Timer()
    Dim hwnd As Long

    hwnd = FindWindow("#32770", "MsgBox Caption")
    If hwnd > 0 Then
    Call SendMessage(hwnd, WM_CLOSE, 0, 0)
    End If

    End Sub

    ' CODE ENDS ----------------------

    On the other hand, I would definitely go with your suggestion and create a
    custom message box with a timer.


    Regards,

    Sixto



    Jonathan Wood <jwood@softcircuits.com> wrote in message
    news:3916034e$1@news.devx.com...
    > I don't think you can do this for the simple reason that VB code does not
    > run while a message box is displayed.
    >
    > Moreover, if it did, this seems like a lot of hassle. I would definitely
    > just create a custom form for your message box and implement your timer in
    > the code for that form.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > Leonardo Bosi <leobosi@proximity.com.ar> wrote in message
    > news:3915eebe@news.devx.com...
    > > Hello,
    > >
    > > From my VB app, I have to show a message box (using the MsgBox native VB
    > > function, or one of the MessageBox API functions), and be able to remove

    > the
    > > message box from my code when a Timer event occurs (if the user didn't

    > close
    > > it until that moment). To achieve this, I thought I could get the handle

    > to
    > > the message box window, in order to close that window (using either
    > > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to

    get
    > > the window handle for a message box I showed from my code? I read the
    > > documentation for the different API functions used to create message

    boxes
    > > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any

    of
    > > them a way for me to get that window handle...
    > >
    > > Any help would be appreciated...
    > >
    > > Thanks,
    > >
    > >
    > > Leonardo Bosi
    > > Proximity Soft
    > > Buenos Aires, Argentina
    > > --------------------------------------------
    > > leobosi@proximity.com.ar
    > > http://www.proximity.com.ar
    > >
    > >

    >
    >




  5. #5
    Sixto Santos Guest

    Re: Window handle for a MessageBox

    Hello Jonathan!

    Although it is true that an application would stop execution while a MsgBox
    is being displayed, a Timer control would still execute the Timer event but
    only at run-time, so you must compile and run the application to take
    advantage of it.

    Knowing this we can use the FindWindow and the SendMessage API functions to
    find and close the message box.

    To test it, add a Command button and a Timer to a form, add the code bellow
    and compile and run the program. The message box would close one second
    after being displayed.

    ' CODE BEGINS ----------------------
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
    Long) As Long
    Private Const WM_CLOSE = &H10

    Private Sub Command1_Click()
    Timer1.Interval = 1000
    Timer1.Enabled = True
    MsgBox "Close Me", vbExclamation, "MsgBox Caption"

    End Sub

    Private Sub Timer1_Timer()
    Dim hwnd As Long

    hwnd = FindWindow("#32770", "MsgBox Caption")
    If hwnd > 0 Then
    Call SendMessage(hwnd, WM_CLOSE, 0, 0)
    End If

    End Sub

    ' CODE ENDS ----------------------

    On the other hand, I would definitely go with your suggestion and create a
    custom message box with a timer.


    Regards,

    Sixto



    Jonathan Wood <jwood@softcircuits.com> wrote in message
    news:3916034e$1@news.devx.com...
    > I don't think you can do this for the simple reason that VB code does not
    > run while a message box is displayed.
    >
    > Moreover, if it did, this seems like a lot of hassle. I would definitely
    > just create a custom form for your message box and implement your timer in
    > the code for that form.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > Leonardo Bosi <leobosi@proximity.com.ar> wrote in message
    > news:3915eebe@news.devx.com...
    > > Hello,
    > >
    > > From my VB app, I have to show a message box (using the MsgBox native VB
    > > function, or one of the MessageBox API functions), and be able to remove

    > the
    > > message box from my code when a Timer event occurs (if the user didn't

    > close
    > > it until that moment). To achieve this, I thought I could get the handle

    > to
    > > the message box window, in order to close that window (using either
    > > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to

    get
    > > the window handle for a message box I showed from my code? I read the
    > > documentation for the different API functions used to create message

    boxes
    > > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any

    of
    > > them a way for me to get that window handle...
    > >
    > > Any help would be appreciated...
    > >
    > > Thanks,
    > >
    > >
    > > Leonardo Bosi
    > > Proximity Soft
    > > Buenos Aires, Argentina
    > > --------------------------------------------
    > > leobosi@proximity.com.ar
    > > http://www.proximity.com.ar
    > >
    > >

    >
    >




  6. #6
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Jonathan,

    You're right, when using the native VB elements (Timer control and the
    MsgBox function) for this task, the VB code doesn't continue executing until
    the user closes the MsgBox, and timer events don't fire. But if the
    application is compiled as an executable, it does (see Sixto's message in
    this thread). Additionally, if we use the MessageBox API function instead of
    the MsgBox VB native function, it also works while in the VB IDE.

    Thanks!


    Leonardo Bosi
    Proximity Soft
    Buenos Aires, Argentina
    --------------------------------------------
    leobosi@proximity.com.ar
    http://www.proximity.com.ar



  7. #7
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Jonathan,

    You're right, when using the native VB elements (Timer control and the
    MsgBox function) for this task, the VB code doesn't continue executing until
    the user closes the MsgBox, and timer events don't fire. But if the
    application is compiled as an executable, it does (see Sixto's message in
    this thread). Additionally, if we use the MessageBox API function instead of
    the MsgBox VB native function, it also works while in the VB IDE.

    Thanks!


    Leonardo Bosi
    Proximity Soft
    Buenos Aires, Argentina
    --------------------------------------------
    leobosi@proximity.com.ar
    http://www.proximity.com.ar



  8. #8
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Sixto,

    Thanks for your help, I solved my problem using that code. Note that I used
    the MessageBox API function instead of the MsgBox native VB function (this
    way I was able to test my program while in the VB IDE).

    Thanks again!


    Leonardo Bosi
    Proximity Soft
    Buenos Aires, Argentina
    --------------------------------------------
    leobosi@proximity.com.ar
    http://www.proximity.com.ar



  9. #9
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Sixto,

    Thanks for your help, I solved my problem using that code. Note that I used
    the MessageBox API function instead of the MsgBox native VB function (this
    way I was able to test my program while in the VB IDE).

    Thanks again!


    Leonardo Bosi
    Proximity Soft
    Buenos Aires, Argentina
    --------------------------------------------
    leobosi@proximity.com.ar
    http://www.proximity.com.ar



  10. #10
    Karl E. Peterson Guest

    Re: Window handle for a MessageBox

    Hi Leonardo --

    I wrote about this very problem in the Jan99 VBPJ. If you have that issue, pull it
    out for the full commentary. Or, hit my site and download the TimedMsg.zip sample.
    No need to go to the MessageBox API, unless you're not comfortable testing that
    routine as an EXE.

    Later... Karl
    --
    http://www.mvps.org/vb



    "Leonardo Bosi" <leobosi@proximity.com.ar> wrote in message
    news:3915eebe@news.devx.com...
    > Hello,
    >
    > From my VB app, I have to show a message box (using the MsgBox native VB
    > function, or one of the MessageBox API functions), and be able to remove the
    > message box from my code when a Timer event occurs (if the user didn't close
    > it until that moment). To achieve this, I thought I could get the handle to
    > the message box window, in order to close that window (using either
    > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to get
    > the window handle for a message box I showed from my code? I read the
    > documentation for the different API functions used to create message boxes
    > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any of
    > them a way for me to get that window handle...
    >
    > Any help would be appreciated...
    >
    > Thanks,
    >
    >
    > Leonardo Bosi
    > Proximity Soft
    > Buenos Aires, Argentina
    > --------------------------------------------
    > leobosi@proximity.com.ar
    > http://www.proximity.com.ar
    >
    >




  11. #11
    Karl E. Peterson Guest

    Re: Window handle for a MessageBox

    Hi Leonardo --

    I wrote about this very problem in the Jan99 VBPJ. If you have that issue, pull it
    out for the full commentary. Or, hit my site and download the TimedMsg.zip sample.
    No need to go to the MessageBox API, unless you're not comfortable testing that
    routine as an EXE.

    Later... Karl
    --
    http://www.mvps.org/vb



    "Leonardo Bosi" <leobosi@proximity.com.ar> wrote in message
    news:3915eebe@news.devx.com...
    > Hello,
    >
    > From my VB app, I have to show a message box (using the MsgBox native VB
    > function, or one of the MessageBox API functions), and be able to remove the
    > message box from my code when a Timer event occurs (if the user didn't close
    > it until that moment). To achieve this, I thought I could get the handle to
    > the message box window, in order to close that window (using either
    > CloseWindow or DestroyWindow). So I'm asking: Is there a way for me to get
    > the window handle for a message box I showed from my code? I read the
    > documentation for the different API functions used to create message boxes
    > (MessageBox, MessageBoxEx, MessageBoxIndirect), and didn't find in any of
    > them a way for me to get that window handle...
    >
    > Any help would be appreciated...
    >
    > Thanks,
    >
    >
    > Leonardo Bosi
    > Proximity Soft
    > Buenos Aires, Argentina
    > --------------------------------------------
    > leobosi@proximity.com.ar
    > http://www.proximity.com.ar
    >
    >




  12. #12
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Karl,

    Yes, your sample was enough for me to solve my problem. It was recommended
    by several fellows...

    Thanks for your frequent contributions!




  13. #13
    Leonardo Bosi Guest

    RE: Window handle for a MessageBox

    Hello Karl,

    Yes, your sample was enough for me to solve my problem. It was recommended
    by several fellows...

    Thanks for your frequent contributions!




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center