Click to See Complete Forum and Search --> : A tough one .... at least for me
ChrisJackson
Dec 7th, 1999, 04:47 AM
Hi.
How can I check to see if a Message Box is currently displayed in Windows from VB? For example, a Message Box that's displayed when you try to go to a non-existent website?
Thanks in advance.
Chris
Aaron Young
Dec 7th, 1999, 08:34 AM
You can use the FindWindow API to check for the Dialog Class of #32770
Some programs also use this class for the main form, such as the Connect Dialog, so to make sure it's a Messagebox you can check it's Style Flags for WS_POPUPWINDOW, eg.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const WS_POPUP = &H80000000
Private Const WS_SYSMENU = &H80000
Private Const WS_BORDER = &H800000
Private Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Private Const GWL_STYLE = (-16)
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim lHwnd As Long
Dim bDialog As Boolean
'Check For the Dialog Class
lHwnd = FindWindow("#32770", vbNullString)
If lHwnd Then
'Check to see if it's a Messagebox, (Includes the POPUPWINDOW Style)..
If (GetWindowLong(lHwnd, GWL_STYLE) And WS_POPUPWINDOW) = WS_POPUPWINDOW Then
bDialog = True
End If
End If
If bDialog Then
Caption = "Messagebox Displayed."
ElseIf Caption <> "Form1" Then
Caption = "Form1"
End If
End Sub
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net
ChrisJackson
Dec 10th, 1999, 04:30 AM
Aaron, I used the code you provided above and my form displayed "Messagebox Displayed." although there was no Message Box actually displayed.
I guess that the code is recognizing my Form as a Message Box, which is not going to help me. I need to be able to detect a Message Box such as:
Microsoft Internet Explorer
Internet Explorer cannot open the Internet site: http://www.jhsdlafjkasjofiowe.com
A connection with the server could not be established.
This is the kind of Message Box that I need my VB program to work around and it seems that it would not fall into the same Window class as a VB Form or a running application.
Any ideas or clarifications Aaron, or anyone else?
TIA.
Chris
[This message has been edited by ChrisJackson (edited 12-10-1999).]
[This message has been edited by ChrisJackson (edited 12-10-1999).]
Serge
Dec 10th, 1999, 09:00 PM
Not a problem. You can add a couple other APIs and modify the code a little that Aaron provided to get to know if the MessageBox is displayed:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Const WS_POPUP = &H80000000
Private Const WS_SYSMENU = &H80000
Private Const WS_BORDER = &H800000
Private Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Private Const GWL_STYLE = (-16)
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim lHwnd As Long
Dim lHwndStatic As Long
Dim strBuffer As String
Dim lngLen As Long
'Check For the Dialog Class
Do
lHwnd = FindWindowEx(0, lHwnd, "#32770", vbNullString)
If lHwnd Then
'Check to see if it's a Messagebox
If (GetWindowLong(lHwnd, GWL_STYLE) And WS_POPUPWINDOW) = WS_POPUPWINDOW Then
'Get the static class (a label) hWnd
Do
lHwndStatic = FindWindowEx(lHwnd, lHwndStatic, "Static", vbNullString)
If lHwndStatic Then
lngLen = GetWindowTextLength(lHwndStatic)
strBuffer = Space(lngLen)
If GetWindowText(lHwndStatic, strBuffer, Len(strBuffer)) Then
If InStr(strBuffer, "Internet Explorer cannot open") > 0 Then
MsgBox "You found the Message Box in Explorer", vbInformation, "Found IE"
End If
End If
End If
Loop Until lHwndStatic = 0
End If
End If
Loop Until lHwnd = 0
End Sub
Regards,
------------------
Serge
Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)
ChrisJackson
Dec 11th, 1999, 09:09 PM
Serge and Aaron,
Thanks for your replies, my problem is still not solved. Here's why.
You'll notice that in the code, the Timer event code checks for the "Internet Explorer" window. But when a Message Box appears on the Screen, the Timer event won't fire until OK is clicked on the Message Box.. I am trying to get my program to detect if this message box appears while it is running, send "Enter" or "Escape" to get rid of it, and then continue with execution.
In simpler terms, I want it to be able to detect the Message Box popping up, get rid of it, and continue to run.
Is this possible?
TIA.
Chris
------------------
CJ
Serge
Dec 11th, 1999, 10:11 PM
It is possible.
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) 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 WS_POPUP = &H80000000
Private Const WS_SYSMENU = &H80000
Private Const WS_BORDER = &H800000
Private Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Private Const GWL_STYLE = (-16)
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_SPACE = &H20
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim lHwnd As Long
Dim lHwndStatic As Long
Dim strBuffer As String
Dim lngLen As Long
Dim lngOK As Long
'Check For the Dialog Class
Do
lHwnd = FindWindowEx(0, lHwnd, "#32770", vbNullString)
If lHwnd Then
'Check to see if it's a Messagebox
If (GetWindowLong(lHwnd, GWL_STYLE) And WS_POPUPWINDOW) = WS_POPUPWINDOW Then
'Get the static class (a label) hWnd
Do
lHwndStatic = FindWindowEx(lHwnd, lHwndStatic, "Static", vbNullString)
If lHwndStatic Then
lngLen = GetWindowTextLength(lHwndStatic)
strBuffer = Space(lngLen)
If GetWindowText(lHwndStatic, strBuffer, Len(strBuffer)) Then
If InStr(strBuffer, "Internet Explorer cannot open") > 0 Then
lngOK = FindWindowEx(lHwnd, 0, "Button", vbNullString)
Call SendMessage(lngOK, WM_KEYDOWN, VK_SPACE, 0)
Call SendMessage(lngOK, WM_KEYUP, VK_SPACE, 0)
End If
End If
End If
Loop Until lHwndStatic = 0
End If
End If
Loop Until lHwnd = 0
End Sub
I tested this and it works just fine.
------------------
Serge
Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)
ChrisJackson
Dec 13th, 1999, 05:00 AM
Serge, I used the code you provided, but it still didn't work. Please let me know how you tested the code so that I can make sure that I am doing the same thing. In case I'm doing something wrong, my code is listed below so that you can test it.
I'm not doubting whether the code can find the Message Box, but, based on my testing the code, the Timer Event doesn't occur when a Message Box is displayed.
Please correct me if I'm wrong.....I hope I am because I need to be able to work around the Message Box.
Thanks.
Chris
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) 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 WS_POPUP = &H80000000
Private Const WS_SYSMENU = &H80000
Private Const WS_BORDER = &H800000
Private Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Private Const GWL_STYLE = (-16)
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_SPACE = &H20
Private Sub Command1_Click()
WebBrowser1.Navigate2 "www.kejrdkajsfkle.com"
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim lHwnd As Long
Dim lHwndStatic As Long
Dim strBuffer As String
Dim lngLen As Long
Dim lngOK As Long
'Check For the Dialog Class
Do
lHwnd = FindWindowEx(0, lHwnd, "#32770", vbNullString)
If lHwnd Then
'Check to see if it's a Messagebox
If (GetWindowLong(lHwnd, GWL_STYLE) And WS_POPUPWINDOW) = WS_POPUPWINDOW Then
'Get the static class (a label) hWnd
Do
lHwndStatic = FindWindowEx(lHwnd, lHwndStatic, "Static", vbNullString)
If lHwndStatic Then
lngLen = GetWindowTextLength(lHwndStatic)
strBuffer = Space(lngLen)
If GetWindowText(lHwndStatic, strBuffer, Len(strBuffer)) Then
If InStr(strBuffer, "Internet Explorer cannot open") > 0 Then
lngOK = FindWindowEx(lHwnd, 0, "Button", vbNullString)
Call SendMessage(lngOK, WM_KEYDOWN, VK_SPACE, 0)
Call SendMessage(lngOK, WM_KEYUP, VK_SPACE, 0)
End If
End If
End If
Loop Until lHwndStatic = 0
End If
End If
Loop Until lHwnd = 0
End Sub
[Edited by ChrisJackson on 09-22-2000 at 09:51 PM]
ChrisJackson
Dec 13th, 1999, 08:34 PM
Serge, you were right, THE CODE DOES WORK! It didn't work when I ran it yesterday as a VB project, but it worked like a charm when I made it into an executable (EXE) program this morning. I guess this was the step that you were doing that I wasn't.
Thanks for all of your help and your patience. I really appreciate it.
Chris Jackson
------------------
CJ
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.