最小化应用程序到系统托盘

我有一个由 C # 和 Visual Studio 2010驱动的 Windows 表单应用程序。

如何将应用程序最小化到系统托盘(而不是任务栏) ,然后在双击系统托盘时将其返回?知道吗?另外,我如何在系统托盘的图标中制作一些菜单,当我右键单击它时,它会显示一个菜单,如登录,断开,连接,诸如此类的东西。 还有,有没有什么方法可以像气球一样从系统托盘中弹出来?

PS: I already added a notifyIcon, but I do not know how to use it.

221320 次浏览

处理窗体的“调整大小”事件。在此处理程序中,重写 Resize 事件的基本功能,使窗体最小化为 系统托盘,而不是到任务栏。这可以通过执行 在窗体的 Resize 事件处理程序中执行以下操作: 检查 表单的 WindowState 属性设置为 FormWindowState.Minimized.If yes, hide your form, enable the NotifyIcon object, and show the 显示一些信息的气球提示。一旦 WindowState 变为 设置 NotifyIcon 对象的 可见属性设置为 false。现在,希望窗口在下列情况下重新出现 双击任务栏中的 NotifyIcon 对象, 处理 NotifyIcon 的 MouseDoubleClick 事件 使用 Show ()方法的窗体。

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;
}
}

我发现这个可以完成整个解决方案。上面的答案未能从任务栏中删除窗口。

private void ImportStatusForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(3000);
this.ShowInTaskbar = false;
}
}


private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon.Visible = false;
}

此外,最好使用窗体设计器设置通知图标控件的以下属性。

this.notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info; //Shows the info icon so the user doesn't think there is an error.
this.notifyIcon.BalloonTipText = "[Balloon Text when Minimized]";
this.notifyIcon.BalloonTipTitle = "[Balloon Title when Minimized]";
this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon"))); //The tray icon to use
this.notifyIcon.Text = "[Message shown when hovering over tray icon]";

我会和你一起去

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


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

试试这个

 private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.BalloonTipText = "Application Minimized.";
notifyIcon1.BalloonTipTitle = "test";
}


private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = false;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000);
}
}


private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}

不要忘记添加图标文件到您的通知图标,否则它将不会出现在托盘。

在系统托盘中单击图像时,可以验证框架是否可见,然后必须设置 Visible = true 或 false

对于你的右击通知菜单,在表单中添加一个上下文菜单,并对其进行编辑,通过双击每个上下文菜单项来设置鼠标选择事件,然后通过在 notifyicon 属性中选择 ContextMenuStrip 来将其附加到 notifyicon1。

这是我在我的应用程序中使用的方法,它相当简单,不言自明,但我很高兴给予更多的细节,以回应您的意见。

    public Form1()
{
InitializeComponent();


// When window state changed, trigger state update.
this.Resize += SetMinimizeState;


// When tray icon clicked, trigger window state change.
systemTrayIcon.Click += ToggleMinimizeState;
}


// Toggle state between Normal and Minimized.
private void ToggleMinimizeState(object sender, EventArgs e)
{
bool isMinimized = this.WindowState == FormWindowState.Minimized;
this.WindowState = (isMinimized) ? FormWindowState.Normal : FormWindowState.Minimized;
}


// Show/Hide window and tray icon to match window state.
private void SetMinimizeState(object sender, EventArgs e)
{
bool isMinimized = this.WindowState == FormWindowState.Minimized;


this.ShowInTaskbar = !isMinimized;
systemTrayIcon.Visible = isMinimized;
if (isMinimized) systemTrayIcon.ShowBalloonTip(500, "Application", "Application minimized to tray.", ToolTipIcon.Info);
}

最小化;