Detecting a modal dialog box of another process

I want to determine if another process.exe process is currently displaying a dialog box? Is there any way to do this in C #?

To find out if I can get a handle to a dialog box. I tried the Spy ++ find search tool, when I try to drag the crawler over the dialog box, it does not highlight the dialog box, but fills in the details and mentions AppCustomDialogBox and mentions the handle number

Please advise how I can programmatically detect this.

Thanks,

+5
source share
2 answers

As modal dialogs usually disable parent windows, you can list all the top-level windows for the process and see if they are enabled using the IsWindowEnabled() function.

+2
source

When the application displays a dialog box, (for me, quiet annoyance) the behavior of the Windows operating system is to display the newly created window on top of all the others. Therefore, if I assume that you know which process to look at, the way to detect a new window is to configure the Windows hook:

  delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); [DllImport("user32.dll")] public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); [DllImport("user32.dll")] public static extern bool UnhookWinEvent(IntPtr hWinEventHook); // Constants from winuser.h public const uint EVENT_SYSTEM_FOREGROUND = 3; public const uint WINEVENT_OUTOFCONTEXT = 0; //The GetForegroundWindow function returns a handle to the foreground window. [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); // For example, in Main() function // Listen for foreground window changes across all processes/threads on current desktop IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, new WinEventDelegate(WinEventProc), 0, 0, WINEVENT_OUTOFCONTEXT); void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { IntPtr foregroundWinHandle = GetForegroundWindow(); //Do something (fe check if that is the needed window) } //When you Close Your application, remove the hook: UnhookWinEvent(hhook); 

I have not tried this code explicitly for dialog boxes, but for individual processes it works well. Please remember that this code cannot work in a Windows service or in a console application, since it requires a message pump (these are Windows applications). You will need to create your own.

Hope this helps

+2
source

Source: https://habr.com/ru/post/985050/


All Articles