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

■ Menu 클래스 : 색상 선택하기

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


TestProject.zip


MainWindow.cs

 

 

using System;

using System.Reflection;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 텍스트 블럭

        /// </summary>

        private TextBlock textBlock;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800;

            Height     = 600;

            Title      = "Menu 클래스 : 색상 선택하기";

            FontFamily = new FontFamily("나눔고딕코딩");

            FontSize   = 16;

 

            DockPanel dockPanel = new DockPanel();

 

            Content = dockPanel;

 

            Menu menu = new Menu();

 

            dockPanel.Children.Add(menu);

 

            DockPanel.SetDock(menu, Dock.Top);

 

            this.textBlock = new TextBlock();

 

            this.textBlock.Text          = Title;

            this.textBlock.TextAlignment = TextAlignment.Center;

            this.textBlock.FontSize      = 32;

            this.textBlock.Background    = SystemColors.WindowBrush;

            this.textBlock.Foreground    = SystemColors.WindowTextBrush;

 

            dockPanel.Children.Add(this.textBlock);

 

            #region Color 메뉴

 

            MenuItem colorMenuItem = new MenuItem();

 

            colorMenuItem.Header = "_Color";

 

            menu.Items.Add(colorMenuItem);

 

            #endregion

                #region Foreground 메뉴

 

                MenuItem foregroundMenuItem = new MenuItem();

 

                foregroundMenuItem.Header = "_Foreground";

 

                foregroundMenuItem.SubmenuOpened += foregroundMenuItem_SubmenuOpened;

 

                colorMenuItem.Items.Add(foregroundMenuItem);

 

                FillSubsidaryMenuColor(foregroundMenuItem, subsidaryForegroundMenuItem_Click);

 

                #endregion

                #region Background 메뉴

 

                MenuItem backgroundMenuItem = new MenuItem();

 

                backgroundMenuItem.Header = "_Background";

 

                backgroundMenuItem.SubmenuOpened += backgroundMenuItem_SubmenuOpened;

 

                colorMenuItem.Items.Add(backgroundMenuItem);

 

                FillSubsidaryMenuColor(backgroundMenuItem, subsidaryBackgroundMenuItem_Click);

 

                #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

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

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

 

        #region 전경색 메뉴 항목 하위 메뉴 OPEN 처리하기 - foregroundMenuItem_SubmenuOpened(sender, e)

 

        /// <summary>

        /// 전경색 메뉴 항목 하위 메뉴 OPEN 처리하기

        /// </summary>

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

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

        private void foregroundMenuItem_SubmenuOpened(object sender, RoutedEventArgs e)

        {

            MenuItem menuItem = sender as MenuItem;

 

            foreach(MenuItem childMenuItem in menuItem.Items)

            {

                childMenuItem.IsChecked = ((this.textBlock.Foreground as SolidColorBrush).Color == (Color)childMenuItem.Tag);

            }

        }

 

        #endregion

        #region 하위 전경색 메뉴 항목 클릭시 처리하기 - subsidaryForegroundMenuItem_Click(sender, e)

 

        /// <summary>

        /// 하위 전경색 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void subsidaryForegroundMenuItem_Click(object sender, RoutedEventArgs e)

        {

            MenuItem menuItem = sender as MenuItem;

 

            Color color = (Color)menuItem.Tag;

 

            this.textBlock.Foreground = new SolidColorBrush(color);

        }

 

        #endregion

        #region 배경색 메뉴 항목 하위 메뉴 OPEN 처리하기 - backgroundMenuItem_SubmenuOpened(sender, e)

 

        /// <summary>

        /// 배경색 메뉴 항목 하위 메뉴 OPEN 처리하기

        /// </summary>

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

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

        private void backgroundMenuItem_SubmenuOpened(object sender, RoutedEventArgs e)

        {

            MenuItem menuItem = sender as MenuItem;

 

            foreach(MenuItem childMenuItem in menuItem.Items)

            {

                childMenuItem.IsChecked = ((this.textBlock.Background as SolidColorBrush).Color == (Color)childMenuItem.Tag);

            }

        }

 

        #endregion

        #region 하위 배경색 메뉴 항목 클릭시 처리하기 - subsidaryBackgroundMenuItem_Click(sender, e)

 

        /// <summary>

        /// 하위 배경색 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        private void subsidaryBackgroundMenuItem_Click(object sender, RoutedEventArgs e)

        {

            MenuItem menuItem = sender as MenuItem;

 

            Color color = (Color)menuItem.Tag;

 

            this.textBlock.Background = new SolidColorBrush(color);

        }

 

        #endregion

 

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

 

        #region 하위 메뉴 색상 채우기 - FillSubsidaryMenuColor(parentMenuItem, eventHandler)

 

        /// <summary>

        /// 하위 메뉴 색상 채우기

        /// </summary>

        /// <param name="parentMenuItem">부모 메뉴 항목</param>

        /// <param name="eventHandler">이벤트 핸들러</param>

        private void FillSubsidaryMenuColor(MenuItem parentMenuItem, RoutedEventHandler eventHandler)

        {

            foreach(PropertyInfo propertyInfo in typeof(Colors).GetProperties())

            {

                Color color = (Color)propertyInfo.GetValue(null, null);

 

                int count = 0;

 

                count += color.R == 0 || color.R == 255 ? 1 : 0;

                count += color.G == 0 || color.G == 255 ? 1 : 0;

                count += color.B == 0 || color.B == 255 ? 1 : 0;

 

                if(color.A == 255 && count > 1)

                {

                    MenuItem menuItem = new MenuItem();

 

                    menuItem.Header = "_" + propertyInfo.Name;

                    menuItem.Tag    = color;

 

                    menuItem.Click += eventHandler;

 

                    parentMenuItem.Items.Add(menuItem);

 

 

                    Rectangle rectangle = new Rectangle();

 

                    rectangle.Fill   = new SolidColorBrush(color);

                    rectangle.Width  = 12d;

                    rectangle.Height = 12d;

 

                    menuItem.Icon = rectangle;

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요