如何开始的WinForm程序最小化到系统托盘? [英] How to start WinForm app minimized to tray?

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

问题描述

我已经成功地创建了一个应用程序,最大程度地减少使用的NotifyIcon托盘。当表单被手动关闭它成功地隐藏在桌面,任务栏,使用Alt-Tab。当试图启动与最小化的应用程序出现问题。起初,问题是,应用程序将被最小化,但仍然会出现在使用Alt-Tab对话框。改变FormBorderStyle到的工具窗口选项之一(从无选项)固定这一点,但是引入了另一个问题。当应用程序第一次启动时最小化窗口的标题栏是可见的正上方开始菜单:

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!

推荐答案

正确的方法做,这是prevent从得到明显的摆在首位的形式。这需要压倒一切的SetVisibleCore()。让我们承担了展会,并退出命令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天全站免登陆