如何启动最小化到托盘的 WinForm 应用程序? [英] How to start WinForm app minimized to tray?

查看:26
本文介绍了如何启动最小化到托盘的 WinForm 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功创建了一个使用 NotifyIcon 最小化到托盘的应用程序.当表单被手动关闭时,它会成功地从桌面、任务栏和 alt 选项卡中隐藏.尝试以最小化的应用程序启动时会出现问题.起初的问题是该应用程序将被最小化,但仍会出现在 alt-tab 对话框中.将 FormBorderStyle 更改为 ToolWindow 选项之一(来自无"选项)解决了这个问题,但引入了另一个问题.当应用首次启动时,最小化窗口的标题栏在开始菜单的正上方可见:

I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:

打开窗体并关闭它会使其正确隐藏.我已经尝试了很多变体,但现在基本上是这样工作的......

Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...

WindowState 在设计器中设置为最小化.在构造函数中进行一些初始化后,我有以下几行:

WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:

this.Visible = false;
this.ShowInTaskbar = false;

当 NotifyIcon 被双击时,我有以下内容:

When the NotifyIcon is double-clicked I have the following:

 this.WindowState = FormWindowState.Normal;
 this.Visible = true;
 this.ShowInTaskbar = true;

就像我说的,我已经尝试了很多小的变化(this.Hide() 等).有没有办法让 NotifyIcon 成为主要组件,这样我就可以在让 NotifyIcon 运行的同时完全启动和处理表单?必须有一种方法可以在没有任何奇怪的情况下最小化表单的情况下启动应用程序.请帮我找到它!

Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!

推荐答案

正确的做法是首先防止表单变得可见.这需要覆盖 SetVisibleCore().让我们假设一个带有 Show 和 Exit 命令的 NotifyIcon 上下文菜单.你可以这样实现:

The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        notifyIcon1.ContextMenuStrip = contextMenuStrip1;
        this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
        this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
    }

    private bool allowVisible;     // ContextMenu's Show command used
    private bool allowClose;       // ContextMenu's Exit command used

    protected override void SetVisibleCore(bool value) {
        if (!allowVisible) {
            value = false;
            if (!this.IsHandleCreated) CreateHandle();
        }
        base.SetVisibleCore(value);
    }

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (!allowClose) {
            this.Hide();
            e.Cancel = true;
        }
        base.OnFormClosing(e);
    }

    private void showToolStripMenuItem_Click(object sender, EventArgs e) {
        allowVisible = true;
        Show();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
        allowClose = true;
        Application.Exit();
    }
}

注意 Load 事件的一个问题,它在主窗体首次显示之前不会触发.所以一定要在表单的构造函数中进行初始化,而不是在 Load 事件处理程序中.

Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.

这篇关于如何启动最小化到托盘的 WinForm 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆