【发布时间】:2016-04-05 20:14:52
【问题描述】:
我想得到我的wpf接口的位置。这段代码在c#2.0可以工作,但是在c#4.0报错。这里是代码。
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
Rectangle myRect = new Rectangle();
private void button1_Click(object sender, System.EventArgs e)
{
RECT rct;
if(!GetWindowRect(new HandleRef(this, this.Handle), out rct )) //Here is the error
{
MessageBox.Show("ERROR");
return;
}
MessageBox.Show( rct.ToString() );
myRect.X = rct.Left;
myRect.Y = rct.Top;
myRect.Width = rct.Right - rct.Left + 1;
myRect.Height = rct.Bottom - rct.Top + 1;
}
【问题讨论】:
-
什么是错误代码?
-
错误是未包含“句柄”的定义。
-
你的班级定义是什么?