不能始终如一地把形式带到前面

Iva*_*čin 6 c# forms winforms

我尝试了几件事,但没有一件能起作用......

我点击NotifyIcon时应该在所有Windows前面的Form.所以这是我尝试过的:

private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.TopMost = true;
        this.BringToFront();
        this.Focus();
        this.TopMost = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用SetForegroundWindow:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
Run Code Online (Sandbox Code Playgroud)

通过增加

        SetForegroundWindow(this.Handle);
Run Code Online (Sandbox Code Playgroud)

在if块的末尾.

最后,我看到如果我在NotifyIcon上单击鼠标右键并且上下文菜单打开时不起作用,我可以左键单击NotifyIcon并将其带到前面.

我试图在开头添加此代码:

        cmsNotifyIcon.Show();
        cmsNotifyIcon.Close();
Run Code Online (Sandbox Code Playgroud)

因此它显示和关闭notifyIcon上下文菜单,作为解决方法的可能想法,但它没有帮助.

关于如何做到这一点的任何想法,或解决这个问题?

Cat*_*ICU 5

如果你在MouseUp上做了怎么办?