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

■ KeyGesture 클래스 : 메뉴 단축키 사용하기

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


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"

    Width="800"

    Height="600"

    Title="KeyGesture 클래스 : 메뉴 단축키 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <DockPanel>

        <Menu DockPanel.Dock="Top"

            Padding="5"

            FontSize="16">

            <MenuItem Name="editMenuItem" Header="편집(_E)">

                <MenuItem Name="cutMenuItem" Header="잘라내기(_T)"

                    InputGestureText="Ctrl+X" />

                <MenuItem Name="copyMenuItem" Header="복사하기(_C)"

                    InputGestureText="Ctrl+C" />

                <MenuItem Name="pasteMenuItem" Header="붙여넣기(_V)"

                    InputGestureText="Ctrl+V" />

                <MenuItem Name="deleteMenuItem" Header="삭제하기(_D)"

                    InputGestureText="Delete" />

            </MenuItem>

        </Menu>

        <TextBlock Name="textBlock"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            TextWrapping="Wrap"

            FontSize="32"

            Text="샘플 클립보드 텍스트" />

    </DockPanel>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Collections.Generic;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 키 제스처 딕셔너리

        /// </summary>

        Dictionary<KeyGesture, MenuItem> keyGestureDictionary = new Dictionary<KeyGesture, MenuItem>();

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.editMenuItem.SubmenuOpened += editMenuItem_SubmenuOpened;

            this.cutMenuItem.Click          += cutMenuItem_Click;

            this.copyMenuItem.Click         += copyMenuItem_Click;

            this.pasteMenuItem.Click        += pasteMenuItem_Click;

            this.deleteMenuItem.Click       += deleteMenuItem_Click;

 

            this.keyGestureDictionary.Add(new KeyGesture(Key.X, ModifierKeys.Control), this.cutMenuItem   );

            this.keyGestureDictionary.Add(new KeyGesture(Key.C, ModifierKeys.Control), this.copyMenuItem  );

            this.keyGestureDictionary.Add(new KeyGesture(Key.V, ModifierKeys.Control), this.pasteMenuItem );

            this.keyGestureDictionary.Add(new KeyGesture(Key.Delete                 ), this.deleteMenuItem);

        }

 

        #endregion

 

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

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

 

        #region 키 DOWN 사전 처리하기 - OnPreviewKeyDown(e)

 

        /// <summary>

        /// 키 DOWN 사전 처리하기

        /// </summary>

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

        protected override void OnPreviewKeyDown(KeyEventArgs e)

        {

            base.OnKeyDown(e);

 

            foreach(KeyGesture keyGesture in this.keyGestureDictionary.Keys)

            {

                if(keyGesture.Matches(null, e))

                {

                    e.Handled = true;

 

                    this.keyGestureDictionary[keyGesture].RaiseEvent

                    (

                        new RoutedEventArgs

                        (

                            MenuItem.ClickEvent,

                            this.keyGestureDictionary[keyGesture]

                        )

                    );

                }

            }

        }

 

        #endregion

 

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

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

 

        #region 편집 메뉴 항목 하위 메뉴 오픈시 처리하기 - editMenuItem_SubmenuOpened(sender, e)

 

        /// <summary>

        /// 편집 메뉴 항목 하위 메뉴 오픈시 처리하기

        /// </summary>

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

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

        private void editMenuItem_SubmenuOpened(object sender, RoutedEventArgs e)

        {

            bool isEnabled = (this.textBlock.Text != null && this.textBlock.Text.Length > 0);

 

            this.cutMenuItem.IsEnabled    = isEnabled;

            this.copyMenuItem.IsEnabled   = isEnabled;

            this.pasteMenuItem.IsEnabled  = Clipboard.ContainsText();

            this.deleteMenuItem.IsEnabled = isEnabled;

        }

 

        #endregion

        #region 잘라내기 메뉴 항목 클릭시 처리하기 - cutMenuItem_Click(sender, e)

 

        /// <summary>

        /// 잘라내기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        protected void cutMenuItem_Click(object sender, RoutedEventArgs e)

        {

            copyMenuItem_Click(sender, e);

 

            deleteMenuItem_Click(sender, e);

        }

 

        #endregion

        #region 복사하기 메뉴 항목 클릭시 처리하기 - copyMenuItem_Click(sender, e)

 

        /// <summary>

        /// 복사하기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        protected void copyMenuItem_Click(object sender, RoutedEventArgs e)

        {

            if(this.textBlock.Text != null && this.textBlock.Text.Length > 0)

            {

                Clipboard.SetText(this.textBlock.Text);

            }

        }

 

        #endregion

        #region 붙여넣기 메뉴 항목 클릭시 처리하기 - pasteMenuItem_Click(sender, e)

 

        /// <summary>

        /// 붙여넣기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        protected void pasteMenuItem_Click(object sender, RoutedEventArgs e)

        {

            if(Clipboard.ContainsText())

            {

                this.textBlock.Text = Clipboard.GetText();

            }

        }

 

        #endregion

        #region 삭제하기 메뉴 항목 클릭시 처리하기 - deleteMenuItem_Click(sender, e)

 

        /// <summary>

        /// 삭제하기 메뉴 항목 클릭시 처리하기

        /// </summary>

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

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

        protected void deleteMenuItem_Click(object sender, RoutedEventArgs e)

        {

            this.textBlock.Text = null;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요