c#窗体点击关闭按钮怎么最小化到托盘中而不退出程序?

如题所述

楼主你好~

可以在窗体的Closing事件中进行操作,大约如下:
privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// 取消关闭窗体
e.Cancel = true;

// 将窗体变为最小化
this.WindowState = FormWindowState.Minimized;
}

如果要实现最小化到托盘,则必须在窗体内加入控件notifyicon,并为其设置一个Icon,这个就是托盘上的小图标,然后在上面的代码中添加下列几行:

this.ShowInTaskbar = false; //不显示在系统任务栏
notifyIcon.Visible = true; //托盘图标可见

并且实现notifyIcon的DoubleClick事件,这个事件使得双击托盘图标可以让窗体回来:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal ;
this.ShowInTaskbar = true;
}
}

请追问~
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-12
#region 窗体最小化到状态栏
private void MainForm_FormClosing(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{ e.cancel= true; //取消关闭窗体事件
notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
} private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) //双击最小化图标显示窗体
{
if (this.ShowInTaskbar == false)
{
notifyIcon1.Visible = true;
this.ShowInTaskbar = false;
this.Show();
this.Activate();
this.WindowState = FormWindowState.Normal;
}
}本回答被网友采纳
第2个回答  2013-04-12
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是缩小到托盘?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//你的最小化程序...这样就可以 e.Cancel = true;
}
}
第3个回答  2013-04-12
在点击事件里面写,this.WindowState=Minimized,复制进去就可以了。
第4个回答  2013-04-12
在关闭事件中写 取消关闭,然后最少华。
相似回答