使用FindWindow,FindWindowEx找到一个窗体上的控件句柄了。
这个控件是个按钮,如何能通过这个句柄得到这个控件的大小,位置
IntPtr hwndTest = FindWindow ( null, "TestTitle" ); //查找应用程序
IntPtr hwndBtn = FindWindowEx(hwndTest, new IntPtr(0), null, "OK");
可以通过hwndBtn 得到控件的大小位置吗?
这个控件是个按钮,如何能通过这个句柄得到这个控件的大小,位置
IntPtr hwndTest = FindWindow ( null, "TestTitle" ); //查找应用程序
IntPtr hwndBtn = FindWindowEx(hwndTest, new IntPtr(0), null, "OK");
可以通过hwndBtn 得到控件的大小位置吗?
解决方案 »
- 如何将datatable传入存储过程中
- 急求通过WEBCLIENT类在c#中实现网络通信的实例??
- 如何用窗体的Load执行如ngen C:\mRelease\myinfoapp.exe的Vs2008的Dos命令呢?
- 如何取得checkboxlist所以选中的项,将它们存如数据库的一个字段?
- 求webclient类中文url请求的解决方案
- 求助一C#+SQL Server连接字符串
- 关于ado.net的一些问题
- 如何在VS2005将文本框的内容输出到excel
- WINFORM中treeview右击某接点弹出菜单,在右击没有接点的地方还是弹出菜单而且接点还是选中状态,怎么让它不再弹出菜单而且取消接点的选中
- 什么时候应该使用 ExecuteDataset 方法,什么时候应该使用 ExecuteReader 方法?
- GDI+发生一般性错误
- 关于winform自定义控件的一个问题
通过句柄拿到托管类Control c = Control.FromHandle(IntPtr hwnd);Control 类上的属性多.
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]
我在程序中可以找到这个控件的句柄,但是使用
Control ctl = Control.FromHandle(hwndBtn);
返回ctl是null