簡體   English   中英

單擊x並通過系統托盤ToolStripMenuItem刪除時如何隱藏窗口表單

[英]How do I hide a window form when x is clicked and CLose through system tray ToolStripMenuItem

我是C#.Net的新手,我正在尋找一種方法,當單擊窗口中的“ X”(右上角的“關閉”按鈕)時隱藏表單窗口,並在我從系統任務欄上下文中單擊退出時關閉應用程序菜單

為了簡化我想做的事情,我想要像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");

        }
    }
}
` 

通常,當我必須這樣做時,我會保留一個statusflag來記住該呼叫是從其他地方來關閉我的。 因此,在我的關閉處理程序中,我可以檢查是否需要關閉或隱藏...

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM