아래와 같은 그라데이션 배경 콘트롤을 만들 때 사용한다. 



해당 usercontrol에는 텍스트박스 하나만 올라가있다.


아래 소스 적용시 아래의 아이콘 이미지를 받아서 리소스에 등록한다.




code-behind

using System;

using System.ComponentModel;

using System.ComponentModel.Design;

using System.Drawing;

using System.Drawing.Design;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace Banana

{

    public partial class ucTitle : UserControl

    {

        public ucTitle()

        {

            InitializeComponent();

            this.SetStyle(ControlStyles.UserPaint, true);

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            Control_SetLocation();

        }

 

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)

        {

            //높이 26으로 고정

            base.SetBoundsCore(x, y, width, this._titleBarHeight, specified);

        }

 

        private void ucTitle_Paint(object sender, PaintEventArgs e)

        {

            //배경 그라데이션 칠하기

            Point startPoint = new Point(0, 0);

            Point endPoint = new Point(this.Size.Width, this.Size.Height);

            LinearGradientBrush lgb = new LinearGradientBrush(startPoint, endPoint, this._color1, this._color2);

            Graphics g = e.Graphics;

            g.FillRectangle(lgb, 0, 0, this.Size.Width, this.Size.Height);

        }

        private void ucTitle_Load(object sender, EventArgs e)

        {

            this.Paint += new PaintEventHandler(ucTitle_Paint);

            this.Refresh();

 

            ToolTip tip = new ToolTip();

            tip.AutoPopDelay = 5000;

            tip.InitialDelay = 500;

            tip.ReshowDelay = 500;

            tip.ShowAlways = true;

 

            tip.SetToolTip(this.QMark, TooltipMsg);

        }

        private void ucTitle_Resize(object sender, EventArgs e)

        {

            this.Refresh();

        }

 

        #region

 

        [Browsable(true)]

        [Category("Custom")]

        [Description("타이틀")]

        public string Title

        {

            get

            {

                return this.txtTitle.Text;

            }

            set

            {

                this.txtTitle.Text = value;

            }

        }

        [Browsable(true)]

        [Category("Custom")]

        [Description("그라데이션 좌측")]

        public Color GradationColor1

        {

            get { return this._color1; }

            set

            {

                this._color1 = value;

                this.Refresh();

            }

        }

        private Color _color1 = Color.FromArgb(255, 127, 160, 206);

 

        [Browsable(true)]

        [Category("Custom")]

        [Description("그라데이션 우측 색상")]

        public Color GradationColor2

        {

            get { return this._color2; }

            set

            {

                this._color2 = value;

                this.Refresh();

            }

        }

        private Color _color2 = Color.FromArgb(255, 162, 198, 237);

 

        [Browsable(true)]

        [Category("Custom")]

        [Description("툴팁 이벤트가 발생할 아이콘 표시여부")]

        public bool TooltipQuestionMark

        {

            get { return this._qmark; }

            set

            {

                this._qmark = value;

                this.QMark.Visible = this._qmark;

            }

        }

        private bool _qmark = false;

 

        [Browsable(true)]

        [Category("Custom")]

        [Description("툴팁 실행시 표시할 메시지")]

        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]

        public string TooltipMsg

        {

            //해당 메시지를 PropertyGrid에서 Multiline으로 작성하고 싶다면 "[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]"를 추가한다.

            //그리고 참조에 "System.Design.dll"을 추가하고 "using System.Drawing.Design;" 작성하면 된다.

            get; set;

        }

 

        [Browsable(true)]

        [Category("Custom")]

        [Description("타이틀바 높이 고정")]

        public int TitleBarHeight

        {

            get { return this._titleBarHeight; }

            set

            {

                this._titleBarHeight = value;

                this.SetBounds(this.Location.X, this.Location.Y, this.Size.Width, this._titleBarHeight, BoundsSpecified.Height);

                Control_SetLocation();

            }

        }

        private int _titleBarHeight = 26;

 

 

        #endregion

 

        private void txtTitle_SizeChanged(object sender, EventArgs e)

        {

            Control_SetLocation();

        }

        private void Control_SetLocation()

        {

            //txtTitle 먼저 높이 선정 후

            int controlLocationY = this.Height / 2 - txtTitle.Height / 2;

            txtTitle.Location = new Point(txtTitle.Location.X, controlLocationY);

 

            //Qmark 높이 선정

            int paddingLeft = txtTitle.Location.X + txtTitle.Size.Width + 5;

            QMark.Location = new Point(paddingLeft, controlLocationY - 2);

        }

    }

}

 

 




designer

namespace Banana

{

    partial class ucTitle

    {

        /// <summary>

        /// 필수 디자이너 변수입니다.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

 

        /// <summary>

        /// 사용 중인 모든 리소스를 정리합니다.

        /// </summary>

        /// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region 구성 요소 디자이너에서 생성한 코드

 

        /// <summary>

        /// 디자이너 지원에 필요한 메서드입니다.

        /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.

        /// </summary>

        private void InitializeComponent()

        {

            this.txtTitle = new System.Windows.Forms.Label();

            this.QMark = new System.Windows.Forms.PictureBox();

            ((System.ComponentModel.ISupportInitialize)(this.QMark)).BeginInit();

            this.SuspendLayout();

            //

            // txtTitle

            //

            this.txtTitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

            | System.Windows.Forms.AnchorStyles.Left)));

            this.txtTitle.AutoSize = true;

            this.txtTitle.BackColor = System.Drawing.Color.Transparent;

            this.txtTitle.Font = new System.Drawing.Font("굴림", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(129)));

            this.txtTitle.Location = new System.Drawing.Point(5, 7);

            this.txtTitle.Name = "txtTitle";

            this.txtTitle.Padding = new System.Windows.Forms.Padding(10, 0, 0, 0);

            this.txtTitle.Size = new System.Drawing.Size(39, 12);

            this.txtTitle.TabIndex = 0;

            this.txtTitle.Text = "title";

            this.txtTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

            this.txtTitle.SizeChanged += new System.EventHandler(this.txtTitle_SizeChanged);

            //

            // QMark

            //

            this.QMark.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

            | System.Windows.Forms.AnchorStyles.Left)));

            this.QMark.BackColor = System.Drawing.Color.Transparent;

            this.QMark.Image = global::Banana.Properties.Resources.questionmark;

            this.QMark.Location = new System.Drawing.Point(5, 5);

            this.QMark.Name = "QMark";

            this.QMark.Size = new System.Drawing.Size(16, 16);

            this.QMark.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

            this.QMark.TabIndex = 5;

            this.QMark.TabStop = false;

            this.QMark.Visible = false;

            this.QMark.Cursor = System.Windows.Forms.Cursors.Help;

            //

            // ucTitle

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.BackColor = System.Drawing.SystemColors.ActiveCaption;

            this.Controls.Add(this.QMark);

            this.Controls.Add(this.txtTitle);

            this.Name = "ucTitle";

            this.Padding = new System.Windows.Forms.Padding(5);

            this.Size = new System.Drawing.Size(516, 26);

            this.Load += new System.EventHandler(this.ucTitle_Load);

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.ucTitle_Paint);

            this.Resize += new System.EventHandler(this.ucTitle_Resize);

            ((System.ComponentModel.ISupportInitialize)(this.QMark)).EndInit();

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        private System.Windows.Forms.Label txtTitle;

        private System.Windows.Forms.PictureBox QMark;

    }

}

 




Posted by motolies
,