WPF中的C#4.0 GetWindowRect [英] C#4.0 GetWindowRect in wpf

查看:55
本文介绍了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 ))

您可以在以下位置找到更多文档:

you can find more documentation on : https://msdn.microsoft.com/fr-fr/library/system.windows.interop.windowinterophelper%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

这篇关于WPF中的C#4.0 GetWindowRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆