Reputation: 11788
I have the following problem: I want to be able to tell if my application window is the foreground window in Windows. I am using C#, .Net Framework 3.5 on Windows XP.
I actually can think of two ways to do what I am after
GetForeGroundWindow
and compare the returned hWnd to the hWnd of my formForm.ActiveForm
is null
or an object referenceMethod 1 seems OK, but I would rather not use pinvoke unless I have to. I am not entirely sure about method 2 although it seems to work OK.
Which method should I use, is there any other way?
Upvotes: 1
Views: 3757
Reputation: 1509
It appear ActiveForm is application specific.
If you want to know if/when your form is the active form for the entire OS, you are stuck with API and a hWnd compare.
[System.Runtime.InteropServices.DllImport( "user32.dll" )]
public static extern IntPtr GetForegroundWindow();
Upvotes: 3