public static List <WindowInfo> GetAllDesktopWindows(IntPtr pHWnd) { List <WindowInfo> wndParam = new List <WindowInfo>(); List <int> child = WindowFormAPI.GetChildWindows(pHWnd.ToInt32()); foreach (var item in child) { IntPtr hWnd = new IntPtr(item); WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); wnd.hWnd = hWnd; //标题 GetWindowTextW(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); //类名 GetClassNameW(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); //进程ID GetWindowThreadProcessId(hWnd.ToInt32(), out wnd.ProcessId); //控件id wnd.ItemId = WindowFormAPI.GetDlgCtrlID(hWnd); //父句柄 wnd.FatherhWnd = WindowFormAPI.GetParent(hWnd); wndParam.Add(wnd); wndParam.AddRange(GetAllDesktopWindows(hWnd)); } return(wndParam); }
/// <summary> /// 通过窗体句柄获取标题值 /// </summary> /// <param name="hWnd"></param> /// <returns></returns> public static string GetTitleValueByIntPtr(IntPtr hWnd) { string titleVal = ""; StringBuilder titleValSb = new StringBuilder(1024); WindowFormAPI.GetWindowTextW(hWnd, titleValSb, titleValSb.Capacity); titleVal = titleValSb.ToString(); return(titleVal); }
/// <summary> /// 最大化最小化,指定标题窗口 /// </summary> /// <param name="titleArr">标题组</param> /// <param name="handleType">操作类型 1,正常 2,最小化 3,最大化</param> public static void MinOrMaxWindowFormByTitle(string[] titleArr, int handleType) { foreach (var item in titleArr) { IntPtr curForm = FindWindow(null, item); if (curForm != null && curForm.ToInt32() > 0) { WindowFormAPI.ShowWindow(curForm, handleType); } } }
/// <summary> /// 设置微信窗口前置-避免因为遮挡等原因造成解析不成功 /// </summary> private void SetWeChatFormBefore() { if (weChatForm != null && weChatForm.ToInt32() > 0) { //最小化某些窗口 WindowFormAPI.MinOrMaxWindowFormByTitle(new string[] { "发起会话", "TeamViewer Panel" }, 2); LogInfo("最大化微信窗口"); //暂时不按照正常处理,减少因为刷屏导致卡屏,造成截屏失败 //WindowFormAPI.ShowWindow(weChatForm, 1); WindowFormAPI.ShowWindow(weChatForm, 3); LogInfo("前置微信窗口"); WindowFormAPI.SetForegroundWindow(weChatForm); } }
/// <summary> /// 关闭数组中的title的窗口 /// </summary> /// <param name="titleArr"></param> public static void CloseWindowByTitle(string[] titleArr) { foreach (var item in titleArr) { IntPtr tipsForm = WindowFormAPI.FindWindow(null, item); if (tipsForm.ToInt32() > 0) { IntPtr sureBtn = WindowFormAPI.GetDlgItem(tipsForm, 2); if (sureBtn.ToInt32() > 0) { WindowFormAPI.ClickByIntPtr(sureBtn); } } } }
void StatusTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //socket连接一直维持 ConnectSocketServerNew(); if (DateTime.Now.Hour == 6) { if (DateTime.Now.Minute >= 45 && DateTime.Now.Minute <= 50) { LogInfo(string.Format("重置了GetWeChatQrCodeCount次数")); GetWeChatQrCodeCount = 0; richTextBox1Sb = new StringBuilder(); } } //最小化某些窗口 WindowFormAPI.MinOrMaxWindowFormByTitle(Config.MinFormTitles, 2); }
/// <summary> /// 获取所有桌面窗体 /// </summary> /// <returns></returns> public static WindowInfo[] GetAllDesktopWindows() { //用来保存窗口对象 列表 List <WindowInfo> wndList = new List <WindowInfo>(); EnumWindows(delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); wnd.hWnd = hWnd; GetWindowTextW(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); GetClassNameW(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); GetWindowThreadProcessId(hWnd.ToInt32(), out wnd.ProcessId); //控件id wnd.ItemId = WindowFormAPI.GetDlgCtrlID(hWnd); //父句柄 wnd.FatherhWnd = WindowFormAPI.GetParent(hWnd); wndList.Add(wnd); return(true); }, 0); return(wndList.ToArray()); }
/// <summary> /// 通过窗体句柄发送点击消息 /// </summary> /// <param name="hWnd"></param> public static void ClickByIntPtr(IntPtr hWnd) { WindowFormAPI.SendMessage(hWnd, BM_CLICK, IntPtr.Zero, ""); WindowFormAPI.SendMessage(hWnd, BM_CLICK, IntPtr.Zero, ""); }
/// <summary> /// 操作微信,并获取动态验证码 /// </summary> /// <returns></returns> private string GetQrCode() { String qrCode = ""; try { if (!String.IsNullOrEmpty(Config.FixedQrCode)) { return(Config.FixedQrCode); } if (DateTime.Now.Hour >= 21 || DateTime.Now.Hour < 7) { qrCode = "夜间不允许自动获取验证码"; return(qrCode); } //IntPtr intptr1 = WindowFormAPI.FindWindow("WeWorkWindow", null); //IntPtr intptr2 = WindowFormAPI.FindWindow("WeChatLogin", null); //if (intptr1 == null || intptr1.ToInt32() == 0) //{ // qrCode = "当前企业微信未登录"; // return qrCode; //} if (GetWeChatQrCodeCount > 20) { qrCode = "当前获取验证码次数已用完"; return(qrCode); } //1.获取句柄并最大化,并前置,避免最小化等原因造成无法点击 weChatForm = WindowHanler.WindowFormAPI.FindWindow(null, "企业微信"); LogInfo("===========开始获取验证码==========="); SetWeChatFormBefore(); //2.点击消息菜单,切花到动态验证码,注意次步骤一定要设置动态验证码为置顶状态 LogInfo("后台获取微信验证码"); ClickMsgWidthHeight("L"); Thread.Sleep(1000); LogInfo("点击微信消息菜单"); ClickMenuWidthHeight("L"); Thread.Sleep(1000); LogInfo("点击右面空白处"); ClickNullSpaceClickWidthHeight("L"); //3.点击重新获取验证码,并将获取页面关闭-OLD //ClickGenQrCodeWidthHeight("L"); LogInfo("主动发送微信验证码"); //2018.10.17 新增同步获取验证码方法 String qrCodeNew = GetQrCodeSync(); if (!string.IsNullOrEmpty(qrCodeNew)) { return(qrCodeNew); } CommonUse.PostHtml(Config.WX_URL, string.Format("{0}={1}", "userid_weixin", Config.Userid_Weixin)); GetWeChatQrCodeCount++; Thread.Sleep(3000); WindowFormAPI.CloseFormByTitle("动态密码认证"); //3.1如果使用微信登陆,则先按照截图方式解析一次,如果不行,再行复制粘贴 if (Config.USE_BD_API) { LogInfo("API解析验证码-开始"); qrCode = GetQrCodeByImage(); LogInfo("API解析验证码-完成"); LogInfo("API解析验证码-结果:" + qrCode); } if (string.IsNullOrEmpty(qrCode)) { SetWeChatFormBefore(); //4.如果经过图片解析,仍然不能成功的,用用賦值粘貼 LogInfo("API未获取到验证码"); LogInfo("开始点击右键"); ClickQrCodeLeftClickWidthHeight("R"); Thread.Sleep(1000); //鼠标移动到空白处 LogInfo("移动鼠标到空白处"); MouseFlag.SetCursorPos(Config.NullSpaceClickWidthHeight["X"], Config.NullSpaceClickWidthHeight["Y"]); Thread.Sleep(1000); LogInfo("移动鼠标到复制处"); MouseFlag.SetCursorPos(Config.QrCodeRightCopyClickWidthHeight["X"], Config.QrCodeRightCopyClickWidthHeight["Y"]); Thread.Sleep(1000); LogInfo("点击复制"); ClickQrCodeRightCopyClickWidthHeight("L"); Thread.Sleep(1000); //5.获取剪切板验证码并粘贴 qrCode = getClipBrdStr(); if (!string.IsNullOrEmpty(qrCode)) { if (Regex.IsMatch(qrCode, @"\d{6}")) { qrCode = Regex.Match(qrCode, @"\d{6}").Value; } } } } catch (Exception ex) { LogInfo(ex.Message + ex.StackTrace); } LogInfo("===========结束获取验证码==========="); return(qrCode); }