将WinForms应用程序最小化到系统托盘的正确方法是什么?
注意:最小化到系统托盘 ; 在时钟任务栏的右侧.我不是要求最小化到任务栏,这是当你点击窗口上的"减号"按钮时会发生的事情.
我已经看到了像"最小化,设置ShowInTaskbar = false,然后显示你的NotifyIcon"这样的hackish解决方案.
像这样的解决方案是hackish,因为应用程序似乎不像其他应用程序那样最小化托盘,代码必须检测何时设置ShowInTaskbar = true,以及其他问题.
这样做的正确方法是什么?
实际上没有管理方法可以在本机winforms中对托盘执行这种形式的动画,但是您可以通过P/Invoke shell32.dll来执行此操作:
这里有一些好的信息(在评论中不是帖子):
http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx
在这里它是在C++中:
http://www.codeproject.com/KB/shell/minimizetotray.aspx
您可以使用它来确定Pinvoke为您的C#版本提供的内容.
this.WindowState = FormWindowState.Minimized
这是内置的方式,它在大多数时候对我来说很好看.唯一的一次是有一些奇怪的,如果你在启动时调用它有时会有一些奇怪,这就是为什么大多数人也会设置ShowInTaskbar = false并隐藏表单.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx
它会有所帮助:
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; } } }