|
-
Dec 6th, 2000, 06:35 AM
#1
Thread Starter
Addicted Member
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]
-
Dec 6th, 2000, 10:23 AM
#2
Fanatic Member
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]
-
Dec 6th, 2000, 11:46 AM
#3
Frenzied Member
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.
-
Dec 6th, 2000, 03:09 PM
#4
Junior Member
Jop, What is "AddressOf CloseBox" argument?
Thanks!
-
Dec 6th, 2000, 03:16 PM
#5
Frenzied Member
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.
-
Dec 7th, 2000, 08:54 AM
#6
Junior Member
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!
-
Dec 7th, 2000, 09:04 AM
#7
Fanatic Member
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]
-
Dec 7th, 2000, 09:49 AM
#8
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|