Winforms - 为什么“Show()”系统托盘双击后,我的应用程序会最小化吗?

发布于 2024-08-27 07:16:02 字数 100 浏览 4 评论 0原文

Winforms - 为什么系统托盘双击后“Show()”会在我的应用程序中最小化?

如何确保在通知图标双击事件中我隐藏的主窗体恢复正常可见,而不是最小化(也不是最大化)

Winforms - why does a "Show()" after a system tray double click end up in my app minimized?

How do I ensure Inthe notifyicon double click event that my hidden main form comes back visible as normal, not minimized (nor maximised for that matter too)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-09-03 07:16:02

我猜您将应用程序放在最小化操作的托盘中。在这种情况下,Show 只是恢复可见性。

尝试在 Show() 之前添加 form.WindowState = Normal

I would guess that you put your application in tray on minimize action. In that case, Show just restores visibility.

Try adding form.WindowState = Normal before Show().

请叫√我孤独 2024-09-03 07:16:02

使用 NotifyIcon 隐藏表单通常是可取的,这样您的应用程序就会立即在托盘中启动。您可以通过重写 SetVisibleCore() 方法来防止它可见。您通常还希望防止它在用户单击 X 按钮时关闭,重写 OnFormClosing 方法以隐藏表单。您需要一个上下文菜单来允许用户真正退出您的应用程序。

将 NotifyIcon 和 ContextMenuStrip 添加到您的表单中。向 CMS 提供“显示”和“退出”菜单命令。使表单代码如下所示:

  public partial class Form1 : Form {
    bool mAllowClose;
    public Form1() {
      InitializeComponent();
      notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
      notifyIcon1.ContextMenuStrip = contextMenuStrip1;
      showToolStripMenuItem.Click += notifyIcon1_DoubleClick;
      exitToolStripMenuItem.Click += (o, e) => { mAllowClose = true; Close(); };
    }

    protected override void SetVisibleCore(bool value) {
      // Prevent form getting visible when started
      // Beware that the Load event won't run until it becomes visible
      if (!this.IsHandleCreated) {
        this.CreateHandle();
        value = false;
      }
      base.SetVisibleCore(value);
    }

    protected override void OnFormClosing(FormClosingEventArgs e) {
      if (!this.mAllowClose) {    // Just hide, unless the user used the ContextMenuStrip
        e.Cancel = true;
        this.Hide();
      }
    }

    void notifyIcon1_DoubleClick(object sender, EventArgs e) {
      this.WindowState = FormWindowState.Normal;  // Just in case...
      this.Show();
    }

  }

Hiding your form with the NotifyIcon is often desirable so your app starts in the tray right away. You can prevent it from getting visible by overriding the SetVisibleCore() method. You also typically want to prevent it from closing when the user clicks the X button, override the OnFormClosing method to hide the form. You'll want a context menu to allow the user to really quit your app.

Add a NotifyIcon and a ContextMenuStrip to your form. Give the CMS the Show and Exit menu commands. Make the form code look like this:

  public partial class Form1 : Form {
    bool mAllowClose;
    public Form1() {
      InitializeComponent();
      notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
      notifyIcon1.ContextMenuStrip = contextMenuStrip1;
      showToolStripMenuItem.Click += notifyIcon1_DoubleClick;
      exitToolStripMenuItem.Click += (o, e) => { mAllowClose = true; Close(); };
    }

    protected override void SetVisibleCore(bool value) {
      // Prevent form getting visible when started
      // Beware that the Load event won't run until it becomes visible
      if (!this.IsHandleCreated) {
        this.CreateHandle();
        value = false;
      }
      base.SetVisibleCore(value);
    }

    protected override void OnFormClosing(FormClosingEventArgs e) {
      if (!this.mAllowClose) {    // Just hide, unless the user used the ContextMenuStrip
        e.Cancel = true;
        this.Hide();
      }
    }

    void notifyIcon1_DoubleClick(object sender, EventArgs e) {
      this.WindowState = FormWindowState.Normal;  // Just in case...
      this.Show();
    }

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文