■ 반투명 윈도우 사용하기

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


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"

    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

 

 

using System;

using System.Runtime.InteropServices;

using System.Windows;

using System.Windows.Interop;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #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

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Loaded                 += Window_Loaded;

            this.closeButton.Click += closeButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            ApplyGlassEffect(this);

        }

 

        #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

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 글래스 효과 적용하기 - ApplyGlassEffect(window)

 

        /// <summary>

        /// 글래스 효과 적용하기

        /// </summary>

        /// <param name="window">윈도우</param>

        private void ApplyGlassEffect(Window window)

        {

            WindowInteropHelper helper = new WindowInteropHelper(window);

            

            AccentPolicy accentPolicy = new AccentPolicy();

 

            accentPolicy.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;

 

            int accentPolicySize = Marshal.SizeOf(accentPolicy);

 

            IntPtr memoryHandle = Marshal.AllocHGlobal(accentPolicySize);

 

            Marshal.StructureToPtr(accentPolicy, memoryHandle, false);

 

            WindowCompositionAttributeData data = new WindowCompositionAttributeData();

 

            data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;

            data.Data      = memoryHandle;

            data.DataSize  = accentPolicySize;

 

            SetWindowCompositionAttribute(helper.Handle, ref data);

 

            Marshal.FreeHGlobal(memoryHandle);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG