■ UWP 앱 윈도우를 최소화/복원하는 방법을 보여준다.
▶ DWMWINDOWATTRIBUTE.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
namespace TestProject { /// <summary> /// DWM 윈도우 어트리뷰트 /// </summary> public enum DWMWINDOWATTRIBUTE : uint { /// <summary> /// NCRenderingEnabled /// </summary> NCRenderingEnabled = 1, /// <summary> /// NCRenderingPolicy /// </summary> NCRenderingPolicy, /// <summary> /// TransitionsForceDisabled /// </summary> TransitionsForceDisabled, /// <summary> /// AllowNCPaint /// </summary> AllowNCPaint, /// <summary> /// CaptionButtonBounds /// </summary> CaptionButtonBounds, /// <summary> /// NonClientRtlLayout /// </summary> NonClientRtlLayout, /// <summary> /// ForceIconicRepresentation /// </summary> ForceIconicRepresentation, /// <summary> /// Flip3DPolicy /// </summary> Flip3DPolicy, /// <summary> /// ExtendedFrameBounds /// </summary> ExtendedFrameBounds, /// <summary> /// HasIconicBitmap /// </summary> HasIconicBitmap, /// <summary> /// DisallowPeek /// </summary> DisallowPeek, /// <summary> /// ExcludedFromPeek /// </summary> ExcludedFromPeek, /// <summary> /// Cloak /// </summary> Cloak, /// <summary> /// Cloaked /// </summary> Cloaked, /// <summary> /// FreezeRepresentation /// </summary> FreezeRepresentation } } |
▶ WindowInformation.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; namespace TestProject { /// <summary> /// 윈도우 정보 /// </summary> public class WindowInformation { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 윈도우 핸들 - WindowHandle /// <summary> /// 윈도우 핸들 /// </summary> public IntPtr WindowHandle { get; set; } #endregion #region 윈도우 텍스트 - WindowText /// <summary> /// 윈도우 텍스트 /// </summary> public string WindowText { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"WINDOW HANDLE={WindowHandle}, WINDOW TEXT={WindowText}"; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 찾기 (확장) - FindWindowEx(parentWindowHandle, childAfterWindowHandle, className, windowText) /// <summary> /// 윈도우 찾기 (확장) /// </summary> /// <param name="parentWindowHandle">부모 윈도우 핸들</param> /// <param name="childAfterWindowHandle">찾기 이후 자식 윈도우 핸들</param> /// <param name="className">클래스명</param> /// <param name="windowText">윈도우 텍스트</param> /// <returns>윈도우 핸들</returns> [DllImport("user32.dll")] private static extern IntPtr FindWindowEx(IntPtr parentWindowHandle, IntPtr childAfterWindowHandle, string className, string windowText); #endregion #region 윈도우 스레드 프로세스 ID 구하기 - GetWindowThreadProcessId(windowHandle, processID) /// <summary> /// 윈도우 스레드 프로세스 ID 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="processID">프로세스 ID</param> /// <returns>스레드 ID</returns> [DllImport("user32")] private static extern uint GetWindowThreadProcessId(IntPtr windowHandle, out int processID); #endregion #region UWP 프로세스 여부 구하기 - IsImmersiveProcess(processHandle) /// <summary> /// UWP 프로세스 여부 구하기 /// </summary> /// <param name="processHandle">프로세스 핸들</param> /// <returns>UWP 프로세스 여부</returns> [DllImport("user32")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsImmersiveProcess(IntPtr processHandle); #endregion #region 윈도우 텍스트 구하기 - GetWindowText(windowHandle, stringBuilder, maximumCount) /// <summary> /// 윈도우 텍스트 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="stringBuilder">문자열 빌더</param> /// <param name="maximumCount">최대 카운트</param> /// <returns>윈도우 텍스트</returns> [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr windowHandle, StringBuilder stringBuilder, int maximumCount); #endregion #region 윈도우 표시 여부 구하기 - IsWindowVisible(windowHandle) /// <summary> /// 윈도우 표시 여부 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>윈도우 표시 여부</returns> [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr windowHandle); #endregion #region 윈도우 최소화 여부 구하기 - IsIconic(windowHandle) /// <summary> /// 윈도우 최소화 여부 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>윈도우 최소화 여부</returns> [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsIconic(IntPtr windowHandle); #endregion #region DWM 윈도우 어트리뷰트 구하기 - DwmGetWindowAttribute(windowHandle, attribute, value, valueSize) /// <summary> /// DWM 윈도우 어트리뷰트 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="attribute">어트리뷰트</param> /// <param name="value">값</param> /// <param name="valueSize">값 크기</param> /// <returns>처리 결과</returns> [DllImport("dwmapi.dll")] private static extern int DwmGetWindowAttribute(IntPtr windowHandle, DWMWINDOWATTRIBUTE attribute, out bool value, int valueSize); #endregion #region 윈도우 표시하기 - ShowWindow(windowHandle, command) /// <summary> /// 윈도우 표시하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="command">명령</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr windowHandle, int command); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { while(true) { List<WindowInformation> infoList = GetUWPWindowInformationList(); foreach(WindowInformation windowInformation in infoList) { Console.WriteLine(windowInformation); ShowWindow(windowInformation.WindowHandle, 6); // SW_MINIMIZE } Console.WriteLine("UWP APP WINDOWS MINIMIZED."); Thread.Sleep(3000); foreach(WindowInformation info in infoList) { Console.WriteLine(info); ShowWindow(info.WindowHandle, 9); // SW_RESTORE } Console.WriteLine("UWP APP WINDOWS RESOTRED."); Thread.Sleep(3000); } } #endregion #region UWP 윈도우 정보 리스트 구하기 - GetUWPWindowInformationList() /// <summary> /// UWP 윈도우 정보 리스트 구하기 /// </summary> /// <returns>UWP 윈도우 정보 리스트</returns> private static List<WindowInformation> GetUWPWindowInformationList() { List<WindowInformation> list = new List<WindowInformation>(); IntPtr windowHandle = IntPtr.Zero; StringBuilder windowTextStringBuilder = new StringBuilder(1024); string windowText; bool windowVisible; bool isIconic; do { windowHandle = FindWindowEx(IntPtr.Zero, windowHandle, "ApplicationFrameWindow", null); if(windowHandle == IntPtr.Zero) { break; } GetWindowText(windowHandle, windowTextStringBuilder, 1024); windowText = windowTextStringBuilder.ToString(); if(string.IsNullOrEmpty(windowText)) { continue; } windowVisible = IsWindowVisible(windowHandle); if(!windowVisible) { continue; } isIconic = IsIconic(windowHandle); if(isIconic) { continue; } DwmGetWindowAttribute(windowHandle, DWMWINDOWATTRIBUTE.Cloaked, out bool isCloacked, Marshal.SizeOf(typeof(bool))); if(isCloacked) { Console.WriteLine($"CLOAKED : {windowHandle}, {windowText}"); continue; } WindowInformation info = new WindowInformation() { WindowHandle = windowHandle, WindowText = windowText }; list.Add(info); } while(true); return list; } #endregion } } |