我有一个由C#和Visual Studio 2010提供支持的Windows窗体应用程序。
如何将我的应用最小化到系统托盘(而非任务栏),然后在系统托盘中双击时将其恢复?任何的想法?另外,如何在系统托盘中的图标中创建一些菜单,当我右键单击它时,它会显示一个菜单,如登录,断开连接,连接等等。 此外,是否有任何方法可以显示从系统托盘弹出的气球?
PS:我已经添加了一个notifyIcon,但我不知道如何使用它。
答案 0 :(得分:144)
处理表单的Resize事件。在此处理程序中,您重写 Resize事件的基本功能,使表单最小化 系统托盘而不是任务栏。这可以通过这样做来完成 在你的表单中跟随Resize事件处理程序:检查是否 form的WindowState属性设置为FormWindowState.Minimized。如果 是的,隐藏您的表单,启用NotifyIcon对象,并显示 气球提示显示一些信息。一旦WindowState成为 FormWindowState.Normal,通过设置它来禁用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;
}
}
答案 1 :(得分:68)
我发现这可以完成整个解决方案。上面的答案无法从任务栏中删除窗口。
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]";
答案 2 :(得分:17)
我会去
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;
}
答案 3 :(得分:16)
答案 4 :(得分:12)
试试这个
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;
}
答案 5 :(得分:3)
这是我在我的应用程序中使用的方法,它相当简单且不言自明,但我很乐意提供更多详细信息来回答您的意见。
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);
}
答案 6 :(得分:1)
单击系统托盘中的图像,您可以验证框架是否可见,然后您必须设置Visible = true或false
答案 7 :(得分:1)
...并且右键单击通知菜单向表单添加上下文菜单并编辑它,并通过双击它们为每个上下文菜单设置鼠标点击事件,然后通过选择notifyicon属性中的ContextMenuStrip将其附加到notifyicon1。
答案 8 :(得分:-2)
this.WindowState = FormWindowState.Minimized;