簡體   English   中英

如何在 ac#.net winforms 應用程序中編寫 alt+tab 關閉按鈕

[英]how to program the alt+tab close button in a c#.net winforms application

我有這個跟蹤電池壽命的 winform 應用程序,如果我在“alt+tab”期間關閉應用程序(如下圖所示),它會完全關閉應用程序。

在此處輸入圖像描述

我想對關閉按鈕(如果可能的話)進行編程,以將應用程序最小化到系統托盤,而不是完全關閉它。

我還沒有看到任何類似的解決方案,我所看到的只是禁用 winforms 應用程序上的 alt+tab。

我想這樣做是因為如果我關閉應用程序,電池百分比將不再受到監控,我只是想知道這是否可行。

為了最小化到系統托盤,首先從工具箱中在您的表單上添加 NotifyIcon。 然后相應地添加此代碼。

private System.Windows.Forms.ContextMenu notifyMenu;
private System.Windows.Forms.MenuItem notifyMenuItemClose;
public static bool close = false;

public ApplicationWindow()
{
    InitializeComponent();
}

private void ApplicationWindow_Load(object sender, EventArgs e)
{
    this.notifyMenu = new System.Windows.Forms.ContextMenu();
    this.notifyMenuItemClose = new System.Windows.Forms.MenuItem();
    this.notifyMenu.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] { this.notifyMenuItemClose });
    this.notifyMenuItemClose.Index = 0;
    this.notifyMenuItemClose.Text = "Exit";
    this.notifyMenuItemClose.Click += new System.EventHandler(this.notifyMenuItemClose_Click);
    this.notifyAppIcon.ContextMenu = this.notifyMenu;

}

private void notifyMenuItemClose_Click(object sender, EventArgs e)
{
    close = true;
    this.Close();
}

private void ApplicationWindow_FormClosing(object sender, FormClosingEventArgs e)
{
    if (close)
    {
        e.Cancel = false;
    }
    else
    {
        WindowState = FormWindowState.Minimized;
        e.Cancel = true;
    }
}

private void notifyAppIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (e.Button==MouseButtons.Left)
    {
        this.Show();
        WindowState = FormWindowState.Normal;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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