■ ToolTip 클래스 : 사용자 정의 그리기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 마진

        /// </summary>

        private const int MARGIN = 10;

 

        /// <summary>

        /// 이미지

        /// </summary>

        private Image image;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.image = Image.FromFile("happy.png");

 

            #region 이벤트를 설정한다.

 

            this.Load                  += Form_Load;

            this.clickMeButton.Click   += clickMeButton_Click;

            this.dontClickButton.Click += dontClickButton_Click;

            this.whateverButton.Click  += whateverButton_Click;

            this.toolTip.Popup         += toolTip_Popup;

            this.toolTip.Draw          += toolTip_Draw;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

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

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

        private void Form_Load(object sender, EventArgs e)

        {

            this.toolTip.SetToolTip

            (

                this.whateverButton,

                "Whatever...\nWhatever...\nWhatever..."

            );

        }

 

        #endregion

        #region 툴팁 팝업 처리하기 - toolTip_Popup(sender, e)

 

        /// <summary>

        /// 툴팁 팝업 처리하기

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void toolTip_Popup(object sender, PopupEventArgs e)

        {

            int imageWidth  = 2 * MARGIN + this.image.Width;

            int imageHeight = 2 * MARGIN + this.image.Height;

 

            int toolTipWidth  = e.ToolTipSize.Width + 2 * MARGIN + imageWidth;

            int toolTipHeight = e.ToolTipSize.Height;

 

            if(toolTipHeight < imageHeight)

            {

                toolTipHeight = imageHeight;

            }

 

            e.ToolTipSize = new Size(toolTipWidth, toolTipHeight);

        }

 

        #endregion

        #region 툽팁 그리기 - toolTip_Draw(sender, e)

 

        /// <summary>

        /// 툽팁 그리기

        /// </summary>

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

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

        private void toolTip_Draw(object sender, DrawToolTipEventArgs e)

        {

            e.DrawBackground();

 

            e.DrawBorder();

 

            e.Graphics.DrawImage(this.image, MARGIN, MARGIN);

 

            using(StringFormat stringFormat = new StringFormat())

            {

                stringFormat.Alignment     = StringAlignment.Near;

                stringFormat.LineAlignment = StringAlignment.Center;

 

                int imageWidth = 2 * MARGIN + this.image.Width;

 

                Rectangle rectangle = new Rectangle

                (

                    imageWidth,

                    0,

                    e.Bounds.Width - imageWidth,

                    e.Bounds.Height

                );

 

                e.Graphics.DrawString

                (

                    e.ToolTipText,

                    e.Font,

                    Brushes.Green,

                    rectangle,

                    stringFormat

                );

            }

        }

 

        #endregion

        #region Click Me 버튼 클릭시 처리하기 - clickMeButton_Click(sender, e)

 

        /// <summary>

        /// Click Me 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void clickMeButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show(this, "Click Me", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

 

        #endregion

        #region Don't Click 버튼 클릭시 처리하기 - dontClickButton_Click(sender, e)

 

        /// <summary>

        /// Don't Click 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void dontClickButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Don't Click", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

 

        #endregion

        #region Whatever 버튼 클릭시 처리하기 - whateverButton_Click(sender, e)

 

        /// <summary>

        /// Whatever 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void whateverButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Whatever", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요