I'm making a C# WPF application, and every time the users clicks somewhere outside of my application, I want to display an alert with the TITLE of the window clicked. To detect clicks outside my application, I'm using LowLevelMouseProc
. But how would I get the window that has focus in C#?
Thanks in advance!
EDIT: I know this is not related to my original question, but how would I get the control focused, as well?
GetForegroundWindow should do it.
Regarding your follow-up question on how to get the control with focus, you can get the HWND of the focused control using GetGUIThreadInfo. Note that you pass this the thread that owns the foreign window, not the foreign window itself (the function docs tell you how to to get the thread ID from the foreign window handle). The focused HWND is returned through the GUITHREADINFO.hwndFocus
member.
Note that in general, you can't do better than a HWND, because the foreground window is not necessarily a .NET application. For example, if the foreign window is a WPF window, this will not tell you which WPF control within that window has focus, because WPF controls don't have HWNDs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With