的WinForms:找到最小化形式的大小而不打算FormWindowState.Normal而不、最小化、形式、大小

2023-09-05 00:29:12 作者:死在新鲜感

有一个简单的方法来确定它确实有在的WindowState =普通表格的大小,而无需实际改变形式的国家?

Is there an easy way to determine the size of a Form it does have in WindowState=Normal, without actually changing the Form state?

下面是我现在做的(C#code):

Here is what I do 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;
          //  ...
     }
}

这是我想了code的样子:

This is what I would like the code to look like:

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

在事实上没有 NormalStateLocation NormalStateSize 属性Windows窗体。

In fact there are no NormalStateLocation or NormalStateSize properties in Windows Forms.

推荐答案

我想要做同样的事情,因为没有人真正回答你的问题我张贴我的解决方案,现在,即使你已经满足了。也许有人需要它的某个时候。

I wanted to do the exact same thing and as nobody actually answered your question I'm posting my solution now even though you were already satisfied. Maybe somebody needs it sometime.

    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);
        }
    }