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

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 폰트명

        /// </summary>

        private const string FONT_NAME = "나눔고딕코딩";

 

        /// <summary>

        /// 폰트 크기

        /// </summary>

        private const float FONT_SIZE = 12;

 

        /// <summary>

        /// 폰트 스타일

        /// </summary>

        private const FontStyle FONT_STYLE = FontStyle.Bold;

 

        /// <summary>

        /// 메뉴 제목

        /// </summary>

        private const string MENU_CAPTION = "안녕하세요";

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.helloMenuItem.MeasureItem += helloMenuItem_MeasureItem;

            this.helloMenuItem.DrawItem    += helloMenuItem_DrawItem;

            this.helloMenuItem.Click       += helloMenuItem_Click;

            this.exitMenuItem.Click        += exitMenuItem_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 안녕하세요 메뉴 항목 측정하기 - helloMenuItem_MeasureItem(sender, e)

 

        /// <summary>

        /// 안녕하세요 메뉴 항목 측정하기

        /// </summary>

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

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

        private void helloMenuItem_MeasureItem(object sender, MeasureItemEventArgs e)

        {

            using(Font font = new Font(FONT_NAME, FONT_SIZE, FONT_STYLE))

            {

                SizeF textSize = e.Graphics.MeasureString(MENU_CAPTION, font);

 

                e.ItemHeight = (int)textSize.Height;

                e.ItemWidth  = (int)textSize.Width;

            }

        }

 

        #endregion

        #region 안녕하세요 항목 메뉴 그리기 - helloMenuItem_DrawItem(sender, e)

 

        /// <summary>

        /// 안녕하세요 메뉴 항목 그리기

        /// </summary>

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

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

        private void helloMenuItem_DrawItem(object sender, DrawItemEventArgs e)

        {

            using(Font font = new Font(FONT_NAME, FONT_SIZE, FONT_STYLE))

            {

                if((e.State & DrawItemState.Selected) != DrawItemState.None)

                {

                    using(Brush brush = new LinearGradientBrush(e.Bounds, Color.Red, Color.Black, 90))

                    {

                        e.Graphics.FillRectangle(brush, e.Bounds);

                    }

 

                    e.Graphics.DrawString(MENU_CAPTION, font, Brushes.AliceBlue, e.Bounds.X, e.Bounds.Y);

                }

                else

                {

                    e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);

 

                    e.Graphics.DrawString(MENU_CAPTION, font, Brushes.Black, e.Bounds.X, e.Bounds.Y);

                }

            }

        }

 

        #endregion

        #region 안녕하세요 메뉴 항목 클릭시 처리하기 - helloMenuItem_Click(sender, e)

 

        /// <summary>

        /// 안녕하세요 메뉴 항목 클릭시 처리하기

        /// </summary>

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

        /// <param name="e">이베늩 인자</param>

        private void helloMenuItem_Click(object sender, EventArgs e)

        {

            MessageBox.Show(this, "안녕하세요?");

        }

 

        #endregion

        #region 종료 버튼 클릭시 처리하기 - exitMenuItem_Click(sender, e)

 

        /// <summary>

        /// 종료 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void exitMenuItem_Click(object sender, EventArgs e)

        {

            Close();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요