0

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

  1. 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()

  1. When the user minimizes the application and restors it - works fine

  2. 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.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
soms
  • 3
  • 2
  • without checking isnt there an application minimized/restored type event - or was that just delphi – BugFinder Aug 25 '17 at 10:44
  • I didnt check any application minimized event . – soms Aug 25 '17 at 13:44
  • I tried to log a trace when the control is in the Form_Activated event.And I did the following : clicked several times on the application icon in the task bar.If the application is not in the top of other windows then this click will bring the application to front.A click again will hide the application behind. Surprisingly when the window is visible in front, the form_activated is not hit and when the application is not visible (the next click on the task bar icon) fires the Form_Activated event and i can see the log. – soms Aug 25 '17 at 13:53
  • Hope this will help you. https://stackoverflow.com/questions/11366600/how-do-you-tell-if-a-windows-form-is-open-but-behind-another-window – AlexL Aug 28 '17 at 18:21
  • Thanks Alex.But I still have the problem.I am bringing the application to front and hiding the application by continuously clicking the application button in the task bar.The first click on the button from the task bar, when the application is not in front, is able to bring the application to front but the OnActivated is not fired. The next click on the task bar button, will hide the application and bring the next top most window to top level. That is also happening but then also the activated is fired which I dont except. – soms Aug 30 '17 at 14:25

1 Answers1

0

The problem was that repeated click on the task bar button will fire the activated event when the click hides the application and will not fire the event when the click shows the application.So OnResize () also maintained.

 protected override void OnActivated(EventArgs e)
    {
       IntPtr activeWindow = Engine.GetForegroundWindow();
                    if ((this).Handle == activeWindow)
                    {
                        MainFormActive?.Invoke(this, EventArgs.Empty);

                    }
    }

Resize

  protected override void OnResize (EventArgs e)

{

 if (previousState == FormWindowState.Minimized && this.WindowState == FormWindowState.Normal)

// raise OnActivated 

}

Also I saw the following handy - handling WM_NCACTIVATE in the WndProc

 if (m.Msg == Engine.WM_NCACTIVATE)
{
     if ((this).Handle == Engine.GetForegroundWindow())                
      {
          // Do what ever                
      }
}

WM_NCACTIVATE = 0x0086

Taskbar Minimize C# Windows Form Problem

Is there Windows system event on active window changed?

soms
  • 3
  • 2