■ 반투명 윈도우를 사용하는 방법을 보여준다.
▶ AccentState.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 |
namespace TestProject { /// <summary> /// 강조 상태 /// </summary> public enum AccentState { /// <summary> /// ACCENT_DISABLED /// </summary> ACCENT_DISABLED, /// <summary> /// ACCENT_ENABLE_GRADIENT /// </summary> ACCENT_ENABLE_GRADIENT, /// <summary> /// ACCENT_ENABLE_TRANSPARENTGRADIENT /// </summary> ACCENT_ENABLE_TRANSPARENTGRADIENT, /// <summary> /// ACCENT_ENABLE_BLURBEHIND /// </summary> ACCENT_ENABLE_BLURBEHIND, /// <summary> /// ACCENT_INVALID_STATE /// </summary> ACCENT_INVALID_STATE } } |
▶ AccentPolicy.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 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 강조 정책 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct AccentPolicy { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 강조 상태 /// </summary> public AccentState AccentState; /// <summary> /// 강조 플래그 /// </summary> public int AccentFlag; /// <summary> /// 그라디언트 색상 /// </summary> public int GradientColor; /// <summary> /// 애니메이션 ID /// </summary> public int AnimationID; #endregion } } |
▶ WindowCompositionAttribute.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace TestProject { /// <summary> /// 윈도우 구성 특성 /// </summary> public enum WindowCompositionAttribute { /// <summary> /// WCA_ACCENT_POLICY /// </summary> WCA_ACCENT_POLICY = 19 } } |
▶ WindowCompositionAttributeData.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 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 윈도우 구성 특성 데이터 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct WindowCompositionAttributeData { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 윈도우 구성 특성 /// </summary> public WindowCompositionAttribute Attribute; /// <summary> /// 데이터 /// </summary> public IntPtr Data; /// <summary> /// 데이터 크기 /// </summary> public int DataSize; #endregion } } |
▶ WindowBlurHelper.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 |
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace TestProject { /// <summary> /// 윈도우 반투명 헬퍼 /// </summary> public class WindowBlurHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static #region 윈도우 구성 특성 설정하기 - SetWindowCompositionAttribute(windowHandle, data) /// <summary> /// 윈도우 구성 특성 설정하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="data">데이터</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern int SetWindowCompositionAttribute(IntPtr windowHandle, ref WindowCompositionAttributeData data); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 이용 가능 여부 속성 /// </summary> public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached ( "IsEnabled", typeof(bool), typeof(WindowBlurHelper), new PropertyMetadata(false, IsEnabledPropertyChangedCallback) ); /// <summary> /// 윈도우 반투명 헬퍼 속성 /// </summary> public static readonly DependencyProperty WindowBlurHelperProperty = DependencyProperty.RegisterAttached ( "WindowBlurHelper", typeof(WindowBlurHelper), typeof(WindowBlurHelper), new PropertyMetadata(null, WindowBlurHelperPropertyChangedCallback)); #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 윈도우 /// </summary> private Window window; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 이용 가능 여부 설정하기 - SetIsEnabled(dependencyObject, value) /// <summary> /// 이용 가능 여부 설정하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <param name="value">값</param> public static void SetIsEnabled(DependencyObject dependencyObject, bool value) { dependencyObject.SetValue(IsEnabledProperty, value); } #endregion #region 이용 가능 여부 구하기 - GetIsEnabled(dependencyObject) /// <summary> /// 이용 가능 여부 구하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <returns>이용 가능 여부</returns> public static bool GetIsEnabled(DependencyObject dependencyObject) { return (bool)dependencyObject.GetValue(IsEnabledProperty); } #endregion #region 윈도우 반투명 헬퍼 설정하기 - SetWindowBlurHelper(dependencyObject, value) /// <summary> /// 윈도우 반투명 헬퍼 설정하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <param name="value">값</param> public static void SetWindowBlurHelper(DependencyObject dependencyObject, WindowBlurHelper value) { dependencyObject.SetValue(WindowBlurHelperProperty, value); } #endregion #region 윈도우 반투명 헬퍼 구하기 - GetWindowBlurHelper(dependencyObject) /// <summary> /// 윈도우 반투명 헬퍼 구하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <returns>윈도우 반투명 헬퍼</returns> public static WindowBlurHelper GetWindowBlurHelper(DependencyObject dependencyObject) { return (WindowBlurHelper)dependencyObject.GetValue(WindowBlurHelperProperty); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 이용 가능 여부 속성 변경시 콜백 처리하기 - IsEnabledPropertyChangedCallback(dependencyObject, e) /// <summary> /// 이용 가능 여부 속성 변경시 콜백 처리하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void IsEnabledPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { if(dependencyObject is Window window) { if(true.Equals(e.OldValue)) { GetWindowBlurHelper(window)?.Detach(); window.ClearValue(WindowBlurHelperProperty); } if(true.Equals(e.NewValue)) { WindowBlurHelper helper = new WindowBlurHelper(); helper.Attach(window); window.SetValue(WindowBlurHelperProperty, helper); } } } #endregion #region 윈도우 반투명 헬퍼 속성 변경시 콜백 처리하기 - WindowBlurHelperPropertyChangedCallback(dependencyObject, e) /// <summary> /// 윈도우 반투명 헬퍼 속성 변경시 콜백 처리하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void WindowBlurHelperPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { if(dependencyObject is Window window) { (e.OldValue as WindowBlurHelper)?.Detach(); (e.NewValue as WindowBlurHelper)?.Attach(window); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Event #region 윈도우 소스 초기화시 처리하기 - window_SourceInitialized(sender, e) /// <summary> /// 윈도우 소스 초기화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void window_SourceInitialized(object sender, EventArgs e) { ((Window)sender).SourceInitialized -= window_SourceInitialized; AttachCore(); } #endregion ////////////////////////////////////////////////////////////////////// Function #region 반투명 이용 가능 여부 설정하기 - EnableBlur(window) /// <summary> /// 반투명 이용 가능 여부 설정하기 /// </summary> /// <param name="window">윈도우</param> private static void EnableBlur(Window window) { WindowInteropHelper helper = new WindowInteropHelper(window); AccentPolicy accentPolicy = new AccentPolicy { AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND }; int accentPolicySize = Marshal.SizeOf(accentPolicy); IntPtr accentPolicyHandle = Marshal.AllocHGlobal(accentPolicySize); Marshal.StructureToPtr(accentPolicy, accentPolicyHandle, false); WindowCompositionAttributeData data = new WindowCompositionAttributeData { Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY, DataSize = accentPolicySize, Data = accentPolicyHandle }; SetWindowCompositionAttribute(helper.Handle, ref data); Marshal.FreeHGlobal(accentPolicyHandle); } #endregion #region 첨부하기 (코어) - AttachCore() /// <summary> /// 첨부하기 (코어) /// </summary> private void AttachCore() { EnableBlur(this.window); } #endregion #region 분리하기 (코어) - DetachCore() /// <summary> /// 분리하기 (코어) /// </summary> private void DetachCore() { this.window.SourceInitialized += window_SourceInitialized; } #endregion #region 첨부하기 - Attach(window) /// <summary> /// 첨부하기 /// </summary> /// <param name="window">윈도우</param> private void Attach(Window window) { this.window = window; HwndSource source = PresentationSource.FromVisual(window) as HwndSource; if(source == null) { window.SourceInitialized += window_SourceInitialized; } else { AttachCore(); } } #endregion #region 분리하기 - Detach() /// <summary> /// 분리하기 /// </summary> private void Detach() { try { DetachCore(); } finally { this.window = null; } } #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 28 29 30 31 32 33 34 35 36 37 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen" Width="800" Height="600" WindowStyle="None" AllowsTransparency="True" BorderThickness="1" BorderBrush="Black" Background="#12000000" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" Foreground="RoyalBlue"> 반투명 윈도우 사용하기 </TextBlock> <Button Name="closeButton" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" 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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.closeButton.Click += closeButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { WindowBlurHelper.SetIsEnabled(this, true); } #endregion #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 } } |