Showing a form without activating

Went to the MVP summit this evening - it was great to see everyone. I got a question from Tim about how to show a form without activation using Windows Forms 2.0.

The first thing you should probably ask yourself is - can I host this in a ToolStripDropDown? If you want the window to go away when someone clicks somewhere else, this is probably the control for you. The ToolStripDropDown can host any control via the ToolStripControlHost, and you can inspect the reason it's closing in the ToolStripDropDown.Closing event.

If you want a full-on form, you can now override a property called ShowWithoutActivation. 

   public class NoActivateForm : Form {
protected override bool ShowWithoutActivation {
get {
return true;
}
}
}

Keep in mind this does not "prevent" activation all the time - you can still activate by calling the Activate(), Focus() ... etc methods. If you want to prevent clicks on the client area from activating the window, you can handle the WM_MOUSEACTIVATE message.

          private const int WM_MOUSEACTIVATE = 0x0021, MA_NOACTIVATE = 0x0003;

protected override void WndProc(ref Message m) {
if (m.Msg == WM_MOUSEACTIVATE) {
m.Result = (IntPtr)MA_NOACTIVATE;
return;
}
base.WndProc(ref m);
}