我想检测另一个进程是否说process.exe当前正在显示一个对话框? 有没有办法在C#中做到这一点?
看看我是否可以获得对话框的句柄。我尝试过Spy ++的查找窗口工具,当我尝试在对话框顶部拖动取景器时,它不会突出显示对话框,而是填充详细信息和 提到AppCustomDialogBox并提到句柄号
请告知我如何以编程方式检测到..
谢谢,
答案 0 :(得分:4)
当应用程序显示一个对话框时,Windows操作系统的行为(对我来说悄悄讨厌)是将新创建的窗口显示在所有其他窗口之上。因此,如果我假设您知道要监视哪个进程,则检测新窗口的方法是设置Windows挂钩:
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 (f.e check if that is the needed window)
}
//When you Close Your application, remove the hook:
UnhookWinEvent(hhook);
我没有明确地为对话框尝试该代码,但对于单独的进程,它运行良好。请记住,该代码无法在Windows服务或控制台应用程序中运行,因为它需要message pump(Windows应用程序具有此功能)。你必须创建一个自己的。
希望这有帮助
答案 1 :(得分:2)
由于模态对话框通常禁用父窗口,因此您可以枚举进程的所有顶级窗口,并查看是否使用IsWindowEnabled()
函数启用它们。