WinForms: finding the size of a minimized form without resorting to FormWindowState.Normal

Is there an easy way to determine the size of the form it has in WindowState = Normal without changing the state of the form?

Here is what I am doing now (C # code):

public class MyForm: Form
{
     public void MyMethod()
     {
          // ...
          FormWindowState oldState = this.WindowState;
          this.WindowState = FormWindowState.Normal;

          Point windowLocation = this.Location;
          Size windowSize = this.Size;

          this.WindowState = oldState;
          //  ...
     }
}

Here is what I would like the code to look like this:

public class MyForm: Form
{
     public void MyMethod()
     {
          // no state change here
          Point windowLocation = this.NormalStateLocation;
          Size windowSize = this.NormalStateSize;
     }
}

There are no properties NormalStateLocationor in Windows Forms NormalStateSize.

+5
source share
5 answers

I wanted to do the same, and like no one really answered your question, I submit my decision, even if you are already satisfied. Maybe someone will need it someday.

    class NativeMethods
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        /// <summary>
        /// See MSDN RECT Structure http://msdn.microsoft.com/en-us/library/dd162897(v=VS.85).aspx
        /// </summary>
        private struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        } 

        /// <summary>
        /// See MSDN WINDOWPLACEMENT Structure http://msdn.microsoft.com/en-us/library/ms632611(v=VS.85).aspx
        /// </summary>
        private struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public Point ptMinPosition;
            public Point ptMaxPosition;
            public RECT rcNormalPosition;
        }

        /// <summary>
        /// Gets the window placement of the specified window in Normal state.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <returns></returns>
        public static Rectangle GetPlacement(IntPtr handle)
        {
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement);
            GetWindowPlacement(handle, ref placement);
            var rect = placement.rcNormalPosition;
            return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
        }
    }
+8
source

.NET 2.0, RestoreBounds, .

+5

- , , "" . , AutoSizeMode, . , WindowState Minimized . , .

, , , , Aero. Vista Win7 , , . , Editbin.exe, Vista.

, . , Resize.


, . " + ", "", . ApplicationSettings, . :

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e) {
        if (Properties.Settings.Default.Form1Size.Width > 0) {  // Valid?
            this.Size = Properties.Settings.Default.Form1Size;
            this.Location = Properties.Settings.Default.Form1Location;
        }
        base.OnLoad(e);
    }
    protected override void OnLocationChanged(EventArgs e) {
        if (this.WindowState == FormWindowState.Normal) 
            Properties.Settings.Default.Form1Location = this.Location;
        base.OnLocationChanged(e);
    }
    protected override void OnResize(EventArgs e) {
        if (this.WindowState == FormWindowState.Normal)
            Properties.Settings.Default.Form1Size = this.Size;
        base.OnResize(e);
    }
    protected override void OnFormClosed(FormClosedEventArgs e) {
        Properties.Settings.Default.Save();
        base.OnFormClosed(e);
    }
}
+3

    private Size _normalSize;
    private Point _location;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.LocationChanged += new EventHandler(Form1_LocationChanged);
        this.SizeChanged += new EventHandler(Form1_SizeChanged);
    }

    void Form1_SizeChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Normal)
        {
            this._normalSize = this.Size;
        }
    }

    void Form1_LocationChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Normal)
        {
            this._location = this.Location;
        }
    }
+1

? , :

form.Shown += (s, e1) =>
{
    // Save off the size and location
    form.SaveSizeAndLocation();
};
0

All Articles