[C#/WINUI3/.NET8] AppWindow 클래스 : Create 정적 메소드를 사용해 윈도우 만들기
■ AppWindow 클래스의 Create 정적 메소드를 사용해 윈도우를 만드는 방법을 보여준다. ※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다. ※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType
■ AppWindow 클래스의 Create 정적 메소드를 사용해 윈도우를 만드는 방법을 보여준다. ※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다. ※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType
■ AppWindow 클래스의 Changed 이벤트를 사용해 윈도우 변경시 처리하는 방법을 보여준다. ※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다. ※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType
■ WindowEx 클래스의 ShowMessageDialogAsync 메소드를 사용해 메시지 대화 상자를 표시하는 방법을 보여준다. ※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다. ※ TestProject.csproj 프로젝트 파일에서
■ AppWindow 클래스의 SetIcon 메소드를 사용해 아이콘을 설정하는 방법을 보여준다. ▶ 예제 코드 (C#)
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 |
using System; using System.IO; using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); string iconFilePath = Path.Combine(AppContext.BaseDirectory, "Assets/WindowIcon.ico"); appWindow.SetIcon(iconFilePath); |
■ AppWindow 클래스의 Move 메소드를 사용해 윈도우를 이동시키는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using Windows.Graphics; using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); appWindow.Move(new PointInt32(100, 200)); |
■ AppWindow 클래스의 Resize 메소드를 사용해 윈도우 크기를 설정하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using Windows.Graphics; using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); appWindow.Resize(new SizeInt32 { Width = 800, Height = 600 }); |
■ AppWindow 클래스의 Title 속성을 사용해 윈도우 제목을 설정하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); appWindow.Title = "TestProject"; |
■ AppWindow 클래스의 SetPresenter 메소드를 사용해 윈도우 전체 모드를 설정하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); appWindow.SetPresenter(AppWindowPresenterKind.FullScreen); |
■ AppWindow 클래스의 Presenter 속성을 사용해 OverlappedPresenter 객체를 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; |
■ AppWindow 클래스의 GetFromWindowId 정적 메소드를 사용해 WindowId 객체에서 AppWindow 객체를 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); |
■ Win32Interop 클래스의 GetWindowIdFromWindow 정적 메소드를 사용해 윈도우 핸들에서 WindowId 객체를 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using WinRT.Interop; using Microsoft.UI; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); |
■ WindowNative 클래스의 GetWindowHandle 정적 메소드를 사용해 윈도우 핸들을 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using WinRT.Interop; using Microsoft.UI.Xaml; // ... Window window; // ... nint windowHandle = WindowNative.GetWindowHandle(window); |
■ Window 엘리먼트의 SystemBackdrop 속성을 사용해 시스템 백도롭을 설정하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Window.SystemBackdrop> <MicaBackdrop /> </Window.SystemBackdrop> </Window> |
■ Window 클래스에서 MicaController/DesktopAcrylicController 객체를 사용해 시스템 백드롭을 설정하는 방법을 보여준다. ▶ WindowsSystemDispatcherQueueHelper.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 |
using System; using System.Runtime.InteropServices; using Windows.System; namespace TestProject { /// <summary> /// 윈도우즈 시스템 디스패처 큐 헬퍼 /// </summary> public class WindowsSystemDispatcherQueueHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 디스패처 큐 옵션 - DispatcherQueueOptions /// <summary> /// 디스패처 큐 옵션 /// </summary> [StructLayout(LayoutKind.Sequential)] private struct DispatcherQueueOptions { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Internal #region Field /// <summary> /// 크기 /// </summary> internal int Size; /// <summary> /// 쓰레드 타입 /// </summary> internal int ThreadType; /// <summary> /// 아파트먼트 타입 /// </summary> internal int ApartmentType; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 디스패처 큐 컨트롤러 생성하기 - CreateDispatcherQueueController(options, instance) /// <summary> /// 디스패처 큐 컨트롤러 생성하기 /// </summary> /// <param name="options">디스패처 큐 옵션</param> /// <param name="instance">인스턴스</param> /// <returns>처리 결과</returns> [DllImport("CoreMessaging")] private static unsafe extern int CreateDispatcherQueueController(DispatcherQueueOptions options, IntPtr* instance); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디스패처 큐 컨트롤러 핸들 /// </summary> private IntPtr dispatcherQueueControllerHandle = IntPtr.Zero; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우즈 시스템 디스패처 큐 컨트롤러 보장하기 - EnsureWindowsSystemDispatcherQueueController() /// <summary> /// 윈도우즈 시스템 디스패처 큐 컨트롤러 보장하기 /// </summary> public void EnsureWindowsSystemDispatcherQueueController() { if(DispatcherQueue.GetForCurrentThread() != null) { return; } if(this.dispatcherQueueControllerHandle == IntPtr.Zero) { DispatcherQueueOptions options; options.Size = Marshal.SizeOf(typeof(DispatcherQueueOptions)); options.ThreadType = 2; // DQTYPE_THREAD_CURRENT options.ApartmentType = 2; // DQTAT_COM_STA unsafe { IntPtr dispatcherQueueController; CreateDispatcherQueueController(options, &dispatcherQueueController); this.dispatcherQueueControllerHandle = dispatcherQueueController; } } } #endregion } } |
▶ SystemBackdropsWindow.xaml
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 |
<Window x:Class="TestProject.SystemBackdropsWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="32" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid Name="titleBarGrid" Grid.Row="0" Background="Transparent" /> <StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="12"> <TextBlock HorizontalAlignment="Center"> <Run Text="Current backdrop : " /> <Run x:Name="currentBackdropRun" FontWeight="SemiBold" /> </TextBlock> <Button Name="changeBackdropButton" HorizontalAlignment="Center" Content="Change Backdrop" /> <TextBlock Name="changeStatusTextBlock" HorizontalAlignment="Center" /> </StackPanel> </Grid> </Window> |
▶ SystemBackdropsWindow.xaml.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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
using WinRT; using Microsoft.UI.Xaml; using Microsoft.UI.Composition; using Microsoft.UI.Composition.SystemBackdrops; namespace TestProject { /// <summary> /// 시스템 백드롭 윈도우 /// </summary> public sealed partial class SystemBackdropsWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Public #region 백드롭 타입 - BackdropType /// <summary> /// 백드롭 타입 /// </summary> public enum BackdropType { /// <summary> /// Mica /// </summary> Mica, /// <summary> /// MicaAlt /// </summary> MicaAlt, /// <summary> /// DesktopAcrylicBase /// </summary> DesktopAcrylicBase, /// <summary> /// DesktopAcrylicThin /// </summary> DesktopAcrylicThin, /// <summary> /// DefaultColor /// </summary> DefaultColor, } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 윈도우즈 시스템 디스패처 큐 헬퍼 /// </summary> private WindowsSystemDispatcherQueueHelper windowsSystemDispatcherQueueHelper; /// <summary> /// 현재 백드롭 타입 /// </summary> private BackdropType currentBackdropType; /// <summary> /// 미카 컨트롤러 /// </summary> private MicaController micaController; /// <summary> /// 데스크톱 아크릴 컨트롤러 /// </summary> private DesktopAcrylicController desktopAcrylicController; /// <summary> /// 시스템 백드롭 구성 /// </summary> private SystemBackdropConfiguration systemBackdropConfiguration; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SystemBackdropsWindow() /// <summary> /// 생성자 /// </summary> public SystemBackdropsWindow() { InitializeComponent(); ((FrameworkElement)Content).RequestedTheme = ElementTheme.Default; ExtendsContentIntoTitleBar = true; SetTitleBar(this.titleBarGrid); this.windowsSystemDispatcherQueueHelper = new WindowsSystemDispatcherQueueHelper(); this.windowsSystemDispatcherQueueHelper.EnsureWindowsSystemDispatcherQueueController(); SetBackdrop(BackdropType.Mica); this.changeBackdropButton.Click += changeBackdropButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 백드롭 설정하기 - SetBackdrop(backdropType) /// <summary> /// 백드롭 설정하기 /// </summary> /// <param name="backdropType"></param> public void SetBackdrop(BackdropType backdropType) { this.currentBackdropType = BackdropType.DefaultColor; this.currentBackdropRun.Text = "None (default theme color)"; this.changeStatusTextBlock.Text = ""; if(this.micaController != null) { this.micaController.Dispose(); this.micaController = null; } if(this.desktopAcrylicController != null) { this.desktopAcrylicController.Dispose(); this.desktopAcrylicController = null; } Activated -= Window_Activated; Closed -= Window_Closed; ((FrameworkElement)Content).ActualThemeChanged -= Window_ActualThemeChanged; this.systemBackdropConfiguration = null; if(backdropType == BackdropType.Mica) { if(TrySetMicaBackdrop(false)) { this.currentBackdropRun.Text = "Custom Mica"; this.currentBackdropType = backdropType; } else { backdropType = BackdropType.DesktopAcrylicBase; this.changeStatusTextBlock.Text += " Mica isn't supported. Trying Acrylic."; } } if(backdropType == BackdropType.MicaAlt) { if(TrySetMicaBackdrop(true)) { this.currentBackdropRun.Text = "Custom MicaAlt"; this.currentBackdropType = backdropType; } else { backdropType = BackdropType.DesktopAcrylicBase; this.changeStatusTextBlock.Text += " MicaAlt isn't supported. Trying Acrylic."; } } if(backdropType == BackdropType.DesktopAcrylicBase) { if(TrySetAcrylicBackdrop(false)) { this.currentBackdropRun.Text = "Custom Acrylic (Base)"; this.currentBackdropType = backdropType; } else { this.changeStatusTextBlock.Text += " Acrylic Base isn't supported. Switching to default color."; } } if(backdropType == BackdropType.DesktopAcrylicThin) { if(TrySetAcrylicBackdrop(true)) { this.currentBackdropRun.Text = "Custom Acrylic (Thin)"; this.currentBackdropType = backdropType; } else { this.changeStatusTextBlock.Text += " Acrylic Thin isn't supported. Switching to default color."; } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 활성화시 처리하기 - Window_Activated(sender, e) /// <summary> /// 윈도우 활성화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Activated(object sender, WindowActivatedEventArgs e) { this.systemBackdropConfiguration.IsInputActive = e.WindowActivationState != WindowActivationState.Deactivated; } #endregion #region 윈도우 닫는 경우 처리하기 - Window_Closed(sender, e) /// <summary> /// 윈도우 닫는 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Closed(object sender, WindowEventArgs e) { if(this.micaController != null) { this.micaController.Dispose(); this.micaController = null; } if(this.desktopAcrylicController != null) { this.desktopAcrylicController.Dispose(); this.desktopAcrylicController = null; } Activated -= Window_Activated; this.systemBackdropConfiguration = null; } #endregion #region 윈도우 실제 테마 변경시 처리하기 - Window_ActualThemeChanged(sender, e) /// <summary> /// 윈도우 실제 테마 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_ActualThemeChanged(FrameworkElement sender, object e) { if(this.systemBackdropConfiguration != null) { SetConfigurationSourceTheme(); } } #endregion #region Change Backdrop 버튼 클릭시 처리하기 - changeBackdropButton_Click(sender, e) /// <summary> /// Change Backdrop 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void changeBackdropButton_Click(object sender, RoutedEventArgs e) { BackdropType backdropType; switch(this.currentBackdropType) { case BackdropType.Mica : backdropType = BackdropType.MicaAlt; break; case BackdropType.MicaAlt : backdropType = BackdropType.DesktopAcrylicBase; break; case BackdropType.DesktopAcrylicBase : backdropType = BackdropType.DesktopAcrylicThin; break; case BackdropType.DesktopAcrylicThin : backdropType = BackdropType.DefaultColor; break; default : case BackdropType.DefaultColor : backdropType = BackdropType.Mica; break; } SetBackdrop(backdropType); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 구성 소스 테마 설정하기 - SetConfigurationSourceTheme() /// <summary> /// 구성 소스 테마 설정하기 /// </summary> private void SetConfigurationSourceTheme() { switch(((FrameworkElement)this.Content).ActualTheme) { case ElementTheme.Dark : this.systemBackdropConfiguration.Theme = SystemBackdropTheme.Dark; break; case ElementTheme.Light : this.systemBackdropConfiguration.Theme = SystemBackdropTheme.Light; break; case ElementTheme.Default : this.systemBackdropConfiguration.Theme = SystemBackdropTheme.Default; break; } } #endregion #region Mica 백드롭 설정 시도하기 - TrySetMicaBackdrop(useMicaAlt) /// <summary> /// Mica 백드롭 설정 시도하기 /// </summary> /// <param name="useMicaAlt">Mica 대체 사용 여부</param> /// <returns>처리 결과</returns> private bool TrySetMicaBackdrop(bool useMicaAlt) { if(MicaController.IsSupported()) { this.systemBackdropConfiguration = new SystemBackdropConfiguration(); Activated += Window_Activated; Closed += Window_Closed; ((FrameworkElement)Content).ActualThemeChanged += Window_ActualThemeChanged; this.systemBackdropConfiguration.IsInputActive = true; SetConfigurationSourceTheme(); this.micaController = new MicaController(); this.micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; this.micaController.AddSystemBackdropTarget(this.As<ICompositionSupportsSystemBackdrop>()); this.micaController.SetSystemBackdropConfiguration(this.systemBackdropConfiguration); return true; } return false; } #endregion #region 아크릴 백드롭 설정 시도하기 - TrySetAcrylicBackdrop(useAcrylicThin) /// <summary> /// 아크릴 백드롭 설정 시도하기 /// </summary> /// <param name="useAcrylicThin">가는 아크릴 사용 여부</param> /// <returns>처리 결과</returns> bool TrySetAcrylicBackdrop(bool useAcrylicThin) { if(DesktopAcrylicController.IsSupported()) { this.systemBackdropConfiguration = new SystemBackdropConfiguration(); Activated += Window_Activated; Closed += Window_Closed; ((FrameworkElement)Content).ActualThemeChanged += Window_ActualThemeChanged; this.systemBackdropConfiguration.IsInputActive = true; SetConfigurationSourceTheme(); this.desktopAcrylicController = new DesktopAcrylicController(); this.desktopAcrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; this.desktopAcrylicController.AddSystemBackdropTarget(this.As<ICompositionSupportsSystemBackdrop>()); this.desktopAcrylicController.SetSystemBackdropConfiguration(this.systemBackdropConfiguration); return true; } return false; } #endregion } } |
▶ MainWindow.xaml
■ Window 클래스의 SystemBackdrop 속성을 사용하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> </Window> |
▶ MainWindow.xaml.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 |
using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); if(MicaController.IsSupported()) { SystemBackdrop = new MicaBackdrop(); } else if(DesktopAcrylicController.IsSupported()) { SystemBackdrop = new DesktopAcrylicBackdrop(); } } #endregion } } |
TestProject.zip
■ Window 클래스의 ExtendsContentIntoTitleBar 속성/SetTitleBar 메소드를 사용해 컨텐트를 타이틀바까지 확장하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="32" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid Name="titleGrid" Grid.Row="0" Background="Red" /> <Ellipse Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="30" Fill="RoyalBlue" /> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ExtendsContentIntoTitleBar = true; SetTitleBar(this.titleGrid); } #endregion } } |
TestProject.zip
■ Window 클래스에서 전체 모드를 설정하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Grid> <Button Name="closeButton" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Width="100" Height="30" Content="닫기" /> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using System; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AppWindow appWindow = GetAppWindow(this); OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; appWindow.SetPresenter(AppWindowPresenterKind.FullScreen); // 전체 화면 모드 this.closeButton.Click += closeButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 닫기 버튼 클릭시 처리하기 - closeButton_Click(sender, e) /// <summary> /// 닫기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeButton_Click(object sender, RoutedEventArgs e) { Close(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 앱 윈도우 구하기 - GetAppWindow(window) /// <summary> /// 앱 윈도우 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>앱 윈도우</returns> private AppWindow GetAppWindow(Window window) { IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); return appWindow; } #endregion } } |
TestProject.zip
■ Window 클래스에서 윈도우 크기를 최대화하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> </Window> |
▶ MainWindow.xaml.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 |
using System; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AppWindow appWindow = GetAppWindow(this); OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; presenter.Maximize(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 앱 윈도우 구하기 - GetAppWindow(window) /// <summary> /// 앱 윈도우 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>앱 윈도우</returns> private AppWindow GetAppWindow(Window window) { IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); return appWindow; } #endregion } } |
TestProject.zip
■ Window 클래스에서 최소화/최대화/크기 조정 기능을 비활성화하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> </Window> |
▶ MainWindow.xaml.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 |
using System; using Windows.Graphics; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AppWindow appWindow = GetAppWindow(this); appWindow.Resize(new SizeInt32 { Width = 800, Height = 600 }); // 윈도우 크기 appWindow.Move(new PointInt32(100, 200)); // 윈도우 위치 OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; presenter.IsMinimizable = false; // 최소화 가능 여부 presenter.IsMaximizable = false; // 최대화 가능 여부 presenter.IsResizable = false; // 크기 조정 가능 여부 } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 앱 윈도우 구하기 - GetAppWindow(window) /// <summary> /// 앱 윈도우 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>앱 윈도우</returns> private AppWindow GetAppWindow(Window window) { IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); return appWindow; } #endregion } } |
TestProject.zip
■ Window 클래스에서 최상위 윈도우를 설정하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> </Window> |
TestProject.zip
■ Window 클래스에서 윈도우 제목/크기/위치를 설정하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> </Window> |
▶ MainWindow.xaml.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 |
using System; using Windows.Graphics; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AppWindow appWindow = GetAppWindow(this); appWindow.Title = "TestProject"; // 윈도우 제목 appWindow.Resize(new SizeInt32 { Width = 800, Height = 600 }); // 윈도우 크기 appWindow.Move(new PointInt32(100, 200)); // 윈도우 위치 } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 앱 윈도우 구하기 - GetAppWindow(window) /// <summary> /// 앱 윈도우 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>앱 윈도우</returns> private AppWindow GetAppWindow(Window window) { IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowID); return appWindow; } #endregion } } |
TestProject.zip
■ Window 클래스를 사용해 AppWindow 객체 구하기 ▶ 예제 코드 (C#)
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 |
using System; using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using WinRT.Interop; #region 앱 윈도우 구하기 - GetAppWindow(window) /// <summary> /// 앱 윈도우 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>앱 윈도우</returns> public AppWindow GetAppWindow(Window window) { IntPtr windowHandle = WindowNative.GetWindowHandle(window); WindowId windowID = Win32Interop.GetWindowIdFromWindow(windowHandle); return AppWindow.GetFromWindowId(windowID); } #endregion |
■ AppWindow 클래스의 Resize 메소드를 사용해 윈도우 크기를 설정하는 방법을 보여준다. ▶ App.xaml.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 |
using Windows.Graphics; using Microsoft.UI.Xaml; namespace TestProject; /// <summary> /// 앱 /// </summary> public partial class App : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 윈도우 /// </summary> private Window window; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - App() /// <summary> /// 생성자 /// </summary> public App() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnLaunched(args) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="args">이벤트 인자</param> protected override void OnLaunched(LaunchActivatedEventArgs args) { this.window = new MainWindow(); this.window.AppWindow.Resize(new SizeInt32(800, 600)); this.window.Activate(); } #endregion } |
TestProject.zip
■ 모든 UWP 애플리케이션의 윈도우를 최소화하는 방법을 보여준다. ▶ 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 |
using System; using System.Runtime.InteropServices; 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 윈도우 표시하기 (비동기) - ShowWindowAsync(windowHandle, command) /// <summary> /// 윈도우 표시하기 (비동기) /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="command">명령</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool ShowWindowAsync(IntPtr windowHandle, int command); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { IntPtr windowHandle = IntPtr.Zero; do { windowHandle = FindWindowEx(IntPtr.Zero, windowHandle, "ApplicationFrameWindow", null); if(windowHandle == IntPtr.Zero) { break; } ShowWindowAsync(windowHandle, 6); } while(true); } #endregion } } |
TestProject.zip
■ 오픈 윈도우 핸들 딕셔너리를 구하는 방법을 보여준다. ▶ WindowHandleHelper.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 |
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace TestProject { /// <summary> /// 윈도우 핸들 헬퍼 /// </summary> public static class WindowHandleHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 열거하기 대리자 - EnumWindowsProc(windowHandle, longParameter) /// <summary> /// 윈도우 열거하기 대리자 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private delegate bool EnumWindowsProc(IntPtr windowHandle, int longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 쉘 윈도우 구하기 - GetShellWindow() /// <summary> /// 쉘 윈도우 구하기 /// </summary> /// <returns>쉘 윈도우 핸들</returns> [DllImport("user32.dll")] private static extern IntPtr GetShellWindow(); #endregion #region 윈도우 열거하기 - EnumWindows(enumWindowsProc, longParameter) /// <summary> /// 윈도우 열거하기 /// </summary> /// <param name="enumWindowsProc">윈도우 열거하기 대리자</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc enumWindowsProc, int longParameter); #endregion #region 윈도우 표시 여부 구하기 - IsWindowVisible(windowHandle) /// <summary> /// 윈도우 표시 여부 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>윈도우 표시 여부</returns> [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr windowHandle); #endregion #region 윈도우 텍스트 길이 구하기 - GetWindowTextLength(windowHandle) /// <summary> /// 윈도우 텍스트 길이 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>윈도우 텍스트 길이</returns> [DllImport("user32.dll")] private static extern int GetWindowTextLength(IntPtr windowHandle); #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 //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 오픈 윈도우 핸들 딕셔너리 구하기 - GetOpenWindowHandleDictionary() /// <summary> /// 오픈 윈도우 핸들 딕셔너리 구하기 /// </summary> /// <returns>윈도우 핸들 딕셔너리</returns> public static Dictionary<IntPtr, string> GetOpenWindowHandleDictionary() { IntPtr shellWindowHandle = GetShellWindow(); Dictionary<IntPtr, string> windowHandleDictionary = new Dictionary<IntPtr, string>(); EnumWindows ( delegate(IntPtr windowHandle, int longParameter) { if(windowHandle == shellWindowHandle) { return true; } if(!IsWindowVisible(windowHandle)) { return true; } int windowTextLength = GetWindowTextLength(windowHandle); if(windowTextLength == 0) { return true; } StringBuilder stringBuilder = new StringBuilder(windowTextLength); GetWindowText(windowHandle, stringBuilder, windowTextLength + 1); windowHandleDictionary[windowHandle] = stringBuilder.ToString(); return true; }, 0 ); return windowHandleDictionary; } #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 |
using System; using System.Collections.Generic; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Dictionary<IntPtr, string> dictionary = WindowHandleHelper.GetOpenWindowHandleDictionary(); foreach(KeyValuePair<IntPtr, string> keyValuePair in dictionary) { IntPtr windowHandle = keyValuePair.Key; string windowText = keyValuePair.Value; Console.WriteLine($"{windowText} : {windowHandle}"); } } #endregion } } |
TestProject.zip