c# - 将应用程序最小化到系统托盘

标签 c# windows winforms

我有一个由 C# 和 Visual Studio 2010 提供支持的 Windows 窗体应用。

如何将我的应用程序最小化到系统托盘(不是任务栏),然后在双击系统托盘时将其恢复?任何想法?另外,我怎样才能在系统托盘的图标中制作一些菜单,当我右键单击它时,它会显示一个菜单,如登录、断开连接、连接等。 还有,有什么方法可以让系统托盘弹出气球?

PS:我已经添加了notifyIcon,但是不知道怎么用。

最佳答案

Handle the form’s Resize event. In this handler, you override the basic functionality of the Resize event to make the form minimize to the system tray and not to the taskbar. This can be done by doing the following in your form’s Resize event handler: Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information. Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false. Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s MouseDoubleClick event. Here, you show the form using the Show() method.

private void frmMain_Resize(object sender, EventArgs e)
{
    if (FormWindowState.Minimized == this.WindowState)
    {
       mynotifyicon.Visible = true;
       mynotifyicon.ShowBalloonTip(500);
       this.Hide();
    }

    else if (FormWindowState.Normal == this.WindowState)
    {
       mynotifyicon.Visible = false;
    }
}

关于c# - 将应用程序最小化到系统托盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625421/

相关文章:

c# - "public new virtual void Method()"是什么意思?

c# - 有关 CRM 错误 : "Solution manifest import: FAILURE" 的更多详细信息

c - 如何在 C 中覆盖 Windows 系统函数

c++ - 防止在 _popen 上打开命令行

c# - 静态不可变默认实例

c# - 如何创建像 List<String,Object> 这样的集合?

windows - 在 Windows 上与 Docker 联网

c# - DateTime 问题 - 值在对象外部可用但在对象内部不可用

vb.net - 将整个界面导出为PDF

c# - Winforms 图形闪烁。 (双缓冲没有帮助!)