|
2 | 2 | using Microsoft.Win32; |
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
5 | 7 | using System.Reflection; |
6 | 8 |
|
7 | 9 | namespace HDRProfile |
8 | 10 | { |
| 11 | + enum ShowWindowEnum |
| 12 | + { |
| 13 | + Hide = 0, |
| 14 | + ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3, |
| 15 | + Maximize = 3, ShowNormalNoActivate = 4, Show = 5, |
| 16 | + Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8, |
| 17 | + Restore = 9, ShowDefault = 10, ForceMinimized = 11 |
| 18 | + }; |
| 19 | + |
9 | 20 | public static class Tools |
10 | 21 | { |
11 | 22 | public static void SetAutoStart(string applicationName, string filePath, bool autostart) |
@@ -46,6 +57,39 @@ public static Version ApplicationVersion |
46 | 57 |
|
47 | 58 | public static Logs Logs = new Logs($"{System.AppDomain.CurrentDomain.BaseDirectory}HDRProfile.log", "HDRPProfile", Assembly.GetExecutingAssembly().GetName().Version.ToString(), true); |
48 | 59 |
|
| 60 | + [System.Runtime.InteropServices.DllImport("user32.dll")] |
| 61 | + [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] |
| 62 | + private static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags); |
| 63 | + |
| 64 | + [System.Runtime.InteropServices.DllImport("user32.dll")] |
| 65 | + private static extern int SetForegroundWindow(IntPtr hwnd); |
| 66 | + |
| 67 | + |
| 68 | + public static void BringMainWindowToFront(string processName) |
| 69 | + { |
| 70 | + // get the process |
| 71 | + Process bProcess = Process.GetProcessesByName(processName).FirstOrDefault(); |
| 72 | + |
| 73 | + // check if the process is running |
| 74 | + if (bProcess != null) |
| 75 | + { |
| 76 | + // check if the window is hidden / minimized |
| 77 | + if (bProcess.MainWindowHandle == IntPtr.Zero) |
| 78 | + { |
| 79 | + // the window is hidden so try to restore it before setting focus. |
| 80 | + ShowWindow(bProcess.Handle, ShowWindowEnum.Restore); |
| 81 | + } |
| 82 | + |
| 83 | + // set user the focus to the window |
| 84 | + SetForegroundWindow(bProcess.MainWindowHandle); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + // the process is not running, so start it |
| 89 | + Process.Start(processName); |
| 90 | + } |
| 91 | + } |
| 92 | + |
49 | 93 | } |
50 | 94 |
|
51 | 95 |
|
|
0 commit comments