■ Window 클래스를 사용해 투명 윈도우를 만드는 방법을 보여준다.
▶ MARGIN.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 |
using System.Windows; namespace TestProject { /// <summary> /// 마진 /// </summary> public struct MARGIN { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 왼쪽 /// </summary> public int Left; /// <summary> /// 오른쪽 /// </summary> public int Right; /// <summary> /// 위쪽 /// </summary> public int Top; /// <summary> /// 아래쪽 /// </summary> public int Bottom; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MAGIN(thickness) /// <summary> /// 생성자 /// </summary> /// <param name="thickness">두께</param> public MARGIN(Thickness thickness) { Left = (int)thickness.Left; Right = (int)thickness.Right; Top = (int)thickness.Top; Bottom = (int)thickness.Bottom; } #endregion } } |
▶ GlassHelper.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 |
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Media; using System.Windows.Interop; namespace TestProject { /// <summary> /// 글래스 헬퍼 /// </summary> public class GlassHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Aero 기능 사용하기 - DwmExtendFrameIntoClientArea(windowHandle, margin) /// <summary> /// Aero 기능 사용하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="margin">마진</param> [DllImport("dwmapi", PreserveSig=false)] private static extern void DwmExtendFrameIntoClientArea(IntPtr windowHandle, ref MARGIN margin); #endregion #region Aero 기능 사용 가능 여부 구하기 - DwmIsCompositionEnabled() /// <summary> /// Aero 기능 사용 가능 여부 구하기 /// </summary> /// <returns>Aero 기능 사용 가능 여부</returns> [DllImport("dwmapi", PreserveSig=false)] private static extern bool DwmIsCompositionEnabled(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 글래스 프레임 확장하기 - ExtendGlassFrame(window, marginThickness) /// <summary> /// 글래스 프레임 확장하기 /// </summary> /// <param name="window">윈도우</param> /// <param name="marginThickness">마진 두께</param> /// <returns>처리 결과</returns> public static bool ExtendGlassFrame(Window window, Thickness marginThickness) { if(!DwmIsCompositionEnabled()) { return false; } IntPtr windowHandle = new WindowInteropHelper(window).Handle; if(windowHandle == IntPtr.Zero) { throw new InvalidOperationException("글래스 확장 전 윈도우가 표시되어야 합니다."); } window.Background = Brushes.Transparent; HwndSource.FromHwnd(windowHandle).CompositionTarget.BackgroundColor = Colors.Transparent; MARGIN margins = new MARGIN(marginThickness); DwmExtendFrameIntoClientArea(windowHandle, ref margins); return true; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="투명 윈도우 만들기"> </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 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 소스 초기화시 처리하기 - OnSourceInitialized(e) /// <summary> /// 소스 초기화시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); GlassHelper.ExtendGlassFrame(this, new Thickness(-1)); } #endregion } } |
※ 윈도우 7에서 작동되었으나 윈도우 10에서는 작동되지 않았다.