첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

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

        #region Field

        /// <summary>
        /// 컨텍스트 메뉴 항목
        /// </summary>
        private ContextMenu contextMenu;

        /// <summary>
        /// Bold 메뉴 항목
        /// </summary>
        private MenuItem boldMenuItem;

        /// <summary>
        /// Italic 메뉴 항목
        /// </summary>
        private MenuItem italicMenuItem;

        /// <summary>
        /// 텍스트 데코레이션 메뉴 항목 배열
        /// </summary>
        private MenuItem[] textDecorationMenuItemArray;

        /// <summary>
        /// 클릭한 인라인
        /// </summary>
        private Inline inlineClicked;

        #endregion

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

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            Width      = 800;
            Height     = 600;
            Title      = "ContextMenu 클래스 : 컨텍스트 메뉴 표시하기";
            FontFamily = new FontFamily("나눔고딕코딩");
            FontSize   = 16;

            this.contextMenu = new ContextMenu();

            #region Bold 메뉴

            this.boldMenuItem = new MenuItem();

            this.boldMenuItem.Header = "Bold";

            this.contextMenu.Items.Add(this.boldMenuItem);

            #endregion
            #region Italic 메뉴

            this.italicMenuItem = new MenuItem();

            this.italicMenuItem.Header = "Italic";

            this.contextMenu.Items.Add(this.italicMenuItem);

            #endregion
            #region TextDecorationLocation 열거형 멤버에 대응하는 메뉴를 생성한다.

            TextDecorationLocation[] textDecorationLocationArray = (TextDecorationLocation[])Enum.GetValues(typeof(TextDecorationLocation));

            textDecorationMenuItemArray = new MenuItem[textDecorationLocationArray.Length];

            for(int i = 0; i < textDecorationLocationArray.Length; i++)
            {
                TextDecoration textDecoration = new TextDecoration();

                textDecoration.Location = textDecorationLocationArray[i];

                textDecorationMenuItemArray[i] = new MenuItem();

                textDecorationMenuItemArray[i].Header = textDecorationLocationArray[i].ToString();
                textDecorationMenuItemArray[i].Tag    = textDecoration;

                this.contextMenu.Items.Add(textDecorationMenuItemArray[i]);
            }

            #endregion

            this.contextMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(menuItem_Click));

            TextBlock textBlock = new TextBlock();

            textBlock.FontSize            = 32;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            textBlock.VerticalAlignment   = VerticalAlignment.Center;

            Content = textBlock;

            string   source     = "To be, or not to be, that is the question";
            string[] tokenArray = source.Split();

            foreach(string token in tokenArray)
            {
                Run run = new Run(token);

                run.TextDecorations = new TextDecorationCollection();

                textBlock.Inlines.Add(run);

                textBlock.Inlines.Add(" ");
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application application = new Application();

            application.Run(new MainWindow());
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Protected

        #region 마우스 오른쪽 버튼 업시 처리하기 - OnMouseRightButtonUp(e)

        /// <summary>
        /// 마우스 오른쪽 버튼 업시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonUp(e);

            if((this.inlineClicked = e.Source as Inline) != null)
            {
                this.boldMenuItem.IsChecked   = (this.inlineClicked.FontWeight == FontWeights.Bold);
                this.italicMenuItem.IsChecked = (this.inlineClicked.FontStyle  == FontStyles.Italic);

                foreach(MenuItem menuItem in textDecorationMenuItemArray)
                {
                    menuItem.IsChecked = (this.inlineClicked.TextDecorations.Contains(menuItem.Tag as TextDecoration));
                }

                this.contextMenu.IsOpen = true;

                e.Handled = true;
            }
        }

        #endregion
        #region 메뉴 항목 클릭시 처리하기 - menuItem_Click(sender, e)

        /// <summary>
        /// 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void menuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = e.Source as MenuItem;

            menuItem.IsChecked ^= true;

            if(menuItem == this.boldMenuItem)
            {
                this.inlineClicked.FontWeight = (menuItem.IsChecked ? FontWeights.Bold : FontWeights.Normal);
            }
            else if(menuItem == this.italicMenuItem)
            {
                this.inlineClicked.FontStyle = (menuItem.IsChecked ? FontStyles.Italic : FontStyles.Normal);
            }
            else
            {
                if(menuItem.IsChecked)
                {
                    this.inlineClicked.TextDecorations.Add(menuItem.Tag as TextDecoration);
                }
                else
                {
                    this.inlineClicked.TextDecorations.Remove(menuItem.Tag as TextDecoration);
                }
            }
        }

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

댓글을 달아 주세요