WinForms dialogs with TopMost = true

蓝咒 提交于 2020-01-15 03:22:06

问题


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:

  1. Show it at startup, then immediately:
  2. Hide it with ShowWindow( SW_HIDE )
  3. Never ever close the form, just let it be invisible
  4. Show it with ShowWindow( SW_SHOWNOACTIVATE )


来源:https://stackoverflow.com/questions/1727789/winforms-dialogs-with-topmost-true

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!