|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | + |
| 8 | +namespace TirtaWindows11ShowDesktopUtility |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + [DllImport("kernel32.dll")] |
| 13 | + static extern IntPtr GetConsoleWindow(); |
| 14 | + [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] |
| 15 | + static extern IntPtr FindWindow(string lpClassName, string lpWindowName); |
| 16 | + [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)] |
| 17 | + static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam); |
| 18 | + [DllImport("user32.dll")] |
| 19 | + static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Window Manager user32.dll IntPTR |
| 23 | + /// </summary> |
| 24 | + const int WM_COMMAND = 0x111; |
| 25 | + const int MIN_ALL = 419; |
| 26 | + const int MIN_ALL_UNDO = 416; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Hide selected window |
| 30 | + /// </summary> |
| 31 | + const int SW_HIDE = 0; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Show selected window |
| 35 | + /// </summary> |
| 36 | + const int SW_SHOW = 5; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// String that will be written into the marker files, it's basically just a placeholder. |
| 40 | + /// </summary> |
| 41 | + const string StateMarkerContent = "Yo!"; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Remmeber state in order to perform Show/Hide on the desktop. |
| 45 | + /// False = Hide Only, True = Hide and Show alternating. |
| 46 | + /// </summary> |
| 47 | + const Boolean RememberState = false; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Current Application directory. |
| 51 | + /// </summary> |
| 52 | + static string ApplicationPath = AppDomain.CurrentDomain.BaseDirectory; |
| 53 | + |
| 54 | + static void Main(string[] args) |
| 55 | + { |
| 56 | + // Minimize the console app |
| 57 | + ShowWindow(GetConsoleWindow(), SW_HIDE); |
| 58 | + |
| 59 | + if (args.Contains("--setup")) { |
| 60 | + // If there is a --setup argument |
| 61 | + |
| 62 | + // Show the console app |
| 63 | + ShowWindow(GetConsoleWindow(), SW_SHOW); |
| 64 | + |
| 65 | + // Make a marker that we are in setup mode |
| 66 | + try |
| 67 | + { |
| 68 | + // Create and Write a setup marker file |
| 69 | + System.IO.FileStream a = System.IO.File.Create(ApplicationPath + @"SetupMode.TW11SDU", Encoding.ASCII.GetByteCount(StateMarkerContent), System.IO.FileOptions.RandomAccess); |
| 70 | + a.Write(Encoding.ASCII.GetBytes(StateMarkerContent), 0, Encoding.ASCII.GetByteCount(StateMarkerContent)); |
| 71 | + a.Flush(); |
| 72 | + a.Dispose(); |
| 73 | + } |
| 74 | + catch (Exception _) |
| 75 | + { |
| 76 | + Console.WriteLine("Got exception while trying to make setup marker, Exception message : " + _.Message); |
| 77 | + } |
| 78 | + |
| 79 | + // Let user know the instructions |
| 80 | + Console.WriteLine("==== Currently Running in Setup Mode ===="); |
| 81 | + Console.WriteLine("Setup Mode is unlocked, please re-run the exe by double-clicking."); |
| 82 | + Console.WriteLine("Don't forget to close this current instance, by pressing enter or clicking the 'X' button."); |
| 83 | + Console.WriteLine("\n\nAnother utility from 'TIRTAGT Developer' => https://github.com/TIRTAGT-DEV"); |
| 84 | + Console.ReadLine(); |
| 85 | + return; |
| 86 | + } |
| 87 | + else if (System.IO.File.Exists(ApplicationPath + @"SetupMode.TW11SDU")) |
| 88 | + { |
| 89 | + // Show the console app |
| 90 | + ShowWindow(GetConsoleWindow(), SW_SHOW); |
| 91 | + |
| 92 | + // Delete the Setup marker, to avoid looping into the setup again and again. |
| 93 | + try |
| 94 | + { |
| 95 | + System.IO.File.Delete(ApplicationPath + @"SetupMode.TW11SDU"); |
| 96 | + } |
| 97 | + catch (Exception _) |
| 98 | + { |
| 99 | + Console.WriteLine("Got exception while trying to delete setup marker, Exception message : " + _.Message); |
| 100 | + } |
| 101 | + Console.WriteLine("==== Currently Running in Setup Mode ===="); |
| 102 | + Console.WriteLine("Please make a shortcut to run this app in order to minimize your window."); |
| 103 | + Console.WriteLine("After that, close this app by clicking the 'X' icon or pressing enter."); |
| 104 | + Console.WriteLine("\n\nAnother utility from 'TIRTAGT Developer' => https://github.com/TIRTAGT-DEV"); |
| 105 | + Console.ReadLine(); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + IntPtr lHwnd = FindWindow("Shell_TrayWnd", null); |
| 110 | + |
| 111 | + // Check if the state marker file exist and we should Remember the state. |
| 112 | + if (System.IO.File.Exists(ApplicationPath + @"ReverseMode.TW11SDU") && RememberState) |
| 113 | + { |
| 114 | + // Instead of showing the desktop, show the foreground application application. |
| 115 | + SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero); |
| 116 | + |
| 117 | + // Delete the state marker, so that in the next execution, we will show the desktop. |
| 118 | + try |
| 119 | + { |
| 120 | + System.IO.File.Delete(ApplicationPath + @"ReverseMode.TW11SDU"); |
| 121 | + } |
| 122 | + catch (Exception _) |
| 123 | + { |
| 124 | + Console.WriteLine("Got exception while trying to delete state marker, Exception message : " + _.Message); |
| 125 | + } |
| 126 | + // Quit the app instantly |
| 127 | + return; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + // Show the desktop, and hide all current foreground application. |
| 132 | + SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); |
| 133 | + |
| 134 | + // If Remember state option was enabled. |
| 135 | + if (RememberState) { |
| 136 | + #pragma warning disable CS0162 // Ignore Unreachable code detected |
| 137 | + // Write a state marker so that in the next execution, we will bring all apps to foreground. |
| 138 | + try |
| 139 | + { |
| 140 | + System.IO.FileStream a = System.IO.File.Create(ApplicationPath + @"ReverseMode.TW11SDU", Encoding.ASCII.GetByteCount(StateMarkerContent), System.IO.FileOptions.RandomAccess); |
| 141 | + a.Write(Encoding.ASCII.GetBytes(StateMarkerContent), 0, Encoding.ASCII.GetByteCount(StateMarkerContent)); |
| 142 | + a.Flush(); |
| 143 | + a.Dispose(); |
| 144 | + } |
| 145 | + catch (Exception _) |
| 146 | + { |
| 147 | + Console.WriteLine("Got exception while trying to create state marker, Exception message : " + _.Message); |
| 148 | + } |
| 149 | + #pragma warning restore CS0162 // Ignore Unreachable code detected |
| 150 | + } |
| 151 | + |
| 152 | + // Quit the app instantly |
| 153 | + return; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments