C#使用API枚举窗体查找句柄或获取包含特定字符串的窗口标题及控件内容

问题1:项目需要查找PPT句柄,通过PPT标题精准匹配有时会查找不到

原因:PPT的标题会跟据power point 版本有差异,同时ppt本身打开后标题有时会增加一些内容,比如PPT制作完成后,保存为97——2003版,当我们使用高版本的power point打开标题会出现[兼容模式]字样,这样会导致精确匹配比较难做

能不能做到模糊匹配从而获取PPT句柄呢,答案是可以的

解决方案:使用API :EnumWindows枚举所有窗体,获取标题,再去匹配需要查找的字符串,从而取到句柄

  const int WM_GETTEXT = 0x000D;
        const int WM_GETTEXTLENGTH = 0x000E;

 [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hwnd, int wMsg, int wParam, Byte[] lParam);

/// <summary>
        /// 模糊匹配控件标题
        /// </summary>
        /// <param name="dimStr">模糊匹配字符串</param>
        /// <returns></returns>
        public static IntPtr FindWindowExByDimStrIntoWindow(string dimStr)
        {
            IntPtr iResult = IntPtr.Zero;

            string controlTitle = ""; //控件完全标题

            // 枚举子窗体,查找控件句柄
            int i = EnumWindows(
            (h, l) =>
            {
                int cTxtLen;
                if (IsWindowVisible(h))
                {
                    //对每一个枚举窗口的处理
                    cTxtLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
                    Byte[] byt = new Byte[cTxtLen];
                    SendMessage((int)h, WM_GETTEXT, cTxtLen + 1, byt); //获取内容
                    string str = Encoding.Default.GetString(byt);
                    if (str.ToString().Contains(dimStr))
                    {
                        iResult = h;
                        controlTitle = str.ToString();
                        return false;
                    }
                    else
                        return true;
                }
                else
                    return true;

            },
            0);

            // 返回查找结果
            return iResult;
        }
 

问题二:在指定窗体中,查找子窗口的文本内容

        /// <summary>
        /// 遍历子窗体,寻找文本框内容包含指定字符串的完全内容
        /// </summary>
        /// <param name="hwnd">父窗体句柄</param>
        /// <param name="dimStr">模糊匹配字符串</param>
        /// <returns></returns>
        public static string FindWindowExByDimStrIntoChildWindow2(IntPtr hwnd, string dimStr)
        {
            Log.WriteLog("进入函数:FindWindowExByDimStrIntoChildWindow \n");
            IntPtr iResult = IntPtr.Zero;

            string controlTitle = ""; //控件完全标题

            // 枚举子窗体,查找控件句柄
            int i = EnumChildWindows(
            hwnd,
            (h, l) =>
            {
                int TextLen;
                TextLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
                Byte[] byt = new Byte[TextLen];
                SendMessage((int)h, WM_GETTEXT, TextLen + 1, byt); //获取内容
                string str = Encoding.Default.GetString(byt);
               // Log.WriteLog(string.Format("Edit控件内容{0}\n", str));
                if (str.ToString().Contains(dimStr))
                {
                    iResult = h;
                    controlTitle = str.ToString();
                    Log.WriteLog(string.Format("模糊匹配找到的路径{0}\n", controlTitle));
                    return false;
                }
                else
                    return true;

            },
            0);

            // 返回查找结果
            return controlTitle;
        }

如果知道控件类型为Edit,可以使用以下函数

        /// <summary>
        /// 模糊匹配控件标题
        /// </summary>
        /// <param name="hwnd">父窗体句柄</param>
        /// <param name="dimStr">模糊匹配字符串</param>
        /// <returns></returns>
        public static string FindWindowExByDimStrIntoChildWindow(IntPtr hwnd, string dimStr)
        {
            Log.WriteLog("进入函数:FindWindowExByDimStrIntoChildWindow \n");
            IntPtr iResult = IntPtr.Zero;

            string controlTitle = ""; //控件完全标题

            // 枚举子窗体,查找控件句柄
            int i = EnumChildWindows(
            hwnd,
            (h, l) =>
            {

                IntPtr HwndStatue = FindWindowEx(h, 0, "Edit", null);   //文本框句柄 
                StringBuilder title1 = new StringBuilder(200);
                int len1;
                len1 = GetWindowText(h, title1, 200);

                if (HwndStatue != IntPtr.Zero)
                {
                    int TextLen;
                    TextLen = SendMessage(HwndStatue, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
                    Byte[] byt = new Byte[TextLen];
                    SendMessage((int)HwndStatue, WM_GETTEXT, TextLen + 1, byt); //获取内容
                    string str = Encoding.Default.GetString(byt);
                   // Log.WriteLog(string.Format("Edit控件内容{0}\n", str));
                    if (str.ToString().Contains(dimStr))
                    {
                        iResult = h;
                        controlTitle = str.ToString();
                        Log.WriteLog(string.Format("模糊匹配找到的路径{0}\n", controlTitle));
                        return false;
                    }
                    else
                        return true;
                }
                else
                    return true;

            },
            0);

            // 返回查找结果
            return controlTitle;
        }

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破浪征程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值