[英]C# Minimize to system tray on close
嗨,在我的C#应用程序中,当表单关闭时,我试图将应用程序最小化到系统托盘。 这是我尝试过的代码。
public void MinimizeToTray()
{
try
{
notifyIcon1.BalloonTipTitle = "Sample text";
notifyIcon1.BalloonTipText = "Form is minimized";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我正在调用该方法来形成关闭事件。 但是问题是它没有最小化托盘。 它只是关闭窗体。
e.Cancel = true;
即使您关闭计算机电源,代码也始终会取消该事件,但是以下代码可以帮助您:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
myNotifyIcon.Visible = true;
this.Hide();
e.Cancel = true;
}
}
它将允许以编程方式关闭表单。
在窗体关闭事件中编写一个事件。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
并使用“自定义”菜单条编写要显示的通知图标。
namespace MinimizeTrayNotification
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void MinimzedTray()
{
notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Application;
notifyIcon1.BalloonTipText = "Minimized";
notifyIcon1.BalloonTipTitle = "Your Application is Running in BackGround";
notifyIcon1.ShowBalloonTip(500);
}
private void MaxmizedFromTray()
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = "Maximized";
notifyIcon1.BalloonTipTitle = "Application is Running in Foreground";
notifyIcon1.ShowBalloonTip(500);
}
private void Form1_Resize(object sender, EventArgs e)
{
if(FormWindowState.Minimized==this.WindowState)
{
MinimzedTray();
}
else if (FormWindowState.Normal == this.WindowState)
{
MaxmizedFromTray();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
Form1 frm = new Form1();
frm.Show();
MaxmizedFromTray();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
notifyIcon1.BalloonTipText = "Normal";
notifyIcon1.ShowBalloonTip(500);
}
}
}
您应该取消FormClosing
事件,然后调用MinimizeToTray()
函数。
这是通过FormClosingEventArgs
的Cancel
属性完成的。
另外,考虑在某些情况下使用bool
允许在某些情况下关闭Form
,例如,如果您使用的是File > Exit
菜单或其他方法:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!allowClosing)
{
e.Cancel = true;
MinimizeToTray();
}
}
关闭时最小化将WindowState设置为Minimized
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
您需要使用FormClosing-Event。
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
e.Cancel = true;
MinimizeToTray();
}
您可以按照以下C#示例处理FormClosing
事件,例如micsoft表单关闭事件
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.