簡體   English   中英

當窗口恢復時,是否在C#中引發了一個事件?

[英]Is there an event raised in C# when a window is restored?

在C#/ .NET中恢復窗口時是否有任何事件?

我注意到在激活窗口時會引發一個事件,但是我找不到正在恢復的窗口的相應事件,例如從最大化或最小化狀態。

如果您不喜歡使用窗體的WindowState屬性,並且不希望保留指示窗體先前狀態的標志,則可以在稍低的級別獲得相同的結果。

您需要覆蓋窗體的窗口過程( WndProc )並偵聽指示SC_RESTOREWM_SYSCOMMAND消息 例如:

protected override void WndProc(ref Message m)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_RESTORE = 0xF120;

    if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_RESTORE)
    {
        // Do whatever processing you need here, or raise an event
        // ...
        MessageBox.Show("Window was restored");
    }

    base.WndProc(ref m);
}

你可以這樣檢查:

private void Form1_Resize(object sender, EventArgs e)
{
   if (this.WindowState == FormWindowState.Minimized)
   {
       ...
   }
   else if ....
   {
   }
}

很不確定。 您必須處理SizeChanged事件並檢測WindowState是否從Minimized更改為NormalMaximized更改為Normal 資源

我知道這個問題已經很老了但是它的工作原理如下:

public Form1()
{
    InitializeComponent();
    this.SizeChanged += new EventHandler(Form1_WindowStateTrigger);
}

private void Form1_WindowStateTrigger(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        MessageBox.Show("FORM1 MINIMIZED!");
    }

    if (this.WindowState == FormWindowState.Normal)
    {
        MessageBox.Show("FORM1 RESTORED!");
    }

    if (this.WindowState == FormWindowState.Maximized)
    {
        MessageBox.Show("FORM1 MAXIMIZED!");
    }
}

使用SizeChanged事件,再加上WindowState的檢查就可以了。

校驗:

private void Form1_Activated(Object o, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized) {
        ...
    }
}

Redhart的答案很好。 這是相同的但稍微簡單一點:

public Form1()
{
    InitializeComponent();
    this.SizeChanged += Form1_SizeChanged;
}

#region Event-Handlers
void Form1_SizeChanged(object sender, EventArgs e)
{
    var state = this.WindowState;
    switch (state)
    {
        case FormWindowState.Maximized:
            ClearMyView();
            DisplayMyStuff();
            break;
        case FormWindowState.Minimized:
            break;
        case FormWindowState.Normal:
            ClearMyView();
            DisplayMyStuff();
            break;
        default:
            break;
    }
}
#endregion Event-Handlers

添加以下內容非常簡單:

public partial class Form1 : Form {
    private FormWindowState mLastState;
    public Form1() {
      InitializeComponent();
      mLastState = this.WindowState;
    }
    protected override void OnClientSizeChanged(EventArgs e) {
      if (this.WindowState != mLastState) {
        mLastState = this.WindowState;
        OnWindowStateChanged(e);
      }
      base.OnClientSizeChanged(e);
    }
    protected void OnWindowStateChanged(EventArgs e) {
      // Do your stuff
    }

轉到此鏈接winforms-windowstate-changed-how-to-detect-this?

暫無
暫無

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

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