첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

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 TAB_MARGIN = 3;

        /// <summary>
        /// X 너비
        /// </summary>
        private int xWidth = 8;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load                      += Form_Load;
            this.tabControl.DrawItem  += tabControl_DrawItem;
            this.tabControl.MouseDown += tabControl_MouseDown;

            #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.tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.tabControl.SizeMode = TabSizeMode.Fixed;

            Size tabSize = this.tabControl.ItemSize;

            tabSize.Width  =  100;
            tabSize.Height += 6;

            this.tabControl.ItemSize = tabSize;
        }

        #endregion
        #region 탭 컨트롤 항목 그리기 - tabControl_DrawItem(sender, e)

        /// <summary>
        /// 탭 컨트롤 항목 그리기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            Brush textBrush;
            Brush boxBrush;
            Pen   boxPen;

            Rectangle tabRectangle = this.tabControl.GetTabRect(e.Index);

            if(e.State == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.DarkOrange, tabRectangle);

                e.DrawFocusRectangle();

                textBrush = Brushes.Yellow;
                boxBrush  = Brushes.Silver;
                boxPen    = Pens.DarkBlue;
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.PaleGreen, tabRectangle);

                textBrush = Brushes.DarkBlue;
                boxBrush  = Brushes.LightGray;
                boxPen    = Pens.DarkBlue;
            }

            RectangleF layoutRectangle = new RectangleF
            (
                tabRectangle.Left + TAB_MARGIN,
                tabRectangle.Y + TAB_MARGIN,
                tabRectangle.Width - 2 * TAB_MARGIN,
                tabRectangle.Height - 2 * TAB_MARGIN
            );

            using(StringFormat stringFormat = new StringFormat())
            {
                using(Font smallFont = new Font(Font.FontFamily, 6, FontStyle.Bold))
                {
                    stringFormat.Alignment     = StringAlignment.Near;
                    stringFormat.LineAlignment = StringAlignment.Near;

                    e.Graphics.DrawString
                    (
                        e.Index.ToString(),
                        smallFont,
                        textBrush,
                        layoutRectangle,
                        stringFormat
                    );
                }

                using(Font largeFont = new Font(Font, FontStyle.Bold))
                {
                    stringFormat.Alignment     = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString
                    (
                        this.tabControl.TabPages[e.Index].Text,
                        largeFont,
                        textBrush,
                        layoutRectangle,
                        stringFormat
                    );
                }

                Rectangle rectangle = this.tabControl.GetTabRect(e.Index);

                e.Graphics.FillRectangle
                (
                    boxBrush,
                    layoutRectangle.Right - this.xWidth,
                    layoutRectangle.Top,
                    this.xWidth,
                    this.xWidth
                );

                e.Graphics.DrawRectangle
                (
                    boxPen,
                    layoutRectangle.Right - this.xWidth,
                    layoutRectangle.Top,
                    this.xWidth,
                    this.xWidth
                );

                e.Graphics.DrawLine
                (
                    boxPen,
                    layoutRectangle.Right - this.xWidth,
                    layoutRectangle.Top,
                    layoutRectangle.Right,
                    layoutRectangle.Top + this.xWidth
                );

                e.Graphics.DrawLine
                (
                    boxPen,
                    layoutRectangle.Right - this.xWidth,
                    layoutRectangle.Top + this.xWidth,
                    layoutRectangle.Right,
                    layoutRectangle.Top
                );
            }
        }

        #endregion
        #region 탭 컨트롤 마우스 DOWN 처리하기 - tabControl_MouseDown(sender, e)

        /// <summary>
        /// 탭 컨트롤 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void tabControl_MouseDown(object sender, MouseEventArgs e)
        {
            for(int i = 0; i < this.tabControl.TabPages.Count; i++)
            {
                Rectangle tabRectangle = tabControl.GetTabRect(i);

                RectangleF rectangle = new RectangleF
                (
                    tabRectangle.Left + TAB_MARGIN,
                    tabRectangle.Y + TAB_MARGIN,
                    tabRectangle.Width - 2 * TAB_MARGIN,
                    tabRectangle.Height - 2 * TAB_MARGIN
                );

                if
                (
                    e.X >= rectangle.Right - this.xWidth &&
                    e.X <= rectangle.Right &&
                    e.Y >= rectangle.Top &&
                    e.Y <= rectangle.Top + this.xWidth
                )
                {
                    this.tabControl.TabPages.RemoveAt(i);

                    return;
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요