Gdy aplikacja pokazuje okno dialogowe, The (dla mnie) spokojnie irytujące zachowanie systemu operacyjnego Windows jest pokazanie nowo utworzonego okna na wierzchu wszystkich innych. Więc jeśli założyć, że wiesz, który to sposób na oglądanie, sposób, aby wykryć nowe okno jest założyć hak 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);
I nie spróbować tego kodu jawnie dla okien dialogowych, ale na oddzielnych procesów jest działa dobrze. Pamiętaj, że ten kod nie działa w usłudze Windows lub aplikacji konsoli, ponieważ wymaga ona message pump (mają to aplikacje systemu Windows). Musisz stworzyć własny.
Nadzieja to pomaga
Dzięki temu, że zawęziłem obszar wyszukiwania, aplikacja rzeczywiście miała wiele ukrytych najwyższych poziomów okna, ale zadziałało to dla mnie z odrobiną poprawek, – Ahmed