Results 1 to 6 of 6

Thread: [RESOLVED] Problem closing msgboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    39

    Resolved [RESOLVED] Problem closing msgboxes

    Hi, I am developing a visual basic application. I have implemented no modal msgbox as following:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function MsgboxNoModal Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal
    4.  
    5. lpCaption As String, ByVal wType As Long) As Long
    6.  
    7. Const MB_ICONASTERISK = &H40&
    8.  
    9. Private Sub NoModalMsgboxExample()
    10.   Call MsgboxNoModal(100, "This is NO modal.", "No Modal Msgbox Example", MB_ICONASTERISK)
    11. End Sub

    This works ok. The problem is that if I dont close all the message boxes that appear during execution of the application, when I finish the application it keeps on standby until I close all the message boxes, and then finishes.

    If I add the following code I only close the last msgbox opened, but I would like to close all the message boxes that remain opened. Is there any way to close automatically all the message boxes that remain opened when I finish the application?

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2.  
    3. 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
    4.  
    5. Private Const WM_CLOSE = &H10
    6.  
    7. Private Sub Close_application()
    8.     Dim hWnd As Long
    9.     hWnd = FindWindow("#32770", "No Modal Msgbox Example")
    10.     SendMessage hWnd, WM_CLOSE, 0, 0
    11.     End
    12. End Sub

    I am a beginner using API, so I would appreciate any help. Thank you very much for your help!!!

  2. #2

    Re: Problem closing msgboxes

    i don't have VB open, but try doing somthing like this:

    VB Code:
    1. Private Sub Close_application()
    2.     Dim hWnd&
    3.  
    4.     Do:Doevents
    5.         hWnd& = FindWindow("#32770", "No Modal Msgbox Example")
    6.         X& = SendMessage (hWnd&, WM_CLOSE, 0, 0)
    7.     Loop until hwnd& = 0
    8.     End
    9. End Sub
    Last edited by ghettohacker; Dec 16th, 2005 at 07:13 PM. Reason: i'm still learning

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    39

    Re: Problem closing msgboxes

    Thanks ghettohacker. I have tried the code you suggested but only the last msgbox opened is closed, and the program go into a infinite loop (the FindWindow function always returns the same hWnd).

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Problem closing msgboxes

    Try this ,
    Add a hidden form in your project. (In my case Form2).
    Set Form2 as the owner-window of the message box. (Use hWnd argument)
    When exiting the program, unload Form2. (our UnloadAll sub does it)

    Inside your Main form :
    VB Code:
    1. '[b] Inside Main form[/b]
    2. Option Explicit
    3.  
    4. Private Declare Function MsgboxNoModal Lib "user32" Alias "MessageBoxA" ( _
    5.             ByVal hwnd As Long, _
    6.             ByVal lpText As String, _
    7.             ByVal lpCaption As String, _
    8.             ByVal wType As Long) As Long
    9.  
    10. Const MB_ICONASTERISK = &H40&
    11.  
    12. '==============================================================================
    13.  
    14. Private Sub NoModalMsgboxExample()
    15.  
    16.     ' Form2 is a hidden form
    17.     Call MsgboxNoModal([b]Form2.hwnd[/b], "This is NO modal.", _
    18.                        "No Modal Msgbox Example", MB_ICONASTERISK)
    19.  
    20. End Sub
    21.  
    22. '==============================================================================
    23. Private Sub Form_Unload(Cancel As Integer)
    24.  
    25.     'This ensures that all forms get unloaded
    26.     UnloadAll
    27.  
    28. End Sub

    Inside a module :
    VB Code:
    1. '[b] Inside a Module[/b]
    2. Option Explicit
    3.  
    4. Public Sub UnloadAll()
    5.  
    6.     'Unloads all Form from Forms collection
    7.     Dim frm As Form
    8.  
    9.     For Each frm In Forms
    10.         Unload frm
    11.     Next
    12.  
    13. End Sub

    Hope it helps.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    39

    Re: Problem closing msgboxes

    Thanks iPrank, I have added the code you suggested me and now the program runs ok. Thank you very much for your help!!!

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Problem closing msgboxes

    You are welcome, my friend
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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