■ 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
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 |
<?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"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button Name="createWindowWithCustomizableMicaButton" HorizontalAlignment="Center" Margin="0 10 0 0" Padding="10" Content="Create Window with customizable Mica" /> <Button Name="createWindowWithBuiltInAcrylicButton" HorizontalAlignment="Center" Margin="0 10 0 0" Padding="10" Content="Create Window with built-in Acrylic" /> <Button Name="createWindowWithCustomizableDesktopAcrylicButton" HorizontalAlignment="Center" Margin="0 10 0 0" Padding="10" Content="Create Window with customizable Desktop Acrylic" /> </StackPanel> </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 80 81 82 83 84 85 86 |
using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); createWindowWithCustomizableMicaButton.Click += createWindowWithCustomizableMicaButton_Click; createWindowWithBuiltInAcrylicButton.Click += createWindowWithBuiltInAcrylicButton_Click; createWindowWithCustomizableDesktopAcrylicButton.Click += createWindowWithCustomizableDesktopAcrylicButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Create Window with customizable Mica 버튼 클릭시 처리하기 - createWindowWithCustomizableMicaButton_Click(sender, e) /// <summary> /// Create Window with customizable Mica 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void createWindowWithCustomizableMicaButton_Click(object sender, RoutedEventArgs e) { SystemBackdropsWindow window = new SystemBackdropsWindow(); window.SetBackdrop(SystemBackdropsWindow.BackdropType.Mica); window.Activate(); } #endregion #region Create Window with built-in Acrylic 버튼 클릭시 처리하기 - createWindowWithBuiltInAcrylicButton_Click(sender, e) /// <summary> /// Create Window with built-in Acrylic 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void createWindowWithBuiltInAcrylicButton_Click(object sender, RoutedEventArgs e) { SystemBackdropsWindow window = new SystemBackdropsWindow(); window.SetBackdrop(SystemBackdropsWindow.BackdropType.DesktopAcrylicBase); window.Activate(); } #endregion #region Create Window with customizable Desktop Acrylic 버튼 클릭시 처리하기 - createWindowWithCustomizableDesktopAcrylicButton_Click(sender, e) /// <summary> /// Create Window with customizable Desktop Acrylic 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void createWindowWithCustomizableDesktopAcrylicButton_Click(object sender, RoutedEventArgs e) { SystemBackdropsWindow window = new SystemBackdropsWindow(); window.SetBackdrop(SystemBackdropsWindow.BackdropType.DesktopAcrylicBase); window.Activate(); } #endregion } } |