[英]How do I hide a window form when x is clicked and CLose through system tray ToolStripMenuItem
I am new to C# .Net, I was looking for a way to hide my form window when 'X' on the window (Close button on the top right corner) is clicked and close the application when I click quit from my system tray context menu 我是C#.Net的新手,我正在寻找一种方法,当单击窗口中的“ X”(右上角的“关闭”按钮)时隐藏表单窗口,并在我从系统任务栏上下文中单击退出时关闭应用程序菜单
To simplify what I want to do is I want something like Skype, which can exit through system tray options and hides to system try when clicked on the cross of window Following is my code,I have tried overriding cancel property to true on form closing event but it stops the closing process for system try option as well , How do I differentiate them . 为了简化我想做的事情,我想要像Skype这样的东西,当单击窗口的十字时,它可以通过系统托盘选项退出并隐藏到系统尝试下面是我的代码,我尝试在窗体关闭事件中将cancel属性覆盖为true但是它也停止了系统try选项的关闭过程,如何区分它们。
`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");
}
}
}
`
Usually when I had to do this, I kept a statusflag to remember the call came from somewhere else to close me. 通常,当我必须这样做时,我会保留一个statusflag来记住该呼叫是从其他地方来关闭我的。 So then in my closing handler I could check if I needed to close or hide...
因此,在我的关闭处理程序中,我可以检查是否需要关闭或隐藏...
bool bFormCloseRequested = false: //member of my Form
void MyCloseClick_Handler{object sender, eventArgs e)
{
if(!bFormCloseRequested)
{
e.Cancel = true;
this.hide();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.