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?
AlanPosted Mar 24, 2008, 3:06 PM
The code is for the activation of the entire application.
For individual forms you can handle the .NET Form.Activated or Form.Deactivate events.
ScottPosted Mar 24, 2008, 1:35 PM
AlanPosted Mar 24, 2008, 1:17 PM
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);
}
ScottPosted Mar 24, 2008, 10:04 AM
Bechir BejaouiPosted Mar 21, 2008, 5:44 PM
yourForm.TopMost = true;