첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Window 클래스 : 투명 윈도우 만들기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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에서 작동한다.

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요