本文介绍了WPF中的C#4.0 GetWindowRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取wpf接口的位置.此代码可以在c#2.0中工作,但是在c#4.0中报告错误.这是代码.
I want to get the position of my wpf interface.This code can work in c#2.0,but Report an error in c#4.0.Here is the code.
[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; } 推荐答案您应该使用
WindowInteropHelper();像这样(用于wpf)获取句柄
to get the handle, like this (for wpf)
if(!GetWindowRect(new HandleRef(this, new WindowInteropHelper(this).Handle), out rct ))您可以在以下位置找到更多文档: msdn.microsoft/fr-fr/library/system.windows.interop.windowinterophelper%28v=vs.110%29.aspx?f=255& MSPPError = -2147217396
you can find more documentation on : msdn.microsoft/fr-fr/library/system.windows.interop.windowinterophelper%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
更多推荐
WPF中的C#4.0 GetWindowRect
发布评论