简体   繁体   English

单击x并通过系统托盘ToolStripMenuItem删除时如何隐藏窗口表单

[英]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.

相关问题 如何在C#中隐藏Windows系统托盘? - How do I hide the Windows system tray in C#? 悬停并单击时如何更改ToolStripMenuItem的默认背景色? - How do I change the default back colors of a ToolStripMenuItem when hovered and clicked? 如何关闭设置为autoclose = false的toolstripmenuitem? - How do I close a toolstripmenuitem that is set to autoclose = false? 如何关闭(不杀死)已最小化到系统托盘的应用程序? - How do I close (not kill) an application which has minimized to the system tray? 如何从已经运行的WPF系统托盘应用程序中启动窗口? - How do I launch a window from an already running WPF system-tray application? 当用户在窗体窗口外单击时如何关闭窗体? - How do I close a form when a user clicks outside the form's window? 在最小化并且单击所有者表格一次之前,模态表格不会出现在托盘中。 如何使其显示? - Modal form doesn't appear in tray until minimized and owner-form is clicked once. How do I make it appear? 如何在表单中单击“X”,不会将其关闭但隐藏它 - How to Clicking “X” in Form, will not close it but hide it 单击关闭按钮时隐藏表单而不是关闭 - Hide form instead of closing when close button clicked 单击时启用/禁用ToolStripMenuItem - Enabling / Disabling ToolStripMenuItem when clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM