1. 程式人生 > >C#自定義控制元件美化的RadionButton和CheckButton加入了Image圖示

C#自定義控制元件美化的RadionButton和CheckButton加入了Image圖示

前段時間剛在CSDN上傳了一個美化的Radionbutton和CheckButton,但是加入imagelist後,顯示不出來,在這裡我對程式碼做了一些更改,希望對大家的學習有幫助。

控制元件效果圖如下:


1.      在VS2017環境下新建一個自定義使用者控制元件

2.      更改父類為RadionButton

3.      更改一下MyRadionButton.Designer.cs

 

  private void InitializeComponent()

        {

            components = new System.ComponentModel.Container();

        }

 

4.      MyRadionButton.cs的主要程式碼如下:

using System;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Drawing;

using System.Drawing.Drawing2D;

usingSystem.Data;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

//usingSystem.Windows.Media;

 

namespaceWpfCustomControlLibrary1

{

    public partial class myRadiobutton :RadioButton

    {

        private bool FontASPECT = false;        //判斷字型的方向

        private int Measurement = 255;          //漸變的初始值

        LinearGradientBrush periphery_br;       //外圓的顏色

        LinearGradientBrush central_br;         //滑鼠移入控制元件時中圓的顏色

        LinearGradientBrush nocentral_br;       //無操作時中圓的顏色

        LinearGradientBrush stipple_br;         //內圓選中的顏色

        LinearGradientBrush nostipple_br;       //內圓無選中的顏色

 

         #region   -------屬性----------

        private Color periphercolor = Color.Red;

        [Description("設定外橢圓的漸變色"),Category("自定義屬性")]

        public Color Periphercolor

        {

            get { return this.periphercolor; }

            set

            {

                this.periphercolor = value;

                this.Invalidate();

            }

        }

        private Color centralcolor = Color.Red;

        [Description("設定中間橢圓選中時的漸變色"),Category("自定義屬性")]

        public Color Centdralcolor

        {

            get { return this.centralcolor; }

            set

            {

                this.centralcolor = value;

                this.Invalidate();

            }

        }

        private Color nocentralcolor =Color.Red;

        [Description("設定中間橢圓無操作時的漸變色"), Category("自定義屬性")]

        public Color Nocentdralcolor

        {

            get { return this.nocentralcolor; }

            set

            {

                this.nocentralcolor = value;

                this.Invalidate();

            }

        }

        private Color stipplecolor = Color.Red;

        [Description("設定內橢圓選中時的漸變色"),Category("自定義屬性")]

        public Color Stipplecolor

        {

            get { return this.stipplecolor; }

            set

            {

                this.stipplecolor = value;

                this.Invalidate();

            }

        }

        private Color nostipplecolor =Color.Red;

        [Description("設定內橢圓無操作時的漸變色"),Category("自定義屬性")]

        public Color Nostipplecolor

        {

            get { return this.nostipplecolor; }

            set

            {

                this.nostipplecolor = value;

                this.Invalidate();

            }

        }

        #endregion

        public myRadiobutton()

        {

           // SetStyles();

            InitializeComponent();

        }

        private void SetStyles()

        {

            base.SetStyle(

 

            ControlStyles.UserPaint |

            ControlStyles.OptimizedDoubleBuffer|

            ControlStyles.AllPaintingInWmPaint|

            ControlStyles.ResizeRedraw |

           ControlStyles.SupportsTransparentBackColor, true);

            base.UpdateStyles();

        }

        ///<summary>

        ///控制元件在重繪時觸發

        ///</summary>

        ///

        protected override voidOnPaint(PaintEventArgs e)

        {

             base.OnPaint(e);

            //e.Graphics.Clip = new Region(newRectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width,e.ClipRectangle.Height + this.Image.Height + 20));

           e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle);

            e.Graphics.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

 

            Rectangle boxrect = newRectangle(e.ClipRectangle.X, e.ClipRectangle.Y,SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

           Point imagpoint = newPoint(e.ClipRectangle.X , e.ClipRectangle.Y);

            Rectangle strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width+2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 - SystemInformation.SmallIconSize.Width,e.ClipRectangle.Height);

          

            if (this.Image != null)

            { switch (this.TextImageRelation)

                {

                    caseSystem.Windows.Forms.TextImageRelation.TextAboveImage:

                        imagpoint = new Point(e.ClipRectangle.X +SystemInformation.SmallIconSize.Width + 2 + (strrect.Width - Image.Width) / 2,e.ClipRectangle.Y+Font.Height+2);

                        strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y-Font.Height, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

                        break;

                    caseSystem.Windows.Forms.TextImageRelation.ImageAboveText:

                        imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2 +(strrect.Width - Image.Width) / 2, e.ClipRectangle.Y);

                        strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y+Font.Height, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

                        break;

                    caseSystem.Windows.Forms.TextImageRelation.ImageBeforeText:

                        imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y);

                        strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width +4+Image.Width, e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

                        break;

                    caseSystem.Windows.Forms.TextImageRelation.TextBeforeImage:

                        SizeF sizeF = e.Graphics.MeasureString(this.Text,this.Font);

                    

                        imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width +2+(int)sizeF.Width, e.ClipRectangle.Y);

                        strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

                        break;

                    default:

                        imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2 +(strrect.Width - Image.Width) / 2,e.ClipRectangle.Y);

                        strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);

                        break;

                   }

                drawimage(e.Graphics,imagpoint);//繪製圖像

            }

      

        

            if (FontASPECT=getaspect())

            {

                boxrect.X = e.ClipRectangle.X +e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width;

                strrect.X = e.ClipRectangle.X;

            }

         

            Point mousepos =this.PointToClient(Control.MousePosition);

            bool above =e.ClipRectangle.Contains(mousepos);

 

           drawbox(e.Graphics,boxrect,above);//繪製選擇框

            drawtext(e.Graphics,strrect);    //繪製文字

            if (!Enabled)

                e.Graphics.FillRectangle(newSolidBrush(System.Drawing.Color.FromArgb(127, SystemColors.Control)),e.ClipRectangle);

       

               

           

        }

       

        ///<summary>

        ///繪製單選控制元件的圖案

        /// </summary>

        ///<param g="Graphics">封裝一個繪圖的物件</param>

        ///<paramrect="Rectangel">單選圖案的繪製區域</param>

        ///<param Above="bool">判斷滑鼠是否在控制元件上</param>

        private void drawbox(Graphicsg,Rectangle rect,bool Above)

        { 

            int opactiy = Measurement;

            periphery_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 2,periphercolor),Color.FromArgb(opactiy,periphercolor),LinearGradientMode.ForwardDiagonal);

 

            opactiy = (int)(.4f * opactiy +.5f);

            central_br = newLinearGradientBrush(rect,Color.FromArgb(opactiy/10,centralcolor),Color.FromArgb(opactiy,centralcolor),LinearGradientMode.ForwardDiagonal);

 

            opactiy = (int)(.4f * opactiy +.5f);

            nocentral_br  = new LinearGradientBrush(rect,Color.FromArgb(opactiy / 10, nocentralcolor), Color.FromArgb(opactiy,nocentralcolor), LinearGradientMode.ForwardDiagonal);

 

            opactiy = Measurement;

            stipple_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 10, stipplecolor),Color.FromArgb(opactiy, stipplecolor), LinearGradientMode.BackwardDiagonal);

 

            opactiy = (int)(.4f * opactiy +.5f);

            nostipple_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 10, nostipplecolor),Color.FromArgb(opactiy, nostipplecolor), LinearGradientMode.ForwardDiagonal);

            int size = this.Font.Height;

            Rectangle box = newRectangle(rect.X+((rect.Width-size)/2),rect.Y+((rect.Height-size)/2),size-2,size-2);

            Rectangle glyph = newRectangle(box.X+3,box.Y+3,box.Width-6,box.Height-6);

            g.FillEllipse(newSolidBrush(SystemColors.Window),box);

            if(Above && this.Enabled)

            {

                g.DrawEllipse(newPen(central_br,(float)(3)),newRectangle(box.X+2,box.Y+2,box.Width-4,box.Height-4));

 

            }

            else

            {

                g.DrawEllipse(newPen(nocentral_br, (float)(3)), new Rectangle(box.X + 2, box.Y + 2, box.Width -4, box.Height - 4));

 

            }

            g.DrawEllipse(newPen(periphery_br,(float)(1.5)),box);

            g.FillEllipse(newSolidBrush(SystemColors.Window), glyph);

            if (this.Checked)

                g.FillEllipse(stipple_br,glyph);

            else

                g.FillEllipse(nostipple_br,glyph);

        }

 

        ///<summary>

        ///繪製文字

        ///</summary>

        ///<paramg="Grdaphics">封裝一個繪圖物件</param>

        ///<paramrect="Rectangle">繪製文字的區域</param>

        private void drawtext(Graphicsg,Rectangle rect)

        {

            StringFormat tem_stringf = newStringFormat();

            tem_stringf.Alignment =StringAlignment.Near;

            tem_stringf.LineAlignment =StringAlignment.Center;

            if (FontASPECT)

                tem_stringf.FormatFlags =StringFormatFlags.DirectionRightToLeft;

            if (!FontASPECT)

                g.DrawString(this.Text,this.Font, new SolidBrush(this.ForeColor), rect, tem_stringf);

            else

            {

                rect.X = rect.X -SystemInformation.SmallIconSize.Width / 2 + 2;

               g.DrawString(this.Text,this.Font, new SolidBrush(this.ForeColor),rect,tem_stringf);

            }

        }

 

        /// <summary>

        /// 繪製imagelist圖示

       /// </summary>

        /// <paramname="g"></param>

        /// <paramname="rect"></param>

        private void drawimage(Graphics g,Pointimagpoint)

        {

            g.DrawImage(this.Image,imagpoint);

           // ImageList.Draw(g, new Point(rect.X,rect.Y),this.ImageIndex);

        }

 

        ///<summary>

        ///獲取文字讀取方向

        ///</summary>

        ///<return>布林型</return>

        private bool getaspect()

        {

            bool tem_aspect = false;

            if (this.RightToLeft == RightToLeft.Yes)

                tem_aspect = true;

            if (this.RightToLeft ==RightToLeft.No)

                tem_aspect = false;

            return tem_aspect;

        }

 

   

 

    }

}