English 中文(简体)
Using C# FormWindowState to restore up?
原标题:

I d like to detect if my application is minimized under certain situations, and if it is, the window needs to be restored. I can do that easily as follows:

if(this.WindowState == FormWindowState.Minimized) {
    this.WindowState = FormWindowState.Normal;
}

However, what happens if the user first maximizes the form, then minimizes it? I don t know whether to set the WindowState to FormWindowState.Normal or FormWindowState.Maximized. Is there a method or API call I can check to solve this problem?

最佳回答

The code shown below does what you need. Overriding the user s choice is pretty unwise btw.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        mLastState = this.WindowState;
    }
    FormWindowState mLastState;
    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (mLastState != this.WindowState) {
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = mLastState;
            else mLastState = this.WindowState;
        }
    }
}
问题回答

I use this solution to restore forms in MDI form. First you have to define:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

and when it comes to restore:

ShowWindowAsync(this.MdiChildren[i].Handle, this.SW_RESTORE);

This will restore form to the previous state without using additional state holders. Also you may find this article interesting

I think you should be able to call this.Show() and it will restore to the previous (visible) state.

Here s an approach that utilizes the OnResize method of the form

https://stackoverflow.com/a/6837421/578731:

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

Turns out he was right. The following seems to nicely restore your window:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

Hope this helps.

I think this is the best option (Microsoft said):

this.WindowState = System.Windows.Forms.FormWindowState.Normal;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签