My application when not in focus or minimized or behind some other windows need to show a notification window to bring attention. And need to dismiss the notification window when the user brings the application upfront.
I am able to determine whether the main form(i.e my application) is active by the combination of
FormWindowState.Minimized and this.Handle != activeWindow
Now if the user brings the application upfront by himself, I have to remove the notification window.
To determine that i am using the Form_Activated inside the main form of the application and inside the Form_Activated, I am trying to find the main form of the window is active.
I tried the following but not working in all the scenarios in which the user can bring the application upfront.
subscribe the Form.ActiveForm and in side that determine it is the main form - Form.ActiveForm is MainForm
When the user minimizes the application and restors it - works fine
2.When the user uses Alt+Tab to bring that application upfront - works fine
3.When the user clicks the application icon up from the task bar - not working
Use the IntPtr GetForegroundWindow()
When the user minimizes the application and restors it - works fine
When the user uses Alt+Tab to bring that application upfront - not working as the handle is not retrieved
3.When the user clicks the application icon up from the task bar - not working as the handle is not retrieved
Using the this.Focused - Is false in the above cases
using IntPtr GetTopWindow(IntPtr hwnd) like GetTopWindow((sender as MainForm).Handle).ToString() != "0
".this is true even if the notification window is shown which is not I want.
If I just subscribe the Form_Activated , when the notification window is shown, then also the mainForm_Activated is fired.Hence I need to do some additional checks here and all the ways I tried above, did not turn out success for all the ways in which the user can bring the application to foreground.
Any help would be greatly appreciated.