我有一个由C#和Visual Studio 2010驱动的Windows窗体应用程序。
如何最小化我的应用程序到系统托盘(不是任务栏),然后在双击系统托盘时将其重新带回?任何想法?另外,如何在系统任务栏中的图标中创建一些菜单,当我右键单击它时,它会显示诸如“登录”,“断开连接”,“连接”之类的菜单。另外,有什么方法可以显示像从系统托盘中弹出的气球一样?
PS:我已经添加了notifyIcon,但是我不知道如何使用它。
我有一个由C#和Visual Studio 2010驱动的Windows窗体应用程序。
如何最小化我的应用程序到系统托盘(不是任务栏),然后在双击系统托盘时将其重新带回?任何想法?另外,如何在系统任务栏中的图标中创建一些菜单,当我右键单击它时,它会显示诸如“登录”,“断开连接”,“连接”之类的菜单。另外,有什么方法可以显示像从系统托盘中弹出的气球一样?
PS:我已经添加了notifyIcon,但是我不知道如何使用它。
Answers:
处理窗体的Resize事件。在此处理程序中,您将覆盖Resize事件的基本功能,以使窗体最小化到系统托盘而不是任务栏。这可以通过在窗体的Resize事件处理程序中执行以下操作来完成:检查窗体的WindowState属性是否设置为FormWindowState.Minimized。如果是,请隐藏表单,启用NotifyIcon对象,并显示显示一些信息的提示框。一旦WindowState变为FormWindowState.Normal,通过将其Visible属性设置为false来禁用NotifyIcon对象。现在,您希望在任务栏上双击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]";
this.Show();
之前不见了this.WindowState = FormWindowState.Normal;
Hide()
。但是这种方法的问题在于,当您按Alt-Tab时,窗口仍会出现。我更喜欢打电话Hide()
,那样您根本不必使用该ShowInTaskbar
属性。
Show()
and 时遇到问题Hide()
;隐藏,显示然后隐藏之后,它将不再显示。
我会去
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;
}
this.Hide()
,则在执行Alt-Tab时仍会出现该窗口。
这是我在我的应用程序中使用的方法,它相当简单并且易于说明,但是我很乐意提供更多详细信息来回答您的评论。
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);
}