I have a Windows Form application that, when the form is Activated, Deactivated, or SizeChanged, the form does something. It's rather specific. For example:
Form Activated: Will set the variable isActive
to true, and focus the input to an input box for someone to enter something too.
Form Deactivated: Will set the variable isActive
to false, so that any focus changes in the application (caused by remote machine messages, chat messages, etc), does not steal focus from other applications.
Right now, I have my webBrowser1.Focus()
command inside of the Form Activated
event which isn't ideal, as when you click on the taskbar icon, it tries to minimize, but since it focuses back to the web browser, the form is then restored/activated again.
I did some searching here on Stack Overflow, and found the following information:
Edited for the below information:
I did find this information in another post here on Stack Overflow:
protected void OnActivateApp(bool activate)
{
Console.WriteLine("Activate {0}", activate);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// Trap WM_ACTIVATEAPP
if (m.Msg == 0x1c) OnActivateApp(m.WParam != IntPtr.Zero);
base.WndProc(ref m);
}
But the behavior is similar to the problem I'm seeing with the above 2 events. When the taskbar icon is CLICKED, the form does catch the Deactivated:
Activate False
But then immediately, it does this:
Activate True
It would appear, that when you minimize a window with the taskbar button, it still remains as the 'focused' application until you click on another application.
One of the posts did suggest I capture the GotFocus
of the form, but there is no event that I can find for a form level for GotFocus
, as GotFocus
is derived from a control, not an application form.
Question: How do I allow the form to be minimized with the taskbar button, and when the form is then reshown, get it to do the webBrowser1.Focus()
command to put the focus where it should be?