internal static bool ShowWindow(IntPtr hWnd, WindowShowCommand showCommand) { return(NativeMethods.ShowWindow(hWnd, (int)showCommand)); }
//Focus on a process window async Task PrepareFocusProcessWindow(string processName, int processIdTarget, IntPtr windowHandleTarget, WindowShowCommand windowShowCommand, bool setWindowState, bool setTempTopMost, bool silentFocus, bool launchKeyboard) { try { if (!vChangingWindow) { vChangingWindow = true; //Check if process is available if (windowHandleTarget == null) { if (!silentFocus) { await Notification_Send_Status("Close", "App no longer running"); } Debug.WriteLine("Show application no longer seems to be running."); vChangingWindow = false; return; } //Check if process is available if (windowHandleTarget == IntPtr.Zero) { if (!silentFocus) { await Notification_Send_Status("Close", "App can't be shown"); } Debug.WriteLine("Application can't be shown, window handle is empty."); vChangingWindow = false; return; } //Update the interface status if (!silentFocus) { if (!(processName.ToLower() == "ctrlui" && vAppActivated)) { await Notification_Send_Status("AppMiniMaxi", "Showing " + processName); } } Debug.WriteLine("Showing application window: " + processName); //Focus on application window handle bool windowFocused = await FocusProcessWindow(processName, processIdTarget, windowHandleTarget, windowShowCommand, setWindowState, setTempTopMost); if (!windowFocused) { await Notification_Send_Status("Close", "Failed showing the app"); Debug.WriteLine("Failed showing the application, no longer running?"); vChangingWindow = false; return; } //Launch the keyboard controller if (launchKeyboard) { await KeyboardControllerHideShow(true); } vChangingWindow = false; } } catch (Exception ex) { await Notification_Send_Status("Close", "Failed showing the app"); Debug.WriteLine("Failed showing the application, no longer running? " + ex.Message); vChangingWindow = false; } }
public static extern bool ShowWindow(IntPtr hWnd, WindowShowCommand nCmdShow);
//Focus on a process window public static async Task <bool> FocusProcessWindow(string processTitle, int processId, IntPtr processWindowHandle, WindowShowCommand windowShowCommand, bool setWindowState, bool setTempTopMost) { try { //Prepare the process focus async Task <bool> TaskAction() { try { //Close open Windows prompts await CloseOpenWindowsPrompts(); //Get the current focused application ProcessMulti foregroundProcess = GetProcessMultiFromWindowHandle(GetForegroundWindow()); //Close open start menu, cortana or search await CloseOpenWindowsStartMenu(foregroundProcess); //Close open Windows system menu await CloseOpenWindowsSystemMenu(foregroundProcess); //Detect the previous window state if (windowShowCommand == WindowShowCommand.None && setWindowState) { WindowPlacement processWindowState = new WindowPlacement(); GetWindowPlacement(processWindowHandle, ref processWindowState); Debug.WriteLine("Detected the previous window state: " + processWindowState.windowFlags); if (processWindowState.windowFlags == WindowFlags.RestoreToMaximized) { windowShowCommand = WindowShowCommand.ShowMaximized; } else { windowShowCommand = WindowShowCommand.Restore; } } //Change the window state command if (setWindowState) { ShowWindowAsync(processWindowHandle, windowShowCommand); await Task.Delay(10); ShowWindow(processWindowHandle, windowShowCommand); await Task.Delay(10); } //Set the window as top most if (setTempTopMost) { SetWindowPos(processWindowHandle, (IntPtr)WindowPosition.TopMost, 0, 0, 0, 0, (int)WindowSWP.NOMOVE | (int)WindowSWP.NOSIZE); await Task.Delay(10); } //Retry to show the window for (int i = 0; i < 2; i++) { try { //Allow changing window AllowSetForegroundWindow(processId); await Task.Delay(10); //Bring window to top BringWindowToTop(processWindowHandle); await Task.Delay(10); //Switch to the window SwitchToThisWindow(processWindowHandle, true); await Task.Delay(10); //Focus on the window UiaFocusWindowHandle(processWindowHandle); await Task.Delay(10); } catch (Exception ex) { Debug.WriteLine("Process focus error: " + ex.Message); } } //Disable the window as top most if (setTempTopMost) { SetWindowPos(processWindowHandle, (IntPtr)WindowPosition.NoTopMost, 0, 0, 0, 0, (int)WindowSWP.NOMOVE | (int)WindowSWP.NOSIZE); await Task.Delay(10); } //Return bool Debug.WriteLine("Focused process window: " + processTitle + " WindowHandle: " + processWindowHandle + " ShowCmd: " + windowShowCommand); return(true); } catch { } Debug.WriteLine("Failed focusing process: " + processTitle); return(false); }; //Focus the process return(await AVActions.TaskStartReturn(TaskAction).Result); } catch { } Debug.WriteLine("Failed focusing process: " + processTitle); return(false); }
public static void SetWindowState(IntPtr hWnd, WindowShowCommand state) { NativeMethods.ShowWindow(hWnd, (int)state); }