我是C#.Net的新手,当我点击窗口上的“X”(右上角的“关闭”按钮)时,我正在寻找隐藏窗体窗口的方法,当我点击退出时关闭应用程序系统托盘上下文菜单
为了简化我想要做的事情,我想要一些类似Skype的东西,它可以通过系统托盘选项退出并隐藏到系统尝试时单击窗口的十字架 以下是我的代码,我已尝试在表单结束事件上覆盖取消属性为true,但它也会停止系统尝试选项的关闭过程,如何区分它们。
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Icon ico;
private void Form1_Load(object sender, EventArgs e)
{
ico = notifyIcon1.Icon;
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
private void showFormToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
}
private void quitFormToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
}
}
}
`
答案 0 :(得分:0)
通常当我不得不这样做的时候,我保持一个状态标志来记住来自其他地方的电话来关闭我。那么在我的结算处理程序中,我可以检查是否需要关闭或隐藏......
bool bFormCloseRequested = false: //member of my Form
void MyCloseClick_Handler{object sender, eventArgs e)
{
if(!bFormCloseRequested)
{
e.Cancel = true;
this.hide();
}
}