Include an application in C # as a task manager

I would like to write a C # application that will switch between some running applications. It should do exact functions like Alt + Tab in windows. I use the SetForegroundWindow() function from the Windows API, but it does not work if the application is minimized in the Windows taskbar. Therefore, I added the ShowWindow() function, but there is one problem: I can not show the window in the original size that the user set.

 [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

Example: I maximize a window and then minimize it to the taskbar. When i call:

 ShowWindow(processWindowHandle, ShowWindowCmd.SW_NORMAL); WindowsApi.SetForegroundWindow(processWindowHandle); 

The window is not maximized. I tried to play with the ShowWindowCmd.SW_NORMAL parameter, but with the same result.

+6
source share
1 answer

I did it before, you want to get a list of all openings, minimize everything, and then repeat this, comparing each program with the one you want to restore, and then restore it. You need to determine which window needs to be restored, I used MainWindowTitle, because I had control over the environment, and therefore I can guarantee that each MainWindowTitle will be unique, you may not have that luxury.

The code I used in the past for this is below, it worked well:

 [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); void SwitchDatabase(string mainWindowTitle) { try { bool launched = false; Process[] processList = Process.GetProcesses(); foreach (Process theProcess in processList) { ShowWindow(theProcess.MainWindowHandle, 2); } foreach (Process theProcess in processList) { if (theProcess.MainWindowTitle.ToUpper().Contains(mainWindowTitle.ToUpper())) { ShowWindow(theProcess.MainWindowHandle, 9); launched = true; } } } catch (Exception ex) { ThrowStandardException(ex); } } 
+2
source

All Articles