/// <summary> /// 获取顶层窗体(直到获取到为止) /// </summary> /// <param name="className"></param> /// <returns></returns> private int GetTopHwnd(GetHwndConfig hwndConfig) { int currenHwnd = 0; do { currenHwnd = GetHwndInfor.FindTopForm(hwndConfig.TopFromClassName, null); Thread.Sleep(1000); } while (currenHwnd == 0); for (int arrIndex = hwndConfig.LevelIndex.Count; arrIndex > 0; arrIndex--) { int indexCount = hwndConfig.LevelIndex[arrIndex - 1]; if (indexCount != 0) { for (int index = 0; index < (indexCount - 1); index++) { currenHwnd = GetHwndInfor.GetWindow(currenHwnd, GetWindowFlagEnum.GetNextHwnd); } } if (arrIndex > 1) { currenHwnd = GetHwndInfor.GetWindow(currenHwnd, GetWindowFlagEnum.GetChildFirstHwnd); } } addLoog("定位句柄位置成功"); return(currenHwnd); }
private void btnGetHwnd_MouseDown(object sender, MouseEventArgs e) { GetHwndInfor ghi = new GetHwndInfor(); if (ghi.ShowDialog() == DialogResult.Yes) { _CurrentMousePointHwndInfo = ghi.HwndInfor; this.labHwendTitle.Text = _CurrentMousePointHwndInfo.CurrentHwndTitle; this.txtHwndPath.Text = string.Join("-", GetHwndPath(_CurrentMousePointHwndInfo.CurrentHwnd)); } }
private void linkRegsActiveX_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { try { GetHwndInfor.AutoRegCom("regsvr32 -s dm.dll"); MessageBox.Show("注册成功!", "注册信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (Exception ee) { MessageBox.Show(ee.Message, "异常"); } }
private void btnLikeHwnd_Click(object sender, EventArgs e) { List <int> list = GetHwndInfor.ClassNameAndTitleToParentHwnd(this.txtLikeTitle.Text, this.txtClassName.Text); if (list.Count == 1) { iActiveHwnd = list[0]; this.txtHwnd.Text = iActiveHwnd.ToString(); this.txtTitle.Text = GetHwndInfor.GetHwndTitle(iActiveHwnd); MessageBox.Show("获取成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show(string.Format("获取到{0}个相同符合条件的句柄位置。", list.Count), "不能唯一确定", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void SynData(object config) { try { GetHwndConfig hwndConfig = (GetHwndConfig)config; int currenHwnd = GetTopHwnd(hwndConfig); while (true) { if (IsSrceenImage) { Bitmap bmp = GetHwndInfor.GetHwndImage(currenHwnd); Invoke(new Action(() => { this.pictureBox1.Image = bmp; bmp = OCRImage.ImageProcessing(bmp);//图像处理 string strTitle = OCRImage.OCRBitmpa(bmp).OCRContent; Invoke(new Action(() => { this.labOCRConten.Text = strTitle; addLoog("OCR识别到的内容为:" + strTitle); })); })); } else { string strTitle = GetHwndInfor.GetHwndTitle(currenHwnd); if (strTitle == "") { addLoog("开始重新定位"); currenHwnd = GetTopHwnd(hwndConfig); } Invoke(new Action(() => { this.labHwendTitle.Text = strTitle; addLoog("获取到句柄内容为:" + strTitle); })); } Thread.Sleep(1000); } } catch (Exception ex) { addLoog("异常:" + ex.Message); } }
private void GetHwndTitle() { try { while (true) { if (iActiveHwnd <= 0) { break; } this.Invoke(new Action(() => { this.txtTitle.Text = GetHwndInfor.GetHwndTitle(iActiveHwnd); })); Thread.Sleep(500); } } catch (Exception) { } }
private void btnGetActiveLocation_MouseDown(object sender, MouseEventArgs e) { try { if (this.ckbIsHidForm.Checked) { this.Hide(); } this.btnGetActiveLocation.Text = "获取位置中……"; GetHwndInfor getHwnd = new GetHwndInfor(); if (getHwnd.ShowDialog() == DialogResult.Yes) { iActiveHwnd = getHwnd.HwndInfor.CurrentHwnd; this.txtHwnd.Text = iActiveHwnd.ToString(); this.txtTitle.Text = string.Format("文本:{0}\r\n类名:{1}\r\n进程路径:{2}\r\n顶层窗体句柄:{3}\r\n鼠标位置:{4}\r\n组件距离顶层窗体的偏移量:{5}\r\n顶层窗体类名:{6}\r\n顶层窗体文本:{7}\r\n父窗体文本:{8}\r\n父窗体类名:{9}", getHwnd.HwndInfor.CurrentHwndTitle, getHwnd.HwndInfor.CurrentHwndClassName, getHwnd.HwndInfor.HwndProcessPath, getHwnd.HwndInfor.TopFromHwnd, getHwnd.HwndInfor.MousePoint, getHwnd.CalculationTopFormOffsetPoint(iActiveHwnd), getHwnd.HwndInfor.TopFromClassName, getHwnd.HwndInfor.TopFromTitle, getHwnd.HwndInfor.ParentTitle, getHwnd.HwndInfor.ParentClassName); //getHwnd.TopFromTitle this.btnGetActiveLocation.Text = "获取位置"; } else { this.btnGetActiveLocation.Text = "获取失败"; } if (this.ckbIsHidForm.Checked) { this.Show(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "异常"); } }
public int GetHwndLevelIndex(int hwnd) { int currHwndObjec = GetHwndInfor.GetWindow(hwnd, GetWindowFlagEnum.GetFirstHwnd); int endHwndObject = GetHwndInfor.GetWindow(hwnd, GetWindowFlagEnum.GetLastHwnd); int index = 1; if (currHwndObjec != hwnd) { do { currHwndObjec = GetHwndInfor.GetWindow(currHwndObjec, GetWindowFlagEnum.GetNextHwnd); if (currHwndObjec == 0 && currHwndObjec == endHwndObject) { index = 0; break; } index++; } while (currHwndObjec != hwnd); } return(index); }
private void btnLikeHwnd2_Click(object sender, EventArgs e) { List <int> list = GetHwndInfor.ClassNameAndTitleToParentHwnd(this.txtTopFormTitle.Text, this.txtTopFormClassName.Text); if (list.Count == 1) { int tempHwnd = list[0]; Regex rg = new Regex("^(?<x>[0-9]+),(?<y>[0-9]+)$"); if (rg.IsMatch(this.txtOffsetPoint.Text)) { GroupCollection group = rg.Match(this.txtOffsetPoint.Text).Groups; Point p = new Point(int.Parse(group["x"].ToString()), int.Parse(group["y"].ToString())); iActiveHwnd = GetHwndInfor.TopFromHwndAndOffsetPointGetHwnd(tempHwnd, p, this.txtParentClassName.Text); if (iActiveHwnd != 0) { this.txtHwnd.Text = iActiveHwnd.ToString(); this.txtTitle.Text = GetHwndInfor.GetHwndTitle(iActiveHwnd); MessageBox.Show("获取成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { this.txtHwnd.Text = "0"; this.txtTitle.Text = "获取失败"; } } else { MessageBox.Show("错误的鼠标位置,正确的格式(x,y):23,89", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(string.Format("获取到{0}个相同符合条件的句柄位置。", list.Count), "不能唯一确定", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public List <int> GetHwndPath(int hwnd) { List <int> PathLis = new List <int>(); int rootHwnd = GetHwndInfor.GetWindow(hwnd, GetWindowFlagEnum.GetTopHwnd); GetHwndConfig hwndConfig = new GetHwndConfig(rootHwnd); //int tempHwnd = hwnd; PathLis.Add(hwnd); hwndConfig.LevelIndex.Add(GetHwndLevelIndex(hwnd)); do { hwnd = GetHwndInfor.GetWindow(hwnd, GetWindowFlagEnum.GetParentHwnd); PathLis.Add(hwnd); if (hwnd == 0) { MessageBox.Show("啊找到最顶层了,出问题了。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } else { if (hwnd == rootHwnd) { hwndConfig.LevelIndex.Add(0); } else { hwndConfig.LevelIndex.Add(GetHwndLevelIndex(hwnd)); } } }while (rootHwnd != hwnd); this.txtHwndConfig.Text = JsonConvert.SerializeObject(hwndConfig); return(PathLis); }
private void btnSelectHwnd_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(_topFormClassName) || !string.IsNullOrEmpty(_topFormTitle)) { TopWindowHwnd = GetHwndInfor.FindTopForm(_topFormClassName, _topFormTitle); } if (TopWindowHwnd != 0) { //收银系统 if (!GetHwndInfor.TopShowForm(TopWindowHwnd)) { MessageBox.Show("置顶窗口失败", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Regex rg = new Regex("^(?<x>[0-9]+),(?<y>[0-9]+)$"); if (rg.IsMatch(this.txtMousePoint.Text)) { GroupCollection group = rg.Match(this.txtMousePoint.Text).Groups; Point p = new Point(int.Parse(group["x"].ToString()), int.Parse(group["y"].ToString())); iActiveHwnd = GetHwndInfor.MousePointGetHwnd(p); this.txtHwnd.Text = iActiveHwnd.ToString(); this.txtClassName.Text = GetHwndInfor.GetHwndClassName(iActiveHwnd); TopWindowHwnd = GetHwndInfor.GetTopFormHwnd(iActiveHwnd); //取消置顶 if (!GetHwndInfor.CancelTopShowForm(TopWindowHwnd)) { MessageBox.Show("取消置顶窗口失败", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } _topFormClassName = GetHwndInfor.GetHwndClassName(TopWindowHwnd); _topFormTitle = GetHwndInfor.GetHwndTitle(TopWindowHwnd); this.txtTitle.Text = string.Format("顶层窗体类名:{0}\r\n顶层窗体题名:{1}", _topFormClassName, _topFormTitle); string tempTitle = GetHwndInfor.GetHwndTitle(iActiveHwnd); Regex rg2 = new Regex(@"^(?<NotNuber>\D*)"); if (rg2.IsMatch(tempTitle)) { txtLikeTitle.Text = rg2.Match(tempTitle).Groups["NotNuber"].ToString(); } else { Regex rg3 = new Regex(@"(?<NotNuber>\D*)$"); if (rg3.IsMatch(tempTitle)) { txtLikeTitle.Text = rg3.Match(tempTitle).Groups["NotNuber"].ToString(); } else { txtLikeTitle.Text = tempTitle; } } } else { MessageBox.Show("错误的鼠标位置,正确的格式(x,y):23,89", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }