使用FindWindow,FindWindowEx找到一个窗体上的控件句柄了。
这个控件是个按钮,如何能通过这个句柄得到这个控件的大小,位置
IntPtr hwndTest = FindWindow ( null, "TestTitle" ); //查找应用程序
IntPtr hwndBtn = FindWindowEx(hwndTest, new IntPtr(0), null, "OK");
可以通过hwndBtn 得到控件的大小位置吗?

解决方案 »

  1.   

    如果是托管的窗口,
    通过句柄拿到托管类Control c = Control.FromHandle(IntPtr hwnd);Control 类上的属性多.
      

  2.   

    1.引入命名空间using System.Runtime.InteropServices
    2.导入user32.dll中的GetWindowRect函数
    3.定义结构体RECT代码如下,button1是你要获取位置的控件:[code=C#]using System.Runtime.InteropServices;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        [DllImport("user32.dll")]
            public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
            private void button1_Click(object sender, EventArgs e)
            {
                RECT rect;//定义rect结构体对象,用来保存控件位置
                GetWindowRect(button1.Handle, out rect);//获取控件位置到rect
                string msg = string.Format("left:{0},top:{1},right{2},bottom{3}", rect.left, rect.top, rect.right, rect.bottom);//格式化字符串
                MessageBox.Show(msg);
            }
        }
    }
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }code]
      

  3.   

    程序获取的是另一个程序的控件句柄
    我在程序中可以找到这个控件的句柄,但是使用
    Control ctl = Control.FromHandle(hwndBtn); 
    返回ctl是null