Detect a Modal Dialog Box

J

Jeremy

Hello,

I'm thinking this may be an easy one but I am getting frustrated. How
can I detect if a process is showing a modal dialog. Like "Are you sure
you want to exit?", or an error message etc...

I stumbled across the GetWindow API but am unsure how to work it
properly. The only examples I found are in VBA or VB6.

Private Declare Function GetWindow _
Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Int32) As IntPtr

Private Const GW_FIRST As Int32 = 0
Private Const GW_LAST As Int32 = 1
Private Const GW_NEXT As Int32 = 2
Private Const GW_PREV As Int32 = 3
Private Const GW_OWNER As Int32 = 4
Private Const GW_CHILD As Int32 = 5
Private Const GW_STYLE As Int32 = (-16)
Private Const WS_DISABLED As Int32 = &H8000000

Am I on the right track here or is the above even correct...can someone
give me a kickstart? Or is there an even simpler method to detect a
modal dialog? Thanks for your help!

Jeremy
 
J

Jeremy

Alight I found the correct API call:

Private Declare Function apiGetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal Hwnd As IntPtr, ByVal _
nIndex As Long) As IntPtr

and I found a function to return true if a modal window is detected.
The problem is it is written for detecting a window in your own
application. I would like to detect a modal window in a exteranl app in
which I return the process.

Function IsFormModal(ByVal frm As Form) As Boolean
Dim f As Form

For Each f In Forms
If Not (f Is frm) Then
If (apiGetWindowLong(f.hWnd, GW_STYLE) And WS_DISABLED)
= 0 Then
' there is another enabled form, so this form
' can't be modal
Exit Function
End If
End If
Next
' all other forms are disabled, therefore this form is modal
IsFormModal = True
End Function

How can I tweak this to loop through windows in an exteranl process in
which I return the Process.MainWindowHandle? Thanks!

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top