void pinLoginStatusTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { this.curTimeLoginFlag = ""; WindowFormAPI.MinOrMaxWindowFormByTitle(new string[] { "TeamViewer Panel", "发起会话" }, 2); try { if (this.loginFlag == "1") { return; } else { this.loginFlag = "1"; } Login(); } catch (Exception ex) { this.richTextBox1.Text = this.richTextBox1.Text + string.Format("执行函数pinLoginStatusTimer_Elapsed发生异常:{0}", ex.Message); } finally { this.loginFlag = ""; } }
/// <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> /// PIN码登陆 /// </summary> private void Submit() { try { /* * 1.获取到验证码图片。 * 2.破解 * 3.赋值 * 4.提价 * 5.重试 */ #region 解析验证码并填入 MoveLeftTopSide(); string imgCode = ""; Bitmap bitmap = GetImage(); int tryCount = 0; while (!"1".Equals(Config.UnNeedQrCode) && tryCount < 2 && imgCode.Length < 4) { try { tryCount++; if (tryCount > 1) { WindowFormAPI.SendMessage(this.changeImgCodeBtnHandler, WindowFormAPI.BM_CLICK, IntPtr.Zero, null); Thread.Sleep(1000); bitmap = GetImage(); } if (bitmap == null) { break; } BreakCodeServer unCheckobj = new BreakCodeServer(bitmap); Thread.Sleep(1000); imgCode = unCheckobj.getPicnum(); } catch (Exception ex) { SetText(string.Format("解析验证码异常:{0}", "未成功解析验证码")); } } if ((!string.IsNullOrEmpty(imgCode) && imgCode.Length == 4)) { WindowFormAPI.SendMessage(this.imgCodeTextHandler, WindowFormAPI.WM_SETTEXT, IntPtr.Zero, imgCode); } #endregion #region 点击登录 Thread.Sleep(2000); WindowFormAPI.SendMessage(this.SubmitBtnHandler, WindowFormAPI.BM_CLICK, IntPtr.Zero, null); #endregion } catch (Exception ex) { SetText(String.Format("Submit提交异常:{0}", ex.Message)); } }
/// <summary> /// 将窗口移动到左上角(避免遮挡) /// </summary> private void MoveLeftTopSide() { try { if (curhWnd != null && curhWnd.ToInt32() > 0) { WindowFormAPI.SetForegroundWindow(curhWnd); WindowFormAPI.MoveWindow(curhWnd, 0, 0, 410, 300, true); } } catch (Exception) { } }
/// <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); } } } }
private static void PinCheck() { IntPtr pinForm = WindowFormAPI.FindWindow(null, "用户 PIN 码验证"); if (pinForm.ToInt32() > 0) { IntPtr pinText = WindowFormAPI.GetDlgItem(pinForm, 1000); IntPtr sureBtn = WindowFormAPI.GetDlgItem(pinForm, 1); if (pinText.ToInt32() > 0 && sureBtn.ToInt32() > 0) { WindowFormAPI.SendMessage(pinText, WindowFormAPI.WM_SETTEXT, IntPtr.Zero, Config.PIN); WindowFormAPI.ClickByIntPtr(sureBtn); } } }
/// <summary> /// 弹窗提示,自动操作,如弹出硬件特征码校验,自动点击提交申请。 /// </summary> private void TipsCheck() { IntPtr tipsForm = WindowFormAPI.FindWindow(null, "提示信息"); if (tipsForm.ToInt32() > 0) { IntPtr sureBtn = WindowFormAPI.GetDlgItem(tipsForm, 2); IntPtr tipInfo = WindowFormAPI.GetDlgItem(tipsForm, 65535); if (tipInfo.ToInt32() > 0) { StringBuilder s = new StringBuilder(512); int i = WindowFormAPI.GetWindowTextW(tipInfo, s, s.Capacity); if (s.ToString().Contains("密码错误")) { passwordError = true; SetText("用户名密码错误"); } } if (sureBtn.ToInt32() > 0) { WindowFormAPI.ClickByIntPtr(sureBtn); } } IntPtr hardwareTipsForm = WindowFormAPI.FindWindow(null, "EasyConnect"); if (hardwareTipsForm.ToInt32() > 0) { //硬件特征码校验 IntPtr yingjianBtn = WindowFormAPI.GetDlgItem(hardwareTipsForm, 1201); //提交申请ID IntPtr submitBtn = WindowFormAPI.GetDlgItem(hardwareTipsForm, 1274); if (yingjianBtn.ToInt32() > 0 && submitBtn.ToInt32() > 0) { WindowFormAPI.ClickByIntPtr(submitBtn); } } IntPtr jsTips = WindowFormAPI.FindWindow(null, "脚本错误"); if (jsTips.ToInt32() > 0) { int tmp = 0; WindowFormAPI.SendMessage(jsTips.ToInt32(), WindowFormAPI.WM_CLOSE, 0, ref tmp); } }
private Bitmap GetImage() { try { WindowFormAPI.SetForegroundWindow(curEasyConnect); Rect rect = new Rect(); WindowFormAPI.GetWindowRect(this.imgCodeHandler, out rect); int startX = rect.Left; int startY = rect.Top; int width = rect.Right - rect.Left; int height = rect.Bottom - rect.Top; Bitmap bitmap = GetPartOfImageRec(startX, startY, width, height); return(bitmap); } catch (Exception ex) { SetText(String.Format("\r\n获取验证码失败:{0}", ex.Message)); return(null); } }
/// <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> /// <param name="x"></param> /// <param name="y"></param> public static void ClickByLocation(int x, int y) { WindowFormAPI.mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero); WindowFormAPI.mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero); }
private void GetAllInfoByPrcName(String prcName) { List <WinFormModel> formInfos = new List <WinFormModel>(); try { #region 根据进程名,获取所有对应窗口下的子句柄 List <WindowInfo> list = WindowFormAPI.GetAllDesktopWindows().ToList(); //List<WindowInfo> newwindows = list.Where(c => c.szWindowName.Contains(prcName)).ToList(); List <WindowInfo> curFormList = new List <WindowInfo>(); foreach (WindowInfo item in list) { curFormList.AddRange(WindowFormAPI.GetAllDesktopWindows(item.hWnd)); } //代理设置 List <WindowInfo> newList = new List <WindowInfo>(); foreach (var item in curFormList) { List <WindowInfo> curList = newList.Where(c => { bool retVal = false; if (c.hWnd == item.hWnd && c.ItemId == item.ItemId) { retVal = true; } return(retVal); }).ToList(); if (curList != null && curList.Count <= 0) { newList.Add(item); } } //List<WindowInfo> curEditList = newList.Where(c => !string.IsNullOrEmpty(c.szWindowName) && c.szWindowName.Contains("代理设置")).ToList(); List <WindowInfo> curEditList1 = list.Where(c => !string.IsNullOrEmpty(c.szWindowName) && c.szWindowName.Contains("EasyConnect")).ToList(); List <WindowInfo> curEditList2 = curFormList.Where(c => !string.IsNullOrEmpty(c.szWindowName) && c.szWindowName.Contains("EasyConnect")).ToList(); List <WindowInfo> curEditList3 = list.Where(c => !string.IsNullOrEmpty(c.szWindowName) && c.szWindowName.Contains("EasyConnect")).ToList(); WindowInfo easyConnect = curEditList3.Where(c => { bool retVal = false; List <WindowInfo> allConnect = WindowFormAPI.GetAllDesktopWindows(c.hWnd); List <WindowInfo> existList = allConnect.Where(d => d.ItemId == 1001).ToList(); if (existList.Count > 0) { retVal = true; } return(retVal); }).FirstOrDefault(); List <WindowInfo> curConnectList = WindowFormAPI.GetAllDesktopWindows(easyConnect.hWnd); WindowInfo editHandler = curConnectList.Where(d => d.ItemId == 1001).FirstOrDefault(); StringBuilder s = new StringBuilder(512); //标题 //WindowFormAPI.GetWindowText(editHandler.hWnd, s, s.Capacity); WindowFormAPI.GetDlgItemText(editHandler.hWnd, editHandler.ItemId, s, s.Capacity); editHandler.szWindowName = s.ToString(); //List<WindowInfo> curEditList2 = newList.Where(c => !string.IsNullOrEmpty(c.szClassName) && c.szClassName.Contains("ComboBox")).ToList(); #endregion } catch (Exception ex) { } }
/// <summary> /// pin码登陆 /// </summary> public void DoLogin() { try { CloseProcess(); Process.Start(Config.SangforCSClientPath); Thread.Sleep(15000); List <WindowInfo> list = WindowFormAPI.GetAllDesktopWindows().ToList(); List <WindowInfo> newwindows = list.Where(c => c.szWindowName.Contains("EasyConnect")).ToList(); int i = 0; foreach (var item in newwindows) { i++; IntPtr curtab1 = WindowFormAPI.GetDlgItem(item.hWnd, 1002); IntPtr curtab2 = WindowFormAPI.GetDlgItem(item.hWnd, 1086); IntPtr curtab3 = WindowFormAPI.GetDlgItem(item.hWnd, 1091); if (curtab1 != IntPtr.Zero && curtab2 != IntPtr.Zero && curtab3 != IntPtr.Zero) { WindowFormAPI.SetForegroundWindow(item.hWnd); int tab1 = WindowFormAPI.FindWindowEx(item.hWnd.ToInt32(), 0, "SysTabControl32", null); int tab2 = WindowFormAPI.FindWindowEx(item.hWnd.ToInt32(), 1, "SysTabControl32", null); int tab3 = WindowFormAPI.FindWindowEx(item.hWnd.ToInt32(), 2, "SysTabControl32", null); if (tab1 != 0) { IntPtr sureBtn = WindowFormAPI.GetDlgItem(item.hWnd, 1086); this.SubmitBtnHandler = sureBtn; if (!string.IsNullOrEmpty(Config.VPNUser) && !string.IsNullOrEmpty(Config.VPNPassword)) { #region 解析账号密码 IntPtr fatherInt = new IntPtr(tab1); List <int> childWindowList = WindowFormAPI.GetChildWindows(tab1); foreach (var curitem in childWindowList) { IntPtr curIntPtr = new IntPtr(curitem); int itemId = WindowFormAPI.GetDlgCtrlID(curIntPtr); //用户名 if (itemId == 1009) { WindowFormAPI.SetForegroundWindow(item.hWnd); WindowFormAPI.SendMessage(curIntPtr, WindowFormAPI.WM_SETTEXT, IntPtr.Zero, Config.VPNUser); curEasyConnect = curIntPtr; WindowFormAPI.SetForegroundWindow(curIntPtr); curhWnd = item.hWnd; MoveLeftTopSide(); continue; } //密码 if (itemId == 1010) { WindowFormAPI.SetForegroundWindow(item.hWnd); WindowFormAPI.SendMessage(curIntPtr, WindowFormAPI.WM_SETTEXT, IntPtr.Zero, Config.VPNPassword); MoveLeftTopSide(); continue; } //验证码-输入文本框 if (itemId == 1011) { WindowFormAPI.SetForegroundWindow(item.hWnd); MoveLeftTopSide(); this.imgCodeTextHandler = curIntPtr; continue; } //验证码-图片 if (itemId == 1022) { WindowFormAPI.SetForegroundWindow(item.hWnd); this.imgCodeHandler = curIntPtr; MoveLeftTopSide(); continue; } //验证码切换 if (itemId == 1023) { WindowFormAPI.SetForegroundWindow(item.hWnd); this.changeImgCodeBtnHandler = curIntPtr; MoveLeftTopSide(); continue; } } #endregion } Submit(); } } } } catch (Exception ex) { SetText(ex.Message); } }