/// <summary> /// /// </summary> /// <param name="hWnd"></param> /// <param name="pt"></param> /// <returns></returns> public Window getWindow(IntPtr hWnd, POINT pt) { Window win = null; try { // Get the window under the cursor if (hWnd == IntPtr.Zero) hWnd = User32.WindowFromPoint(pt); if (hWnd == IntPtr.Zero) return null; win = new Window {hWnd = hWnd}; // Get the rect of window bool b = User32.GetWindowRect(hWnd, out win.rect); StringBuilder sb = new StringBuilder(128); // Get the class name User32.GetClassName(hWnd, sb, sb.Capacity); win.className = sb.ToString(); //Console.Write("cls: " + win.clsName); // Get the text length int n = (int) User32.SendMessage(hWnd, WM.GETTEXTLENGTH, 0, 0); if (n > 0) { // Get the text of window n = (int) User32.SendMessage(hWnd, WM.GETTEXT, (uint) sb.Capacity, sb); win.text = sb.ToString(); //Console.WriteLine("; text: " + win.text); } } catch (Exception e) { Console.WriteLine("Error in public Window getWindow(POINT pt): " + e.Message); } return win; }
// Local mouse action event handler public void MouseAction(object sender, MouseActionEventArgs e) { FrmWindowPicker form = (FrmWindowPicker) sender; Window win = new Tool().getWindow(e.hWnd, e.point); if (win != null) { //high light window's fame switch (e.mouseMessage) { case MouseMessages.WM_LBUTTONDOWN: drawFrame(win); lastWin = win; break; case MouseMessages.WM_LBUTTONUP: drawFrame(lastWin); if (lastWin.hWnd != win.hWnd) { drawFrame(win); Thread thd = new Thread(drawFrame) {Name = "delay"}; thd.Start(win); } lastWin = null; break; } // Exits the global low level mouse hook, calls GC.Collect() actively, and then reenters a new hook form.SetHook(FrmWindowPicker.WH_MOUSE_LL); form.ShowWindowProperties(win); form.PickedWindow = win; } }
/// <summary> /// Show window information in dataGridView2 /// </summary> /// <param name="win"></param> public void ShowWindowProperties(Window win) { if (win == null) return; int n = 0; if (!string.IsNullOrEmpty(win.styles)) n++; if (!string.IsNullOrEmpty(win.extendedStyles)) n++; if (win.hMenu != IntPtr.Zero) n++; if (win.itemType != null) n++; if (win.owner != IntPtr.Zero) n++; if (win.parent != IntPtr.Zero) n++; if (win.child != IntPtr.Zero) n++; if (win.previous != IntPtr.Zero) n++; if (win.next != IntPtr.Zero) n++; if (win.itemStrings != null) n += win.itemStrings.Length; if (win.itemStyles != null) n += win.itemStyles.Length; dgwWindowInfo.Rows.Clear(); dgwWindowInfo.Refresh(); dgwWindowInfo.Rows.Add(4+n); int i = 0; dgwWindowInfo.Rows[i].Cells[0].Value = "HWnd"; dgwWindowInfo.Rows[i++].Cells[1].Value = ((long)win.hWnd).ToString("D8") + " / " + ((long)win.hWnd).ToString("X8"); dgwWindowInfo.Rows[i].Cells[0].Value = "Class Name"; dgwWindowInfo.Rows[i++].Cells[1].Value = win.className; dgwWindowInfo.Rows[i].Cells[0].Value = "Text "; dgwWindowInfo.Rows[i++].Cells[1].Value = win.text; dgwWindowInfo.Rows[i].Cells[0].Value = "Location"; dgwWindowInfo.Rows[i++].Cells[1].Value = string.Format("{0}, {1}, {2}, {3}", win.rect.X, win.rect.Y, win.rect.Right, win.rect.Bottom); dgwWindowInfo.Rows[i].Cells[0].Value = "Size"; dgwWindowInfo.Rows[i++].Cells[1].Value = string.Format("{0} x {1}", win.rect.Width, win.rect.Height); if (!string.IsNullOrEmpty(win.styles)) { dgwWindowInfo.Rows[i].Cells[0].Value = "Window Style"; dgwWindowInfo.Rows[i++].Cells[1].Value = win.styles; } if (!string.IsNullOrEmpty(win.extendedStyles)) { dgwWindowInfo.Rows[i].Cells[0].Value = "Extended Window Style"; dgwWindowInfo.Rows[i++].Cells[1].Value = win.extendedStyles; } if (win.owner != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[1].Value = ((long)win.owner).ToString("D8") + " / " + ((long)win.owner).ToString("X8"); dgwWindowInfo.Rows[i].Cells[1].Style.ForeColor = Color.Blue; dgwWindowInfo.Rows[i++].Cells[0].Value = "Owner HWnd"; } if (win.parent != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[1].Value = ((long)win.parent).ToString("D8") + " / " + ((long)win.parent).ToString("X8"); dgwWindowInfo.Rows[i].Cells[1].Style.ForeColor = Color.Blue; dgwWindowInfo.Rows[i++].Cells[0].Value = "Parent HWnd"; } if (win.child != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[1].Value = ((long)win.child).ToString("D8") + " / " + ((long)win.child).ToString("X8"); dgwWindowInfo.Rows[i].Cells[1].Style.ForeColor = Color.Blue; dgwWindowInfo.Rows[i++].Cells[0].Value = "Child HWnd"; } if (win.previous != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[1].Value = ((long)win.previous).ToString("D8") + " / " + ((long)win.previous).ToString("X8"); dgwWindowInfo.Rows[i].Cells[1].Style.ForeColor = Color.Blue; dgwWindowInfo.Rows[i++].Cells[0].Value = "Previous HWnd"; } if (win.next != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[1].Value = ((long)win.next).ToString("D8") + " / " + ((long)win.next).ToString("X8"); dgwWindowInfo.Rows[i].Cells[1].Style.ForeColor = Color.Blue; dgwWindowInfo.Rows[i++].Cells[0].Value = "Next HWnd"; } if (win.hMenu != IntPtr.Zero) { dgwWindowInfo.Rows[i].Cells[0].Value = "HMenu"; dgwWindowInfo.Rows[i++].Cells[1].Value = ((long)win.hMenu).ToString("D8") + " / " + ((long)win.hMenu).ToString("X8"); } if (win.itemType != null) { dgwWindowInfo.Rows[i].Cells[0].Value = "Type of Items"; dgwWindowInfo.Rows[i++].Cells[1].Value = win.itemType; } if (win.itemStrings != null) { for (int j = 0; j < win.itemStrings.Length; j++) { dgwWindowInfo.Rows[i].Cells[0].Value = "Item String " + j.ToString(); dgwWindowInfo.Rows[i++].Cells[1].Value = win.itemStrings[j]; if (win.itemStyles == null) continue; dgwWindowInfo.Rows[i].Cells[0].Value = "Item Style " + j.ToString(); dgwWindowInfo.Rows[i++].Cells[1].Value = win.itemStyles[j]; } } }