Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

c# - How to cancel the WPF form minimize event

I want to cancel the natural minimizing behavior and change a WPF form size instead.

I have a solution with the Window_StateChanged but it doesn't look so nice - the window first minimized then jumps back and does the size alteration. Is there a way to accomplish this? I Googled for Window_StateChanging but couldn't figure it out, some sort of external lib that I prefer not to use.

That's what I have:

private void Window_StateChanged(object sender, EventArgs e)
{
    switch (this.WindowState)
    {
        case WindowState.Minimized:
            {
                WindowState = System.Windows.WindowState.Normal;
                this.Width = 500;
                this.Height = 800;
                break;
            }
    }
}

Thanks,

EP

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll need to intercept the minimize command before your form fires Window_StateChanged, to avoid the minimize/restore dance you're seeing. I believe the easiest way to do this is to have your form listen for Windows messages and when the minimize command is received, cancel it and resize your form.

Register the SourceInitialized event, in your form constructor:

this.SourceInitialized += new EventHandler(OnSourceInitialized); 

Add these two handlers to your form:

private void OnSourceInitialized(object sender, EventArgs e) {
    HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
    source.AddHook(new HwndSourceHook(HandleMessages));
} 

private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
    // 0x0112 == WM_SYSCOMMAND, 'Window' command message.
    // 0xF020 == SC_MINIMIZE, command to minimize the window.
    if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF020) {
        // Cancel the minimize.
        handled = true;

        // Resize the form.
        this.Width = 500;
        this.Height = 500;
    }

    return IntPtr.Zero;
} 

I suspect this is the approach you were hoping to avoid but boiled down to the code I show it's not too difficult to implement.

Code based on this SO question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...