private void LocateButton_Click(object sender, EventArgs e) { if (WindowTree.SelectedNode != null) { string[] SplitCharacters = { ":" }; string[] Handles = WindowTree.SelectedNode.Name.Split(SplitCharacters, StringSplitOptions.None); IntPtr ParentHandle = new IntPtr(int.Parse(Handles[0])); IntPtr Handle = new IntPtr(int.Parse(Handles[1])); if (NM.IsWindow(Handle) && NM.IsWindowVisible(Handle)) { if (ParentHandle == IntPtr.Zero) { NM.BringWindowToTop(Handle); } else { NM.BringWindowToTop(ParentHandle); } NM.tagRect area = Display.GetWindowRectangleDIP(Handle); IntPtr hWindowDC = NM.GetDC(IntPtr.Zero); IntPtr hRectanglePen = NM.CreatePen(NM.PenStyle.PS_SOLID, 3, (uint)ColorTranslator.ToWin32(Color.Red)); IntPtr hPrevPen = NM.SelectObject(hWindowDC, hRectanglePen); IntPtr hPrevBrush = NM.SelectObject(hWindowDC, NM.GetStockObject(NM.StockObjects.HOLLOW_BRUSH)); for (int i = 0; i < 3; i++) { NM.Rectangle(hWindowDC, area.left, area.top, area.right, area.bottom); Thread.Sleep(300); ClearHighlight(area); if (i < 2) { Thread.Sleep(300); } } NM.SelectObject(hWindowDC, hPrevPen); NM.SelectObject(hWindowDC, hPrevBrush); NM.ReleaseDC(Handle, hWindowDC); } else { BuildTree(); } } WindowTree.Focus(); }
public static void ClickCommon(IntPtr Parent, IntPtr Handle, int X, int Y) { if (!NM.IsWindowVisible(Handle)) { throw new Exception("Window is not visible"); } if (!NM.IsWindowEnabled(Handle)) { throw new Exception("Window is not enabled"); } IntPtr ActualParent; if (Parent == IntPtr.Zero) { ActualParent = Handle; } else { ActualParent = Parent; } if (NM.IsIconic(ActualParent)) { throw new Exception("Window is minimised"); } if (!ActiveWindow(ActualParent)) { SetFocus(Parent, Handle); } else { NM.BringWindowToTop(ActualParent); } NM.tagPoint thePoint = MouseMove(Handle, X, Y); IntPtr WindowAtPoint = NM.WindowFromPoint(thePoint); if (WindowAtPoint != Handle) { throw new Exception("Window is obscured"); } }
public static void SetFocus(IntPtr Parent, IntPtr Control) { Stopwatch timer = Stopwatch.StartNew(); IntPtr ActualParent; if (Parent == IntPtr.Zero) { ActualParent = Control; } else { ActualParent = Parent; } if (!HasFocus(IntPtr.Zero, ActualParent)) { NM.BringWindowToTop(ActualParent); if (!NM.SetForegroundWindow(ActualParent)) { Debug.WriteLine("Falling back to Hotkey method for parent"); // Fall back to the Hotkey (which will have SetForegroundWindow permission) GUI.m_ViewPort.Foreground = ActualParent; while (!GUI.m_ViewPort.Processing) { // Sendkeys won't work so use keybd_event (TODO could also use SendInput) NM.keybd_event(NM.VK_PAUSE, 0, NM.KEYEVENTF_KEYDOWN, UIntPtr.Zero); NM.keybd_event(NM.VK_PAUSE, 0, NM.KEYEVENTF_KEYUP, UIntPtr.Zero); if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { timer.Stop(); throw new Exception("Viewport SetForegroundWindow appeared to not trigger"); } Thread.Sleep(0); } GUI.m_ViewPort.Foreground = IntPtr.Zero; } while (!HasFocus(IntPtr.Zero, ActualParent)) { if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { timer.Stop(); throw new Exception("Failed to set focus to the toplevel window"); } Thread.Sleep(0); } } if (Parent != IntPtr.Zero) { if (!HasFocus(Parent, Control)) { if (!NM.SetForegroundWindow(Control)) { Debug.WriteLine("Falling back to Hotkey method for child"); // Fall back to the Hotkey (which will have SetForegroundWindow permission) GUI.m_ViewPort.Foreground = Control; // Sendkeys won't work so use keybd_event (TODO could also use SendInput) NM.keybd_event(NM.VK_PAUSE, 0x8f, NM.KEYEVENTF_KEYDOWN, UIntPtr.Zero); NM.keybd_event(NM.VK_PAUSE, 0x8f, NM.KEYEVENTF_KEYUP, UIntPtr.Zero); while (GUI.m_ViewPort.Foreground != IntPtr.Zero) { if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { timer.Stop(); throw new Exception("SetForegroundWindow failed to set focus to the window"); } Thread.Sleep(0); } } while (!HasFocus(Parent, Control)) { if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { timer.Stop(); throw new Exception("Failed to set focus to the window"); } Thread.Sleep(0); } timer.Stop(); } } return; }