C#显示通知表,但不关注焦点

C#显示通知表,但不关注焦点,c#,windows,winforms,api,C#,Windows,Winforms,Api,因此,我们认为在聊天应用程序中添加一些Toast功能是一个好主意,它实际上工作得很好,但是,当窗体显示时,它会在短时间内窃取焦点,这会使聊天输入框(当您键入时)闪烁或清除所有文本(因为焦点从它那里被偷走) 我已经回顾了这个网站上的几个帖子,关于如何通过覆盖createparms甚至在不激活或类似内容的情况下进行显示来阻止它的发生,但它的工作并不完全正确 这就是我所拥有的(我为所有的评论道歉,我们的老板希望所有的事情都记录在案): 以及调用该类的示例: if (Settings.Default.P

因此,我们认为在聊天应用程序中添加一些Toast功能是一个好主意,它实际上工作得很好,但是,当窗体显示时,它会在短时间内窃取焦点,这会使聊天输入框(当您键入时)闪烁或清除所有文本(因为焦点从它那里被偷走)

我已经回顾了这个网站上的几个帖子,关于如何通过覆盖
createparms
甚至在不激活
或类似内容的情况下进行
显示来阻止它的发生,但它的工作并不完全正确

这就是我所拥有的(我为所有的评论道歉,我们的老板希望所有的事情都记录在案):

以及调用该类的示例:

if (Settings.Default.PopUpEnabledChat)
            {
                if (!(Settings.Default.NoAlerts))
                    ToastControl.ShowAlert(string.Format("{0}: {1}", user.Nick, description.Replace("\r",
                                                              "").Replace("\n",
                                                                          "").Replace("\0",
                                                                                      "")), channel, Font);
            }

如何才能在不从主应用程序窃取焦点的情况下显示此通知表单?

看起来解决方案与我所做的差不多,只是我需要在通知表单中添加以下内容:

/// <summary>
    /// Do not activate the window just show it
    /// </summary>
    protected override bool ShowWithoutActivation
    {
        get { return true; }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000008; //WS_EX_TOPMOST 
            return cp;
        }
    } 

现在,当显示通知时,它们不会窃取焦点,并且它们在其他窗口的顶部显示得很好:)

更多信息,您是否使用NotifyIcon?不,我们不使用NotifyIcon执行此操作,因为我们不希望系统托盘中有图标。第一个代码示例是ToastForm
,即弹出的实际通知窗口。这有点棘手,尝试一下->他们在那里使用的方法相当…粗糙..似乎不太…准确:
FormBorderStyle=FormBorderStyle.None;WindowState=FormWindowState.Minimized;base.Show();base.Hide();WindowState=FormWindowState.Normal;ShowInTaskbar=false;最顶端=真;MaximizeBox=假;b x=假;ControlBox=false;
这似乎是一个很大的错误。另一种方法更为混乱,谷歌“SW_shownoactive”:)hf,我希望它能帮助做好这项工作。我记得当我必须让它工作时,这是一个多大的PITA问题。+1在尝试实现非模式通知时遇到了同样的问题,这非常有用。阻碍我前进的主要因素是CreateParams标志,它在没有激活的情况下覆盖了Show,而是可见的。Show()。好问题,好答案。
if (Settings.Default.PopUpEnabledChat)
            {
                if (!(Settings.Default.NoAlerts))
                    ToastControl.ShowAlert(string.Format("{0}: {1}", user.Nick, description.Replace("\r",
                                                              "").Replace("\n",
                                                                          "").Replace("\0",
                                                                                      "")), channel, Font);
            }
/// <summary>
    /// Do not activate the window just show it
    /// </summary>
    protected override bool ShowWithoutActivation
    {
        get { return true; }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000008; //WS_EX_TOPMOST 
            return cp;
        }
    } 
 internal static void AlertBottomRight(string msg, string title, Font fnt)
    {
        SliceCount += 1;
        new ToastForm(5000,
                      title,
                      msg,
                      3) {
                             Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
                             Visible = true
                         };
    }

    internal static void AlertBottomLeft(string msg, string title, Font fnt)
    {
        SliceCount += 1;
        new ToastForm(5000,
                      title,
                      msg,
                      2) {
                             Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
                             Visible = true
                         };
    }

    internal static void AlertTopLeft(string msg, string title, Font fnt)
    {
        SliceCount += 1;
        new ToastForm(5000,
                      title,
                      msg,
                      0) {
                             Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
                             Visible = true
                         };
    }

    internal static void AlertTopRight(string msg, string title, Font fnt)
    {
        SliceCount += 1;
        new ToastForm(5000,
                      title,
                      msg,
                      1) {
                             Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
                             Visible = true
                         };
    }