是否有一种简单的方法来确定它在WindowState = Normal中具有的Form的大小,而不实际更改Form状态?
这是我现在做的(C#代码):
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;
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我希望代码看起来像:
public class MyForm: Form
{
public void MyMethod()
{
// no state change here
Point windowLocation = this.NormalStateLocation;
Size windowSize = this.NormalStateSize;
}
}
Run Code Online (Sandbox Code Playgroud)
事实上,Windows窗体中没有NormalStateLocation或NormalStateSize属性.
我想做同样的事情并且没有人真正回答你的问题我现在正在发布我的解决方案,即使你已经满意了.也许有人需要它.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3124 次 |
| 最近记录: |