簡體   English   中英

是否可以更改WPF窗口的“最小化”按鈕事件?

[英]Is it possible to change the event of Minimize button of a WPF window?

我有一個WPF窗口。 它啟用了最小化按鈕。 但要求是,單擊最小化按鈕后,窗口應進入系統托盤。 以下代碼會將窗口隱藏在系統托盤中。 我要在“最小化”按鈕的單擊事件上執行此代碼嗎?

CurrentWindowState = WindowState.Minimized;
            OnStartClick();
            Window_StateChanged();


    private void Window_StateChanged()
    {
        if (CurrentWindowState == WindowState.Minimized)
        {
            ShowInTaskBar = false;
            _notifyIcon.BalloonTipTitle = "Minimize Sucessful";
            _notifyIcon.BalloonTipText = "Minimized the app ";
            _notifyIcon.ShowBalloonTip(400);
            _notifyIcon.Visible = true;
        }
        else if (CurrentWindowState == WindowState.Normal)
        {
            _notifyIcon.Visible = false;
            ShowInTaskBar = true;
        }
    }

反正有這樣做嗎? 還是有其他方法可以做到這一點?

將事件處理程序從Window添加到StateChanged事件

例如

this.StateChanged += (s,e) => Window_StateChanged();

並檢查如下

if (this.WindowState == WindowState.Minimized)

要么

else if (this.WindowState == WindowState.Normal)

假設這是window的實例,並且此代碼是在構造函數中編寫的。

暫無
暫無

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

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