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

■ ToolBar 클래스 사용하기

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


TestProject.zip


MainWindow.cs

 

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800;

            Height     = 600;

            Title      = "ToolBar 클래스 사용하기";

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

            FontSize   = 16;

 

            RoutedUICommand[] routedUICommandArray =

            {

                ApplicationCommands.New  ,

                ApplicationCommands.Open ,

                ApplicationCommands.Save ,

                ApplicationCommands.Print,

                ApplicationCommands.Cut  ,

                ApplicationCommands.Copy ,

                ApplicationCommands.Paste,

                ApplicationCommands.Delete

            };

 

            string[] imageNameArray =

            {

                "NewDocumentHS.png",

                "openHS.png"       ,

                "saveHS.png"       ,

                "PrintHS.png"      ,

                "CutHS.png"        ,

                "CopyHS.png"       ,

                "PasteHS.png"      ,

                "DeleteHS.png"

            };

 

 

            DockPanel dockPanel = new DockPanel();

 

            Content = dockPanel;

 

            ToolBar toolBar = new ToolBar();

 

            dockPanel.Children.Add(toolBar);

 

            DockPanel.SetDock(toolBar, Dock.Top);

 

            for(int i = 0; i < 8; i++)

            {

                if(i == 4)

                {

                    toolBar.Items.Add(new Separator());

                }

 

                Button button = new Button();

 

                button.Command = routedUICommandArray[i];

 

                toolBar.Items.Add(button);

 

                Image image = new Image();

 

                image.Source = new BitmapImage(new Uri("pack://application:,,,/IMAGE/" + imageNameArray[i]));

 

                image.Stretch = Stretch.None;

 

                StackPanel stackPanel = new StackPanel();

 

                stackPanel.Orientation = Orientation.Horizontal;

 

                button.Content = stackPanel;

 

                TextBlock textBlock = new TextBlock();

 

                textBlock.Text = routedUICommandArray[i].Text;

 

                stackPanel.Children.Add(image);

                stackPanel.Children.Add(textBlock);

 

                ToolTip toolTip = new ToolTip();

 

                toolTip.Content = routedUICommandArray[i].Text;

 

                button.ToolTip = toolTip;

 

                CommandBindings.Add(new CommandBinding(routedUICommandArray[i], ToolBarButtonExecuted));

            }

 

            RichTextBox richTextBox = new RichTextBox();

 

            dockPanel.Children.Add(richTextBox);

 

            richTextBox.Focus();

        }

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

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

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

 

        #region 툴바 버튼 실행시 처리하기 - ToolBarButtonExecuted(sender, e)

 

        /// <summary>

        /// 툴바 버튼 실행시 처리하기

        /// </summary>

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

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

        private void ToolBarButtonExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            RoutedUICommand routedUICommand = e.Command as RoutedUICommand;

 

            MessageBox.Show(routedUICommand.Name + " command not yet implemented", Title);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요