■ Clipboard 클래스 : 멀티 포맷 사용하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                    += Form_Load;

            this.copyButton.Click   += copyButton_Click;

            this.pasterButton.Click += pasteButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

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

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

        private void Form_Load(object sender, EventArgs e)

        {

            string text = "The quick brown fox jumps over the lazy dog.";

 

            this.sourceRichTextBox.Text = text;

 

            this.sourceRichTextBox.Select(text.IndexOf("quick"), "quick".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Italic);

 

            this.sourceRichTextBox.Select(text.IndexOf("brown"), "brown".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Bold);

            this.sourceRichTextBox.SelectionColor = Color.Brown;

 

            this.sourceRichTextBox.Select(text.IndexOf("fox"), "fox".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Bold);

            this.sourceRichTextBox.SelectionColor = Color.Red;

 

            this.sourceRichTextBox.Select(text.IndexOf("jumps over"), "jumps over".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Underline);

 

            this.sourceRichTextBox.Select(text.IndexOf("lazy"), "lazy".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Bold);

 

            this.sourceRichTextBox.Select(text.IndexOf("dog"), "dog".Length);

            this.sourceRichTextBox.SelectionFont = new Font(this.sourceRichTextBox.SelectionFont, FontStyle.Bold);

            this.sourceRichTextBox.SelectionColor = Color.Blue;

 

            this.sourceRichTextBox.Select(0, 0);

        }

 

        #endregion

        #region 복사하기 버튼 클릭시 처리하기 - copyButton_Click(sender, e)

 

        /// <summary>

        /// 복사하기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void copyButton_Click(object sender, EventArgs e)

        {

            DataObject dataObject = new DataObject();

 

            dataObject.SetData(DataFormats.Rtf , this.sourceRichTextBox.Rtf );

            dataObject.SetData(DataFormats.Text, this.sourceRichTextBox.Text);

 

            string htmlText;

 

            htmlText = "<HTML>\r\n";

            htmlText += "  <HEAD>The Quick Brown Fox</HEAD>\r\n";

            htmlText += "  <BODY>\r\n";

            htmlText += sourceRichTextBox.Text + "\r\n";

            htmlText += "  </BODY>\r\n";

            htmlText += "</HTML>";

 

            dataObject.SetData(DataFormats.Html, htmlText);

 

            Clipboard.SetDataObject(dataObject);

        }

 

        #endregion

        #region 붙여넣기 버튼 클릭시 처리하기 - pasteButton_Click(sender, e)

 

        /// <summary>

        /// 붙여넣기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void pasteButton_Click(object sender, EventArgs e)

        {

            IDataObject dataObject = Clipboard.GetDataObject();

 

            if(dataObject.GetDataPresent(DataFormats.Rtf))

            {

                this.rtfRichTextBox.Rtf  = dataObject.GetData(DataFormats.Rtf).ToString();

                this.rtfCodeTextBox.Text = dataObject.GetData(DataFormats.Rtf).ToString();

            }

            else

            {

                this.rtfRichTextBox.Clear();

                this.rtfCodeTextBox.Clear();

            }

 

            if(dataObject.GetDataPresent(DataFormats.Text))

            {

                this.textTextBox.Text = dataObject.GetData(DataFormats.Text).ToString();

            }

            else

            {

                this.textTextBox.Clear();

            }

 

            if(dataObject.GetDataPresent(DataFormats.Html))

            {

                this.htmlTextBox.Text = dataObject.GetData(DataFormats.Html).ToString();

            }

            else

            {

                this.htmlTextBox.Clear();

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요