根据窗体的Caption和Class获取窗体的句柄

       private const Int32 MAX_WINDOW_TEXT = 1024;
        private const Int32 MAX_WINDOW_CLASS = 1024;
        private const UInt32 GW_HWNDNEXT = (UInt32)2;
       // private const UInt32 GW_HWNDFIRST = 0;
        private const UInt32 GW_CHILD = 5;
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();//获得桌面上的句柄

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);//根据句柄获取caption

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 GetClassName(IntPtr hWnd, StringBuilder lpClassName, Int32 nMaxCount);//根据句柄获取Class

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);//检索父窗体的句柄

        public static String GetWindowCaption(IntPtr handle)
        {
            StringBuilder builder = new StringBuilder(MAX_WINDOW_TEXT);
            if (GetWindowText(handle, builder, MAX_WINDOW_TEXT) > 0)
                return builder.ToString();
            return String.Empty;
        }

        public static String GetWindowClass(IntPtr handle)
        {
            StringBuilder builder = new StringBuilder(MAX_WINDOW_CLASS);
            if (GetClassName(handle, builder, MAX_WINDOW_CLASS) > 0)
                return builder.ToString();
            return String.Empty;
        }
设计思路
1,需要调用的api ,在正个桌面窗体中查找到所有窗体的句柄。

2,根据窗体句柄获取窗体的Catpion,

3,根据窗体句柄获取窗体的Class,

4,比对caption和class是否相等,

5,把相等的结果加入到数组中。

6,还可以进行模糊查询,用到string.Contains()

代码文件地址:http://download.csdn.net/detail/elrphant/4043321




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值