2012-07-01 28 views
5

我想檢測另一個進程是否說process.exe當前正在顯示一個對話框? 有沒有辦法在C#中做到這一點?檢測另一個進程的模態對話框

看看我是否可以得到對話框的句柄。我試過Spy ++的查找窗口工具,當我試圖拖動查找器在對話框的頂部時,它沒有突出顯示對話框,但是填充了細節,並且提到了AppCustomDialogBox並提到了處理號碼

請告知如何可我編程檢測..

感謝,

回答

2

由於模態對話框通常禁用父窗口(S),你可以枚舉一個進程的所有頂級窗口,看看他們是否正在使用IsWindowEnabled()功能啓用。

+0

由於縮小我的搜索空間非常有用,應用程序實際上有很多隱藏的頂級窗戶,但它爲我做了一些調整, – Ahmed

2

當應用程序顯示一個對話框,將(對我悄悄煩人)的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應用程序具有該功能)。你必須創建一個自己的。

希望這有助於

+0

我的應用程序的生命並沒有在對話框出現之前開始,所以連接新的對話框將無濟於事。 – Ahmed

相關問題