我希望有人能够帮助我,我有我的应用程序代码的方法如下有点让我一个矩形GetWindowRect返回奇数值
var windowPosition = NativeMethods.GetWindowRect(this._hwnd);
return new Rect(windowPosition.Left, windowPosition.Top, windowPosition.Width, windowPosition.Height);
我在用这个我为自定义窗口编写的一些代码在WPF应用程序中,窗口使用IWindowManager.OpenWindow
打开。
当我运行的代码,并在打开的窗口,我在windowPosition
对象,它是一个RECT
top = 1466, bottom = 785, left = 26, right = 26, width = 0, height = -681
我看不到会到底是什么错误代码与这些奇怪得到以下值值在RECT中,因此我在下一行中得到一个ArgumentException
。
我也试着运行这个窗口作为主应用程序窗口,我遇到同样的问题,应用程序使用MVVM和Caliburn Micro虽然我不知道为什么应该有所作为。
作为请求的RECT结构定义如下:
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public void Offset(int dx, int dy)
{
this.Left += dx;
this.Top += dy;
this.Right += dx;
this.Bottom += dy;
}
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
public int Width
{
get
{
return this.Right - this.Left;
}
}
public int Height
{
get
{
return this.Bottom - this.Top;
}
}
public POINT Position
{
get
{
return new POINT { x = this.Left, y = this.Top };
}
}
public SIZE Size
{
get
{
return new SIZE { cx = this.Width, cy = this.Height };
}
}
}
和从NativeMethods所述GetWindowRect方法:
[DllImport("user32.dll", EntryPoint = "GetWindowRect", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRectInternal(IntPtr hWnd, out RECT lpRect);
public static RECT GetWindowRect(IntPtr hwnd)
{
RECT rc;
if (!GetWindowRectInternal(hwnd, out rc))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return rc;
}
我使用WindowInteropHelper(window).Handle
访问hWnd
,我忽略包括来自公众方法NativeMethods现在已经包含了它,对于浪费时间表示歉意。
RECT没有宽度或高度属性,显着增加了你刚刚宣布它错误的几率。奇怪你没有发布相关的代码。 –
向我们展示了如何定义GetWindowRect和RECT结构。 – Dispersia
它是让上下右上,而不是左上下。 –