Prevent a form from getting focus
I am making an application that uses multiple forms at the same time. I have one form that is a "background" form for all the others. The problem is that when I click it, the form comes to the front. I want to keep it behind my other forms, but still in front of other windows that may be open (other running applications). How do I do this?

Bechir BejaouiRank #144 · 15.9K reputationPosted Mar 21, 2008, 1:44 PM EDT
yourForm.TopMost = true;
Scott13 reputationPosted Mar 24, 2008, 6:04 AM EDT
AlanRank #669 · 3.6K reputationPosted Mar 24, 2008, 9:17 AM EDT
To my knowledge, there's no such event in the .NET framework.
However, you can override WndProc() in the form class you'd like to keep on top (or not) to watch for the WM_ACTIVATEAPP message and then handle it accordingly. Something like this:
private const int WM_ACTIVATEAPP = 0x1C;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ACTIVATEAPP)
{
if ( m.WParam.ToInt32() != 0 )
{
// application is receiving focus
this.Topmost = true;
}
else
{
// application is losing focus
this.Topmost = false;
}
}
base.WndProc(ref m);
}
Scott13 reputationPosted Mar 24, 2008, 9:35 AM EDT
AlanRank #669 · 3.6K reputationPosted Mar 24, 2008, 11:06 AM EDT
The code is for the activation of the entire application.
For individual forms you can handle the .NET Form.Activated or Form.Deactivate events.