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

TestProject.zip
다운로드

▶ MainForm.cs

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 이벤트를 설정한다.

            Paint += Form_Paint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

        /// <summary>
        /// 폼 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            const int gap      = 8;
            const int rowCount = 4;

            int width  = DisplayRectangle.Width  / rowCount;
            int height = DisplayRectangle.Height / rowCount;

            for(int x = 0; x < rowCount; x++)
            {
                for(int y = 0; y < rowCount; y++)
                {
                    DrawBorder
                    (
                        e.Graphics,
                        new Rectangle
                        (
                            x * width + gap / 2,
                            y * height + gap / 2,
                            width - gap,
                            height - gap
                        ),
                        BorderStyle.Fixed3D,
                        y == x
                    );
                }
            }

            DrawBorder(e.Graphics, DisplayRectangle, BorderStyle.Fixed3D);
        }

        #endregion

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

        #region 테두리 그리기 - DrawBorder(graphics, rectangle, borderStyle, sunken)

        /// <summary>
        /// 테두리 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="borderStyle">테두리 스타일</param>
        /// <param name="sunken">함몰 여부</param>
        private void DrawBorder(Graphics graphics, Rectangle rectangle, BorderStyle borderStyle, bool sunken)
        {
            if(borderStyle == BorderStyle.FixedSingle)
            {
                rectangle.Width  -= 1;
                rectangle.Height -= 1;

                graphics.DrawRectangle(Pens.Black, rectangle);
            }
            else if(borderStyle == BorderStyle.Fixed3D)
            {
                Color[] colorArray;

                if(sunken)
                {
                    colorArray = new Color[]
                    {
                        SystemColors.ControlDark,
                        SystemColors.ControlDarkDark,
                        SystemColors.ControlLightLight,
                        SystemColors.ControlLight
                    };
                }
                else
                {
                    colorArray = new Color[]
                    {
                        SystemColors.ControlLightLight,
                        SystemColors.ControlLight,
                        SystemColors.ControlDark,
                        SystemColors.ControlDarkDark
                    };
                }

                using(Pen pen = new Pen(colorArray[0]))
                {
                    graphics.DrawLine(pen, rectangle.X, rectangle.Bottom - 1, rectangle.X        , rectangle.Y);
                    graphics.DrawLine(pen, rectangle.X, rectangle.Y         , rectangle.Right - 1, rectangle.Y);
                }

                using(Pen pen = new Pen(colorArray[1]))
                {
                    graphics.DrawLine(pen, rectangle.X + 1, rectangle.Bottom - 2, rectangle.X + 1    , rectangle.Y + 1);
                    graphics.DrawLine(pen, rectangle.X + 1, rectangle.Y + 1     , rectangle.Right - 2, rectangle.Y + 1);
                }

                using(Pen pen = new Pen(colorArray[2]))
                {
                    graphics.DrawLine(pen, rectangle.X        , rectangle.Bottom - 1, rectangle.Right - 1, rectangle.Bottom - 1);
                    graphics.DrawLine(pen, rectangle.Right - 1, rectangle.Bottom - 1, rectangle.Right - 1, rectangle.Y         );
                }

                using(Pen pen = new Pen(colorArray[3]))
                {
                    graphics.DrawLine(pen, rectangle.X + 1    , rectangle.Bottom - 2, rectangle.Right - 2, rectangle.Bottom - 2);
                    graphics.DrawLine(pen, rectangle.Right - 2, rectangle.Bottom - 2, rectangle.Right - 2, rectangle.Y + 1     );
                }
            }
        }

        #endregion
        #region 테두리 그리기 - DrawBorder(graphics, rectangle, borderStyle)

        /// <summary>
        /// 테두리 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="borderStyle">테두리 스타일</param>
        private void DrawBorder(Graphics graphics, Rectangle rectangle, BorderStyle borderStyle)
        {
            DrawBorder(graphics, rectangle, borderStyle, true);
        }

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