C# 使用 .NET 4 将程序最小化到系统托盘的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10230579/
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
Easiest way to have a program minimize itself to the system tray using .NET 4
提问by Only Bolivian Here
I'm making a new WPF application and I need to be able to minimize the application and have nice and snug in the system tray, right beside the clock (or in that general area).
我正在制作一个新的 WPF 应用程序,我需要能够最小化该应用程序,并在系统托盘中,就在时钟旁边(或在那个一般区域)。
This has to work on Windows XP, Vista and 7. I don't have to support older versions of Windows.
这必须适用于 Windows XP、Vista 和 7。我不必支持旧版本的 Windows。
What's the simplest way to achieve this if I'm using .NET 4?
如果我使用 .NET 4,那么最简单的方法是什么?
采纳答案by LaGrandMere
Here's a quick example to show how to minimize to the notification area. You need to add references to the System.Window.Formsand System.Drawingassemblies.
这是一个快速示例,用于展示如何最小化到通知区域。您需要添加对System.Window.Forms和System.Drawing程序集的引用。
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("Main.ico");
ni.Visible = true;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == System.Windows.WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
}
回答by Alex McBride
I've had success using this free notify-icon implementation in WPF.
我在 WPF 中使用这个免费的通知图标实现取得了成功。
http://www.hardcodet.net/projects/wpf-notifyicon
http://www.hardcodet.net/projects/wpf-notifyicon
It's pretty simple to setup and the source code is provided. It doesn't rely on Windows Forms, so it's 'pure' WPF and very customizable.
设置非常简单,并且提供了源代码。它不依赖于 Windows 窗体,因此它是“纯”WPF 并且非常可定制。
You can find a tutorial on how to use it on CodeProject.
And here is the Nuget Package
您可以在CodeProject上找到有关如何使用它的教程。
这是Nuget 包
回答by Serge Voloshenko
Add notifyIconto your App from Toolbox.
Select your main form>> go to the Properties>> select Eventsicon >> under FromClosing eventtype MainForm_FormClosing>> hit enter.
notifyIcon从工具箱添加到您的应用程序。
选择您的主form>> 转到Properties>> 选择Events图标 >> 在FromClosing event类型下MainForm_FormClosing>> 按 Enter。
In opened .csfile enter following event action:
在打开的.cs文件中输入以下事件操作:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
notifyIcon.Visible = true;
ShowInTaskbar = false;
e.Cancel = true;
}
Now your main FORM window will be minimized to the system tray when you click on X button. Next step is to get FORM back to normal state.
Go to the Propertiesof your notifyIcon>> find DoubleClick event>> type NotifyIcon_DoubleClickand hit enter to get event function created for you.
现在,当您单击 X 按钮时,您的主 FORM 窗口将最小化到系统托盘。下一步是让 FORM 恢复正常状态。
转到Properties您的notifyIcon>> 查找DoubleClick event>> 类型NotifyIcon_DoubleClick并按 Enter 以获取为您创建的事件函数。
Put this code to your event:
将此代码放入您的活动中:
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
notifyIcon.Visible = false;
}
Now, if you want to make the notify icon in fancy styleyou can add context menu and link it to your notify icon, so you get something like that:
现在,如果您想让通知图标具有奇特的风格,您可以添加上下文菜单并将其链接到您的通知图标,这样您就可以得到如下内容:
Here is where you link contextMenuStrip to NotifyIcon:
这是您将 contextMenuStrip 链接到 NotifyIcon 的位置:
Good luck!
祝你好运!


