问题
I have a dialog implemented in WinForms that is shown as a notify dialog on the bottom right of the screen. The problem is that whenever is shown it takes the focus and this happens only when TopMost = true. How can I solve this?
回答1:
You need to inherit from Form and override a couple of properties:
[Flags]
enum WS_EX
{
TOPMOST = 0x00000008,
}
class TopMostForm : Form
{
protected override CreateParams CreateParams
{
get
{
var baseParams = base.CreateParams;
baseParams.ExStyle |= (int)WS_EX.TOPMOST;
return baseParams;
}
}
protected override bool ShowWithoutActivation
{
get { return true; }
}
}
Then just simply Show() on this form and it will be displayed as topmost and inactive.
回答2:
Show the dialog with Show instead of ShowDialog. ShowDialog will be topmost, user has to click it before doing something else (modal) Show will show it as normal.
回答3:
How about this strategy:
- Show it at startup, then immediately:
- Hide it with ShowWindow( SW_HIDE )
- Never ever close the form, just let it be invisible
- Show it with ShowWindow( SW_SHOWNOACTIVATE )
来源:https://stackoverflow.com/questions/1727789/winforms-dialogs-with-topmost-true