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

血红的双手。 提交于 2021-01-28 03:31:49

问题


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)


回答1:


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().




回答2:


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();
    }

  }


来源:https://stackoverflow.com/questions/2575788/winforms-why-does-a-show-after-a-system-tray-double-click-end-up-in-my-app

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