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

■ RichTextBox 클래스 사용하기

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


TestProject.zip


MainWindow.cs

 

 

using Microsoft.Win32;

using System;

using System.IO;

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 RichTextBox richTextBox;

 

        /// <summary>

        /// 필터

        /// </summary>

        private string filter = "Document Files(*.xaml)|*.xaml|All files (*.*)|*.*";

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800;

            Height     = 600;

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

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

            FontSize   = 16;

 

            this.richTextBox = new RichTextBox();

 

            this.richTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

 

            Content = this.richTextBox;

 

            this.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

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

 

        #region PREVIEW 텍스트 입력시 처리하기 - OnPreviewTextInput(e)

 

        /// <summary>

        /// PREVIEW 텍스트 입력시 처리하기

        /// </summary>

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

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)

        {

            #region CTRL + O 키를 누른 경우 처리한다.

 

            if(e.ControlText.Length > 0 && e.ControlText[0] == '\x0F')

            {

                OpenFileDialog openFileDialog = new OpenFileDialog();

 

                openFileDialog.CheckFileExists = true;

                openFileDialog.Filter          = filter;

 

                if((bool)openFileDialog.ShowDialog(this))

                {

                    FlowDocument flowDocument = richTextBox.Document;

 

                    TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

 

                    Stream stream = null;

 

                    try

                    {

                        stream = new FileStream(openFileDialog.FileName, FileMode.Open);

 

                        textRange.Load(stream, DataFormats.Xaml);

                    }

                    catch(Exception exception)

                    {

                        MessageBox.Show(exception.Message, Title);

                    }

                    finally

                    {

                        if(stream != null)

                        {

                            stream.Close();

                        }

                    }

                }

 

                e.Handled = true;

            }

 

            #endregion

            #region CTRL + S 키를 누른 경우 처리한다.

 

            if(e.ControlText.Length > 0 && e.ControlText[0] == '\x13')

            {

                SaveFileDialog saveFileDialog = new SaveFileDialog();

 

                saveFileDialog.Filter = filter;

 

                if((bool)saveFileDialog.ShowDialog(this))

                {

                    FlowDocument flowDocument = richTextBox.Document;

 

                    TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

 

                    Stream stream = null;

 

                    try

                    {

                        stream = new FileStream(saveFileDialog.FileName, FileMode.Create);

 

                        textRange.Save(stream, DataFormats.Xaml);

                    }

                    catch(Exception exception)

                    {

                        MessageBox.Show(exception.Message, Title);

                    }

                    finally

                    {

                        if(stream != null)

                        {

                            stream.Close();

                        }

                    }

                }

 

                e.Handled = true;

            }

 

            #endregion

 

            base.OnPreviewTextInput(e);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요