最小化托盘 C# WinForms 应用程序的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:58:24  来源:igfitidea点击:

What's the proper way to minimize to tray a C# WinForms app?

提问by Judah Gabriel Himango

What is the proper way to minimize a WinForms app to the system tray?

将 WinForms 应用程序最小化到系统托盘的正确方法是什么?

Note: minimize to system tray; on the right side of the taskbar by the clock. I'm not asking about minimizing to taskbar, which is what happens when you hit the "minus" button on the window.

注意:最小化到系统托盘;在时钟的任务栏右侧。我不是在问最小化到任务栏,这是当您点击窗口上的“减号”按钮时会发生的情况。

I've seen hackish solutions like, "minimize, set ShowInTaskbar = false, then show your NotifyIcon."

我见过一些骇人听闻的解决方案,例如“最小化,设置 ShowInTaskbar = false,然后显示您的 NotifyIcon”。

Solutions like that are hackish because the app doesn't appear to minimize to the tray like other apps, the code has to detect when to set ShowInTaskbar = true, among other issues.

像这样的解决方案是骇人听闻的,因为该应用程序似乎不像其他应用程序那样最小化到托盘,代码必须检测何时设置 ShowInTaskbar = true,以及其他问题。

What's the proper way to do this?

这样做的正确方法是什么?

采纳答案by FlySwat

There is actually no managed way to do that form of animation to the tray in native winforms, however you can P/Invoke shell32.dll to do it:

实际上没有管理的方法可以在本机 winforms 中对托盘执行这种形式的动画,但是您可以 P/Invoke shell32.dll 来做到这一点:

Some good info here (In the comments not the post):

这里有一些很好的信息(在评论而不是帖子中):

http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx

http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx

And here it is in C++:

这是在 C++ 中的:

http://www.codeproject.com/KB/shell/minimizetotray.aspx

http://www.codeproject.com/KB/shell/minimizetotray.aspx

You can use that to figure out what stuff to Pinvoke for your C# version.

您可以使用它来确定要为您的 C# 版本 Pinvoke 的内容。

回答by Ryan Farley

this.WindowState = FormWindowState.Minimized  

That is the built in way to do it and it looks fine to me most of the time. The only time is has some weirdness to it is if you call it on startup which has some weirdness sometimes which is why most people will also set the ShowInTaskbar = false and hide the form too.

这是内置的方法,大多数时候对我来说都很好。唯一有一些奇怪的是,如果您在启动时调用它,有时会有些奇怪,这就是为什么大多数人也会设置 ShowInTaskbar = false 并隐藏表单的原因。

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx

回答by Gulzar Nazim

Update: Looks like I posted too soon. I was also using the below hack for a tool of mine. Waiting for the right solution for this..........

更新:看起来我发布得太早了。我也在使用下面的 hack 作为我的工具。等待正确的解决方案......

You can use Windows.Forms.NotifyIcon for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

您可以为此使用 Windows.Forms.NotifyIcon。http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

Add NotifyIcon to the form, set some properties and you are done.

将 NotifyIcon 添加到表单中,设置一些属性就完成了。

        this.ShowIcon = false;//for the main form
        this.ShowInTaskbar = false;//for the main form
        this.notifyIcon1.Visible = true;//for notify icon
        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));//set an icon for notifyicon

    private void Form1_Shown(object sender, EventArgs e)
    {
        this.Hide();
    }

回答by FlySwat

This code is tested and supports many options.

此代码经过测试并支持许多选项。

More here: http://code.msdn.microsoft.com/TheNotifyIconExample

更多信息:http: //code.msdn.microsoft.com/TheNotifyIconExample

回答by FlySwat

In the constructor of the Form:

在 Form 的构造函数中:

this.Resize += new EventHandler(MainForm_Minimize);

Then use this Event Handler method:

然后使用此事件处理程序方法:

    private void MainForm_Minimize(object sender, EventArgs e)
    {
        if(this.WindowState == FormWindowState.Minimized)
            Hide();
    }

回答by FlySwat

Similar as above...

和上面类似...

I have a resize event handler that is triggered whenever the window gets resized (Maximized, minimized, etc.)...

我有一个 resize 事件处理程序,每当窗口调整大小(最大化、最小化等)时都会触发它...

    public form1()
    {
       Initialize Component();

       this.Resize += new EventHanlder(form1_Resize);
    } 


    private void form1_Resize(object sender, EventArgs e)
    {
       if (this.WindowState == FormWindowState.Minimized && minimizeToTrayToolStripMenuItem.Checked == true)
       {
             NotificationIcon1.Visible = true;
             NotificationIcon1.BalloonTipText = "Tool Tip Text"
             NotificationIcon1.ShowBalloonTip(2);  //show balloon tip for 2 seconds
             NotificationIcon1.Text = "Balloon Text that shows when minimized to tray for 2 seconds";
             this.WindowState = FormWindowState.Minimized;
             this.ShowInTaskbar = false;
       }
    }

This allows the user to select whether or not they want to minimize to tray via a menubar. They can go to Windows -> and click on Minimize to Tray. If this is checked, and the window is being resized to Minimized, then it will minimize to the tray. Works flawlessly for me.

这允许用户通过菜单栏选择是否要最小化到托盘。他们可以转到 Windows -> 并单击 Minimize to Tray。如果选中此项,并且窗口被调整为最小化,那么它将最小化到托盘。对我来说完美无缺。

回答by James

If you are having problems getting this to work, check that you have

如果您在使用它时遇到问题,请检查您是否有

this.Resize += new System.EventHandler(this.Form1_Resize);

in the fom1.designer.cs

在fom1.designer.cs

回答by Jeyavel

It will helps:

它将有助于:

public partial class Form1 : Form
{
    public static bool Close = false;
    Icon[] images;
    int offset = 0;

    public Form1()
    {
        InitializeComponent();

        notifyIcon1.BalloonTipText = "My application still working...";
        notifyIcon1.BalloonTipTitle = "My Sample Application";
        notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; 
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (FormWindowState.Minimized == WindowState)
        {
            this.Hide();
            notifyIcon1.ShowBalloonTip(500);
            //WindowState = FormWindowState.Minimized;
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        notifyIcon1.ShowBalloonTip(1000);
        WindowState = FormWindowState.Normal;
    }

    private void maximizeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Show();
        WindowState = FormWindowState.Normal;
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close = true;
        this.Close();  
    }

    // Handle Closing of the Form.
    protected override void OnClosing(CancelEventArgs e)
    {
        if (Close)
        {
            e.Cancel = false;
        }
        else
        {
            WindowState = FormWindowState.Minimized;
            e.Cancel = true;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Load the basic set of eight icons.
        images = new Icon[5];
        images[0] = new Icon("C:\icon1.ico");
        images[1] = new Icon("C:\icon2.ico");
        images[2] = new Icon("C:\icon3.ico");
        images[3] = new Icon("C:\icon4.ico");
        images[4] = new Icon("C:\icon5.ico");
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Change the icon.
        // This event handler fires once every second (1000 ms).
        if (offset < 5)
        {
            notifyIcon1.Icon = images[offset];
            offset++;
        }
        else
        {
            offset = 0;
        }
    }
}