private static void GetWindowInfo(IntPtr hWnd, bool isInFocus = false) { // http://www.codeproject.com/Articles/1698/MS-Spy-style-Window-Finder if (hWnd != IntPtr.Zero && WinApi.IsWindow(hWnd) && hWnd != hWndFoundWindow) { WindowSelectorEventArgs e = new WindowSelectorEventArgs(); //Программа int pId; var threadId = WinApi.GetWindowThreadProcessId(hWnd, out pId); using (var p = Process.GetProcessById(pId)) e.ProgramName = p.MainModule.ModuleName; if (isInFocus) { e.HGetForegroundWindow = (int)hWnd; var h = Misc.GetFocusedHandle(threadId, e); if (h != IntPtr.Zero) { hWnd = h; } if (hWnd == hWndFoundWindow) { return; } } //Класс var temp = new StringBuilder(256); WinApi.GetClassName(hWnd, temp, temp.Capacity); e.ClassName = temp.ToString(); temp.Clear(); //Заголовок temp.Capacity = 256; WinApi.GetWindowText(hWnd, temp, temp.Capacity); e.WindowCaption = temp.ToString(); temp.Clear(); //Дескриптор e.HWindow = (int)hWnd; e.HParent = (int)WinApi.GetParent(hWnd); //Очищаем RefreshWindow(hWndFoundWindow); //Рисуем прямоугольник HighlightFoundWindow(hWnd); hWndFoundWindow = hWnd; if (UiUpdate != null) { UiUpdate.Invoke(null, e); } } }
void WindowSelector_UiUpdate(object sender, WindowSelectorEventArgs e) { cmbProgramName.Text = e.ProgramName; txtClassName.Text = e.ClassName; txtWindowCaption.Text = e.WindowCaption; txtHandle.Text = string.Format("0x{0:X8}", e.HWindow); txtParent.Text = string.Format("0x{0:X8}", e.HParent); txtActive.Text = string.Format("0x{0:X8}", e.HActive); txtCaret.Text = string.Format("0x{0:X8}", e.HCaret); txtFocus.Text = string.Format("0x{0:X8}", e.HFocus); txtGetActiveWindow.Text = string.Format("0x{0:X8}", e.HGetActiveWindow); txtGetFocus.Text = string.Format("0x{0:X8}", e.HGetFocus); txtGetForegroundWindow.Text = string.Format("0x{0:X8}", e.HGetForegroundWindow); //txtCapture.Text = string.Format("0x{0:X8}", e.HCapture); }
public static IntPtr GetFocusedHandle(uint threadId, WindowSelectorEventArgs e = null) //Получение хендла активного контрола { //@kurumpa http://stackoverflow.com/a/28409126 IntPtr hWnd = IntPtr.Zero; IntPtr focusedHandle = IntPtr.Zero; var info = new WinApi.GUITHREADINFO(); info.cbSize = Marshal.SizeOf(info); var success = WinApi.GetGUIThreadInfo(threadId, ref info); // target = hwndCaret || hwndFocus || (AttachThreadInput + GetFocus) || hwndActive var currentThreadId = WinApi.GetCurrentThreadId(); if (currentThreadId != threadId) { WinApi.AttachThreadInput(threadId, currentThreadId, true); } focusedHandle = WinApi.GetFocus(); if (e != null) { e.HGetActiveWindow = (int)WinApi.GetActiveWindow(); } if (currentThreadId != threadId) { WinApi.AttachThreadInput(threadId, currentThreadId, false); } if (e != null) { e.HGetFocus = (int)focusedHandle; if (success) { e.HCaret = (int)info.hwndCaret; e.HFocus = (int)info.hwndFocus; e.HActive = (int)info.hwndActive; e.HCapture = (int)info.hwndCapture; } } if (success) { if (info.hwndCaret != IntPtr.Zero) { hWnd = info.hwndCaret; } else if (info.hwndFocus != IntPtr.Zero) { hWnd = info.hwndFocus; } else if (focusedHandle != IntPtr.Zero) { hWnd = focusedHandle; } else if (info.hwndActive != IntPtr.Zero) { hWnd = info.hwndActive; } } else { hWnd = focusedHandle; } return(hWnd); }