■ Window 클래스를 사용해 투명 윈도우를 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStyle="None" Width="800" Height="600" AllowsTransparency="True" Background="Transparent" Title="투명 윈도우 만들기"> <Grid> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="48" FontWeight="Bold" Text="Transparent Window" /> </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 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.Runtime.InteropServices; using System.Windows; using System.Windows.Input; using System.Windows.Interop; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 컴포지션 특성 - WindowCompositionAttribute /// <summary> /// 윈도우 컴포지션 특성 /// </summary> private enum WindowCompositionAttribute { /// <summary> /// WCA_ACCENT_POLICY /// </summary> WCA_ACCENT_POLICY = 19 } #endregion #region 액센트 상태 - AccentState /// <summary> /// 액센트 상태 /// </summary> private enum AccentState { /// <summary> /// ACCENT_DISABLED /// </summary> ACCENT_DISABLED = 0, /// <summary> /// ACCENT_ENABLE_GRADIENT /// </summary> ACCENT_ENABLE_GRADIENT = 1, /// <summary> /// ACCENT_ENABLE_TRANSPARENTGRADIENT /// </summary> ACCENT_ENABLE_TRANSPARENTGRADIENT = 2, /// <summary> /// ACCENT_ENABLE_BLURBEHIND /// </summary> ACCENT_ENABLE_BLURBEHIND = 3, /// <summary> /// ACCENT_INVALID_STATE /// </summary> ACCENT_INVALID_STATE = 4 } #endregion #region 액센트 정책 - AccentPolicy /// <summary> /// 액센트 정책 /// </summary> [StructLayout(LayoutKind.Sequential)] private struct AccentPolicy { /// <summary> /// 액센트 상태 /// </summary> public AccentState AccentState; /// <summary> /// 액센트 플래그 /// </summary> public int AccentFlag; /// <summary> /// 그라디언트 색상 /// </summary> public int GradientColor; /// <summary> /// 애니메이션 ID /// </summary> public int AnimationID; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Inner Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 컴포지션 특성 데이터 - WindowCompositionAttributeData /// <summary> /// 윈도우 컴포지션 특성 데이터 /// </summary> [StructLayout(LayoutKind.Sequential)] private struct WindowCompositionAttributeData { /// <summary> /// 특성 /// </summary> public WindowCompositionAttribute Attribute; /// <summary> /// 데이터 /// </summary> public IntPtr Data; /// <summary> /// 데이터 크기 /// </summary> public int DataSize; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 컴포지션 특성 설정하기 - SetWindowCompositionAttribute(windowHandle, windowCompositionAttributeData) /// <summary> /// 윈도우 컴포지션 특성 설정하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="windowCompositionAttributeData">윈도우 컴포지션 특성 데이터</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern int SetWindowCompositionAttribute ( IntPtr windowHandle, ref WindowCompositionAttributeData windowCompositionAttributeData ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 메인 윈도우 /// </summary> public MainWindow() { InitializeComponent(); this.MouseLeftButtonDown += Window_MouseLeftButtonDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 소스 초기화시 처리하기 - OnSourceInitialized(e) /// <summary> /// 소스 초기화시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); EnableBlur(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 마우스 왼쪽 버튼 DOWN 처리하기 - Window_MouseLeftButtonDown(sender, e) /// <summary> /// 윈도우 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DragMove(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 투명 활성화 하기 - EnableBlur() /// <summary> /// 투명 활성화 하기 /// </summary> private void EnableBlur() { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this); AccentPolicy accentPolicy = new AccentPolicy(); int accentPolicySize = Marshal.SizeOf(accentPolicy); accentPolicy.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND; IntPtr accentPolicyHandle = Marshal.AllocHGlobal(accentPolicySize); Marshal.StructureToPtr(accentPolicy, accentPolicyHandle, false); WindowCompositionAttributeData windowCompositionAttributeData = new WindowCompositionAttributeData(); windowCompositionAttributeData.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY; windowCompositionAttributeData.DataSize = accentPolicySize; windowCompositionAttributeData.Data = accentPolicyHandle; SetWindowCompositionAttribute(windowInteropHelper.Handle, ref windowCompositionAttributeData); Marshal.FreeHGlobal(accentPolicyHandle); } #endregion } } |
※ 윈도우 10에서 작동한다.