First of all, I did not discover this thing. I do not remember where I've got it from, sorry. I am putting it here just to help others who need it.
For this trick, you need the following external function:
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
Now, in the form that you want unfocusable (but still usable), add the following override:
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.Style = 0x40000000 | 0x4000000;
cp.ExStyle &= 0x00080000;
cp.Parent = GetDesktopWindow();
return cp;
}
}
Show this form with only with the overloaded
Show(IWin32Window owner)
method. The "owner" window will always keep the unfocusable form over it and never lose focus when the unfocusable form should be focused.
Moving focus to the unfocusable form from a window other than its owner will draw the focus away.
You can add any controls you like on this form and still sleep well at night; they will work perfectly, but will just not get focus!
Style
and
ExStyle
values taken from
WinUser.h of Microsoft Windows SDK.