private static extern IntPtr SetWinEventHook(_systemEvents eventMin, _systemEvents eventMax, IntPtr hmodWinEventProc, SystemEventHandler lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
//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(); ActivateWindowCommand activateWindowCommand = new ActivateWindowCommand { v_WindowName = windowName, v_Comment = "Generated by Screen Recorder @ " + DateTime.Now.ToString() }; GeneratedCommands.Add(activateWindowCommand); //detect if tracking window open location or activate windows to top left if (_trackWindowOpenLocations) { User32Functions.GetWindowRect(hwnd, out Rect windowRect); MoveWindowCommand moveWindowCommand = new MoveWindowCommand { v_WindowName = windowName, v_XMousePosition = windowRect.left.ToString(), v_YMousePosition = windowRect.top.ToString(), v_Comment = "Generated by Screen Recorder @ " + DateTime.Now.ToString() }; GeneratedCommands.Add(moveWindowCommand); } else if (_activateWindowTopLeft) { //generate command to set window position MoveWindowCommand moveWindowCommand = new MoveWindowCommand { v_WindowName = windowName, v_XMousePosition = "0", v_YMousePosition = "0", v_Comment = "Generated by Screen Recorder @ " + DateTime.Now.ToString() }; 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 ResizeWindowCommand reszWindowCommand = new ResizeWindowCommand { v_WindowName = windowName, v_XWindowSize = width.ToString(), v_YWindowSize = height.ToString(), v_Comment = "Generated by Screen Recorder @ " + DateTime.Now.ToString() }; //add to list GeneratedCommands.Add(reszWindowCommand); } } }