|
-
Dec 16th, 2005, 07:12 AM
#1
Thread Starter
Member
[RESOLVED] Problem closing msgboxes
Hi, I am developing a visual basic application. I have implemented no modal msgbox as following:
VB Code:
Option Explicit
Private Declare Function MsgboxNoModal Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal
lpCaption As String, ByVal wType As Long) As Long
Const MB_ICONASTERISK = &H40&
Private Sub NoModalMsgboxExample()
Call MsgboxNoModal(100, "This is NO modal.", "No Modal Msgbox Example", MB_ICONASTERISK)
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:
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 Any) As Long
Private Const WM_CLOSE = &H10
Private Sub Close_application()
Dim hWnd As Long
hWnd = FindWindow("#32770", "No Modal Msgbox Example")
SendMessage hWnd, WM_CLOSE, 0, 0
End
End Sub
I am a beginner using API, so I would appreciate any help. Thank you very much for your help!!!
-
Dec 16th, 2005, 06:55 PM
#2
Lively Member
Re: Problem closing msgboxes
i don't have VB open, but try doing somthing like this:
VB Code:
Private Sub Close_application()
Dim hWnd&
Do:Doevents
hWnd& = FindWindow("#32770", "No Modal Msgbox Example")
X& = SendMessage (hWnd&, WM_CLOSE, 0, 0)
Loop until hwnd& = 0
End
End Sub
Last edited by ghettohacker; Dec 16th, 2005 at 07:13 PM.
Reason: i'm still learning
-
Dec 19th, 2005, 03:27 AM
#3
Thread Starter
Member
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).
-
Dec 19th, 2005, 11:47 AM
#4
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:
'[b] Inside Main form[/b]
Option Explicit
Private Declare Function MsgboxNoModal Lib "user32" Alias "MessageBoxA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Long) As Long
Const MB_ICONASTERISK = &H40&
'==============================================================================
Private Sub NoModalMsgboxExample()
' Form2 is a hidden form
Call MsgboxNoModal([b]Form2.hwnd[/b], "This is NO modal.", _
"No Modal Msgbox Example", MB_ICONASTERISK)
End Sub
'==============================================================================
Private Sub Form_Unload(Cancel As Integer)
'This ensures that all forms get unloaded
UnloadAll
End Sub
Inside a module :
VB Code:
'[b] Inside a Module[/b]
Option Explicit
Public Sub UnloadAll()
'Unloads all Form from Forms collection
Dim frm As Form
For Each frm In Forms
Unload frm
Next
End Sub
Hope it helps.
-
Dec 20th, 2005, 04:33 AM
#5
Thread Starter
Member
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!!!
-
Dec 20th, 2005, 06:02 AM
#6
Re: [RESOLVED] Problem closing msgboxes
You are welcome, my friend
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|