첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ SystemColors 클래스를 사용해 시스템 색상을 표시하는 방법을 보여준다.

TestProject.zip
다운로드

▶ SystemColorItem.cs

using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 시스템 색상 항목
    /// </summary>
    public class SystemColorItem
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 명칭 - Name

        /// <summary>
        /// 명칭
        /// </summary>
        public string Name { get; set; }

        #endregion
        #region 색성 - Color

        /// <summary>
        /// 색상
        /// </summary>
        public Color Color { get; set; }

        #endregion
        #region 반전 색상 - InvertColor

        /// <summary>
        /// 반전 색상
        /// </summary>
        public Color InvertColor
        {
            get
            {
                return Color.FromArgb(0xff, (byte)(255 - Color.R), (byte)(255 - Color.G), (byte)(255 - Color.B));
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 문자열 구하기 - ToString()

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <returns>문자열</returns>
        public override string ToString()
        {
            return Name;
        }

        #endregion
    }
}

 

▶ 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"
    Width="800"
    Height="600"
    Title="SystemColors 클래스를 사용해 시스템 색상 표시하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <ScrollViewer>
            <WrapPanel Name="wrapPanel"
                Background="{StaticResource {x:Static SystemColors.ActiveBorderBrushKey}}"
                ItemWidth="200"
                ItemHeight="100" />
        </ScrollViewer>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 시스템 색상 아이템 리스트
        /// </summary>
        private List<SystemColorItem> systemColorItemList = new List<SystemColorItem>();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 시스템 색상 아이템 리스트 - SystemColorItemList

        /// <summary>
        /// 시스템 색상 아이템 리스트
        /// </summary>
        public List<SystemColorItem> SystemColorItemList
        {
            get
            {
                return this.systemColorItemList;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded += Window_Loaded;
        }

        #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)
        {
            SetSystemColorItemList();

            AddWrapPanelData();
        }

        #endregion

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

        #region 시스템 색상 아이템 리스트 설정하기 - SetSystemColorItemList()

        /// <summary>
        /// 시스템 색상 아이템 리스트 설정하기
        /// </summary>
        private void SetSystemColorItemList()
        {
            this.systemColorItemList.Clear();

            Type type = typeof(SystemColors);

            PropertyInfo[] propertyInfoArray = type.GetProperties(BindingFlags.Public | BindingFlags.Static);

            for(int i = 0; i < propertyInfoArray.Length; i++)
            {
                SystemColorItem item = new SystemColorItem();

                object propertyValue = propertyInfoArray[i].GetValue(null, null);

                string typeName = propertyValue.GetType().FullName;

                string name = propertyInfoArray[i].Name;

                if(typeName == typeof(Color).FullName)
                {
                    item.Color = (Color)propertyValue;
                    item.Name  = name;

                    this.systemColorItemList.Add(item);
                }
            }
        }

        #endregion
        #region 랩 패널 데이터 추가하기 - AddWrapPanelData()

        /// <summary>
        /// 랩 패널 데이터 추가하기
        /// </summary>
        private void AddWrapPanelData()
        {
            foreach(SystemColorItem item in this.systemColorItemList)
            {
                TextBlock textBlock = new TextBlock();

                textBlock.Background = new SolidColorBrush(item.Color      );
                textBlock.Foreground = new SolidColorBrush(item.InvertColor);
                textBlock.Text       = item.Name;

                this.wrapPanel.Children.Add(textBlock);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요