我想获得我的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;
}
答案 0 :(得分:0)
你应该使用
WindowInteropHelper();
得到句柄,像这样(对于wpf)
if(!GetWindowRect(new HandleRef(this, new WindowInteropHelper(this).Handle), out rct ))
上找到更多文档