【工作经历:阿里巴巴搜索技术研发中心QA ,百度新产品测试部QA】 【领域:测试分析,自动化测试,性能测试,安全测试 】 【个人定位:高级测试工程师+培训师+领域产品专家】

遍历一个窗口的子窗口代码

上一篇 / 下一篇  2008-07-25 21:30:59 / 个人分类:自动化测试


从网上搜索到一段代码。我使用它来验证我的一个思想。
spy++肯定是根据hwnd来遍历的。如果一个控件不能被spy++识别。就只能通过msaa来识别。
这样是说hwnd和msaa并不是一一对应。疑问?

这个代码是无法遍历到遨游的工具栏的。
我使用msaa是可以的。

同时也可以使用消息,通过spy++获取发送的消息,然后模拟就可以了。
鼠标点击的位置是可以通过他们的父控件的位置来计算出来的。

再想,也许可以有其他的方法。


       static void Main(string[] args)
        {
            //IsClientPopupWindows();
            EnumChildWindows(920262, callBackEnumChildWindows, 0);
            Console.ReadKey();
        }


        [DllImport("user32.dll")]
        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
                                                                           string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        public static extern int FindWindow(string strclassName, string strWindowName);


        [DllImport("user32.dll")]
        public static extern int GetLastActivePopup(int hWnd);

        [DllImport("user32.dll")]
        public static extern int AnyPopup();

        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);

        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack lpfn, int lParam);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

        /// <summary>
        /// 回调函数代理
        /// </summary>
        public delegate bool CallBack(int hwnd, int lParam);


        /// <summary>
        /// 进程回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static bool ThreadWindowProcess(int hwnd, int lParam)
        {
            EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
            return true;
        }

        /// <summary>
        /// 窗口回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static bool WindowProcess(int hwnd, int lParam)
        {
            EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
            return true;
        }

        /// <summary>
        /// 子窗口回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static bool ChildWindowProcess(int hwnd, int lParam)
        {
            StringBuilder title = new StringBuilder(200);
            int len;
            len = GetWindowText(hwnd, title, 200);
            if (len < 1)
                return true;
            if(title.ToString().Contains("标准工具栏"))
                EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
            Console.WriteLine(title+" "+hwnd);
            return true;
        }


        /// <summary>
        /// 进程窗口回调函数代理
        /// </summary>
        public static CallBack callBackEnumThreadWindows = new CallBack(ThreadWindowProcess);

        /// <summary>
        /// 窗口回调函数代理
        /// </summary>
        public static CallBack callBackEnumWindows = new CallBack(WindowProcess);


        /// <summary>
        /// 子窗口回调函数代理
        /// </summary>
        public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);

        /// <summary>
        /// 客户端是否弹出对话框
        /// </summary>
        /// <returns></returns>
        public static bool IsClientPopupWindows()
        {
            bool FindError = false;
            EnumWindows(callBackEnumWindows, 0);
            return FindError;
        }

TAG: 自动化测试

sihanjishu的个人空间 引用 删除 sihanjishu   /   2009-09-30 00:49:19
原帖由zengyixun于2009-09-29 12:15:50发表
遨游的工具栏不用msaa,也可以被遍历到的呀


是个别的小局部按钮。你可以试试。当时这是搜狐面试的时候,提的问题。
燃灯斋 引用 删除 zengyixun   /   2009-09-29 12:15:50
遨游的工具栏不用msaa,也可以被遍历到的呀
 

评分:0

我来说两句

Open Toolbar