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

TestProject.zip
0.01MB

▶ GradientPanel.cs

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 그라디언트 패널
    /// </summary>
    public class GradationPanel : Panel
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 왼쪽 색상 - LeftColor

        /// <summary>
        /// 왼쪽 색상
        /// </summary>
        public Color LeftColor { get; set; }

        #endregion
        #region 오른쪽 색상 - RightColor

        /// <summary>
        /// 오른쪽 색상
        /// </summary>
        public Color RightColor { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 페인트시 처리하기 - OnPaint(e)

        /// <summary>
        /// 페인트시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            LinearGradientBrush brush = new LinearGradientBrush
            (
                ClientRectangle,
                LeftColor,
                RightColor,
                0f
            );

            e.Graphics.FillRectangle(brush, ClientRectangle);

            base.OnPaint(e);
        }

        #endregion
    }
}

 

▶ 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();

            this.leftButton.Click  += leftButton_Click;
            this.rightButton.Click += rightButton_Click;

            SetLeftColor(Color.Yellow);
            SetRightColor(Color.Orange);
        }

        #endregion

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

        #region 왼쪽 버튼 클릭시 처리하기 - leftButton_Click(sender, e)

        /// <summary>
        /// 왼쪽 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void leftButton_Click(object sender, EventArgs e)
        {
            Color color;

            if(ShowColorDialog(out color))
            {
                SetLeftColor(color);
            }
        }

        #endregion
        #region 오른쪽 버튼 클릭시 처리하기 - rightButton_Click(sender, e)

        /// <summary>
        /// 오른쪽 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void rightButton_Click(object sender, EventArgs e)
        {
            Color color;

            if(ShowColorDialog(out color))
            {
                SetRightColor(color);
            }
        }

        #endregion

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

        #region 색상 대화 상자 표시하기 - ShowColorDialog(color)

        /// <summary>
        /// 색상 대화 상자 표시하기
        /// </summary>
        /// <param name="color">색상</param>
        /// <returns>처리 결과</returns>
        public bool ShowColorDialog(out Color color)
        {
            color = new Color();

            ColorDialog colorDialog = new ColorDialog();

            if(colorDialog.ShowDialog() == DialogResult.OK)
            {
                color = colorDialog.Color;

                return true;
            }

            return false;
        }

        #endregion
        #region 왼쪽 색상 설정하기 - SetLeftColor(color)

        /// <summary>
        /// 왼쪽 색상 설정하기
        /// </summary>
        /// <param name="color">색상</param>
        private void SetLeftColor(Color color)
        {
            this.gradationPanel.LeftColor = color;

            this.leftButton.BackColor = color;

            this.gradationPanel.Refresh();
        }

        #endregion
        #region 오른쪽 색상 설정하기 - SetRightColor(color)

        /// <summary>
        /// 오른쪽 색상 설정하기
        /// </summary>
        /// <param name="color">색상</param>
        private void SetRightColor(Color color)
        {
            this.gradationPanel.RightColor = color;

            this.rightButton.BackColor = color;

            this.gradationPanel.Refresh();
        }

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

댓글을 달아 주세요