public static void BringChromeWindowToTop() { Process[] procsChrome = Process.GetProcessesByName("chrome"); if (procsChrome.Length == 0) { throw(new Exception("Please open chrome to record element!")); } foreach (Process chrome in procsChrome) { // the chrome process must have a window if (chrome.MainWindowHandle == IntPtr.Zero) { continue; } User32Functions.ActivateWindow(chrome.MainWindowTitle); break; } }
//build window command private static void BuildWindowCommand(IntPtr hWinEventHook, _systemEvents @event, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { switch (@event) { case _systemEvents.EventMin: return; case _systemEvents.EventMax: return; case _systemEvents.EventSystemForeGround: break; case _systemEvents.MinimizeEnd: return; case _systemEvents.MinimizeStart: return; default: return; } int length = GetWindowText(hwnd, _buffer, _buffer.Capacity); var windowName = _buffer.ToString(); //bypass screen recorder and Cortana (Win10) which throws errors if ((windowName == "Screen Recorder") || (windowName == "Cortana")) { return; } if (length > 0) { //wait additional for window to initialize //System.Threading.Thread.Sleep(250); windowName = _buffer.ToString(); dynamic activateWindowCommand = TypeMethods.CreateTypeInstance(_container, "ActivateWindowCommand"); activateWindowCommand.v_WindowName = $"\"{windowName}\""; GeneratedCommands.Add(activateWindowCommand); //detect if tracking window open location or activate windows to top left if (_trackWindowOpenLocations) { User32Functions.GetWindowRect(hwnd, out Rect windowRect); dynamic moveWindowCommand = TypeMethods.CreateTypeInstance(_container, "MoveWindowCommand"); moveWindowCommand.v_WindowName = $"\"{windowName}\""; moveWindowCommand.v_XMousePosition = windowRect.left.ToString(); moveWindowCommand.v_YMousePosition = windowRect.top.ToString(); GeneratedCommands.Add(moveWindowCommand); } else if (_activateWindowTopLeft) { dynamic moveWindowCommand = TypeMethods.CreateTypeInstance(_container, "MoveWindowCommand"); moveWindowCommand.v_WindowName = $"\"{windowName}\""; moveWindowCommand.v_XMousePosition = "0"; moveWindowCommand.v_YMousePosition = "0"; User32Functions.SetWindowPosition(hwnd, 0, 0); GeneratedCommands.Add(moveWindowCommand); } //if tracking window sizes is set if (_trackActivatedWindowSizes) { //create rectangle from hwnd User32Functions.GetWindowRect(hwnd, out Rect windowRect); //do math to get height, etc var width = windowRect.right - windowRect.left; var height = windowRect.bottom - windowRect.top; //generate command to set window position dynamic resizeWindowCommand = TypeMethods.CreateTypeInstance(_container, "ResizeWindowCommand"); resizeWindowCommand.v_WindowName = $"\"{windowName}\""; resizeWindowCommand.v_XWindowSize = width.ToString(); resizeWindowCommand.v_YWindowSize = height.ToString(); //add to list GeneratedCommands.Add(resizeWindowCommand); } } }