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

■ ContextMenu 클래스 사용하기

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

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

 

/// <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>

    /// 클릭 INLINE

    /// </summary>

    private Inline clickedInline;

 

    #endregion

 

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

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

 

    #region 생성자 - MainWindow()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public MainWindow()

    {

        Title  = "컨텍스트 메뉴 사용하기";

        Width  = 800;

        Height = 600;

 

 

        this.contextMenu = new ContextMenu();

 

 

        this.boldMenuItem = new MenuItem();

 

        this.boldMenuItem.Header = "Bold";

 

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

 

 

        this.italicMenuItem = new MenuItem();

 

        this.italicMenuItem.Header = "Italic";

 

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

 

 

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

 

        this.textDecorationMenuItemArray = new MenuItem[textDecorationLocationArray.Length];

 

        for(int i = 0; i < textDecorationLocationArray.Length; i++)

        {

            TextDecoration textDecoration = new TextDecoration();

 

            textDecoration.Location = textDecorationLocationArray[i];

 

 

            this.textDecorationMenuItemArray[i] = new MenuItem();

 

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

            this.textDecorationMenuItemArray[i].Tag    = textDecoration;

 

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

        }

 

 

        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[] wordArray = source.Split();

 

        foreach(string word in wordArray)

        {

            Run run = new Run(word);

 

            run.TextDecorations = new TextDecorationCollection();

 

            textBlock.Inlines.Add(run);

            textBlock.Inlines.Add(" ");

        }

    }

 

    #endregion

 

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

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

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

 

    #region 프로그램 실행하기 - Main()

 

    /// <summary>

    /// 프로그램 실행하기

    /// </summary>

    [STAThread]

    public static void Main()

    {

        Application application = new Application();

 

        application.Run(new MainWindow());

    }

 

    #endregion

 

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

    //////////////////////////////////////////////////////////////////////////////// Protected

 

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

 

    /// <summary>

    /// 마우스 오른쪽 버튼 UP 처리하기

    /// </summary>

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

    protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)

    {

        base.OnMouseRightButtonUp(e);

 

        this.clickedInline = e.Source as Inline;

 

        if(this.clickedInline != null)

        {

            this.boldMenuItem.IsChecked   = (this.clickedInline.FontWeight == FontWeights.Bold);

            this.italicMenuItem.IsChecked = (this.clickedInline.FontStyle  == FontStyles.Italic);

 

            foreach(MenuItem menuItem in this.textDecorationMenuItemArray)

            {

                menuItem.IsChecked = (this.clickedInline.TextDecorations.Contains(menuItem.Tag as TextDecoration));

            }

 

            this.contextMenu.IsOpen = true;

 

            e.Handled = true;

        }

    }

 

    #endregion

 

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

 

    #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.clickedInline.FontWeight = (menuItem.IsChecked ? FontWeights.Bold : FontWeights.Normal);

        }

        else if(menuItem == this.italicMenuItem)

        {

            this.clickedInline.FontStyle = (menuItem.IsChecked ? FontStyles.Italic : FontStyles.Normal);

        }

        else

        {

            if(menuItem.IsChecked)

            {

                this.clickedInline.TextDecorations.Add(menuItem.Tag as TextDecoration);

            }

            else

            {

                this.clickedInline.TextDecorations.Remove(menuItem.Tag as TextDecoration);

            }

        }

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요