Creating Custom Controls In C#

 
 
     

What are Custom Controls

Well Custom Controls are nothing but just graphics. It is used to improve performance of your created application. Look Visual Studio, you can see MenuStrip different from basic controls in system. It looks better than just simple controls in .NET or any language.

So how to create custom controls

It is simple, just call graphics method and draw shapes correspond to the controls.

Consider I want to create a custom button, you know button is a rectangle in system,so just fill rectangle using that buttons co-ordinates (x,y,width,height) with LinearGradientBrush or SolidBrush in Paint() method in C#. C# provides ClientRectangle method to get all co-ordinates of the controls in which rectangle is used. There are many components in which rectangle is used like Button, Panel, CheckBox, TabControl, etc. 

In this article I created sub classes with super classes of Button, Labels etc, so that I can drag and drop created custom controls into my form.
 
Requirements 
  • Visual Studio Professional 2013
  • .NET Framework 4.5 or higher  
Download the source code.  
 
Start 

Step 1: Start Visual Studio and create new Windows Forms Application in C#.

Step 2: Now go to Project, then Add Class

In opened dialog enter class name ButtonZ if you are creating a custom button or enter class name as you wish, just change class name in codes.

Step 3:
 
Copy and paste the following defined custom controls code into your created class code. If you created a class ButtonZ then copy and paste the following ButtonZ code. 
 
Step 4: Now run your form and exit.

In ToolBox, you can see created ButtonZ controls as shown in above image 3. Drag and Drop ButtonZ into your form like basic controls and change values in Properties as shown in above image 2 like StartColor, EndColor, TextLocation_X, TextLocation_Y, Transparent1, Trnsparent2, etc.
 
Step 5: Run your form. 
 
Download the source code to view all custom controls.
Also to see Cutom Windows Form.
Dont know how to customize windows form in C# ?
 
 
Watch video for how to create custom controls in C# using following codes
 
 
C# Custom Controls Codes: 

Custom Button:
   A different shaped buttons like Rectangular,RoundRectangular,Circular etc. 
 
 
Class Name: ButtonZ 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Windows.Forms;  
  7. using System.Drawing.Drawing2D;  
  8.   
  9.   
  10. namespace Custom_Controls_in_CS  
  11. {  
  12.     public class ButtonZ : System.Windows.Forms.Button  
  13.     {  
  14.         Color clr1, clr2;  
  15.         private Color color1 = Color.DodgerBlue;  
  16.         private Color color2 = Color.MidnightBlue;  
  17.         private Color m_hovercolor1 = Color.Turquoise;  
  18.         private Color m_hovercolor2 = Color.DarkSlateGray;  
  19.         private int color1Transparent = 250;  
  20.         private int color2Transparent = 250;  
  21.         private Color clickcolor1 = Color.Yellow;  
  22.         private Color clickcolor2 = Color.Red;  
  23.         private int angle = 90;  
  24.         private int textX = 100;  
  25.         private int textY = 25;  
  26.         private String text = "";  
  27.         public Color buttonborder_1 = Color.FromArgb(220, 220, 220);  
  28.         public Color buttonborder_2 = Color.FromArgb(150, 150, 150);  
  29.         public Boolean showButtonText = true;  
  30.         public int borderWidth = 2;  
  31.         public Color borderColor = Color.Transparent;  
  32.   
  33.         public enum ButtonsShapes  
  34.         {  
  35.             Rect,  
  36.             RoundRect,  
  37.             Circle  
  38.         }  
  39.   
  40.         ButtonsShapes buttonShape;  
  41.   
  42.         public ButtonsShapes ButtonShape  
  43.         {  
  44.             get { return buttonShape; }  
  45.             set  
  46.             {  
  47.                 buttonShape = value; Invalidate();  
  48.             }  
  49.         }  
  50.   
  51.         public String ButtonText  
  52.         {  
  53.             get { return text; }  
  54.             set { text = value; Invalidate(); }  
  55.         }  
  56.   
  57.         public int BorderWidth  
  58.         {  
  59.             get { return borderWidth; }  
  60.             set { borderWidth = value; Invalidate(); }  
  61.         }  
  62.   
  63.   
  64.         void SetBorderColor(Color bdrColor)  
  65.         {  
  66.             int red = bdrColor.R - 40;  
  67.             int green = bdrColor.G - 40;  
  68.             int blue = bdrColor.B - 40;  
  69.             if (red < 0)  
  70.                 red = 0;  
  71.             if (green < 0)  
  72.                 green = 0;  
  73.             if (blue < 0)  
  74.                 blue = 0;  
  75.   
  76.             buttonborder_1 = Color.FromArgb(red, green, blue);  
  77.             buttonborder_2 = bdrColor;  
  78.         }  
  79.   
  80.   
  81.         public Color BorderColor  
  82.         {  
  83.             get { return borderColor; }  
  84.             set  
  85.             {  
  86.                 borderColor = value;  
  87.                 if (borderColor == Color.Transparent)  
  88.                 {  
  89.                     buttonborder_1 = Color.FromArgb(220, 220, 220);  
  90.                     buttonborder_2 = Color.FromArgb(150, 150, 150);  
  91.                 }  
  92.                 else  
  93.                 {  
  94.                     SetBorderColor(borderColor);  
  95.                 }  
  96.   
  97.             }  
  98.         }  
  99.   
  100.         public Color StartColor  
  101.         {  
  102.             get { return color1; }  
  103.             set { color1 = value; Invalidate(); }  
  104.         }  
  105.         public Color EndColor  
  106.         {  
  107.             get { return color2; }  
  108.             set { color2 = value; Invalidate(); }  
  109.         }  
  110.         public Color MouseHoverColor1  
  111.         {  
  112.             get { return m_hovercolor1; }  
  113.             set { m_hovercolor1 = value; Invalidate(); }  
  114.         }  
  115.         public Color MouseHoverColor2  
  116.         {  
  117.             get { return m_hovercolor2; }  
  118.             set { m_hovercolor2 = value; Invalidate(); }  
  119.         }  
  120.         public Color MouseClickColor1  
  121.         {  
  122.             get { return clickcolor1; }  
  123.             set { clickcolor1 = value; Invalidate(); }  
  124.         }  
  125.         public Color MouseClickColor2  
  126.         {  
  127.             get { return clickcolor2; }  
  128.             set { clickcolor2 = value; Invalidate(); }  
  129.         }  
  130.   
  131.         public int Transparent1  
  132.         {  
  133.             get { return color1Transparent; }  
  134.             set  
  135.             {  
  136.                 color1Transparent = value;  
  137.                 if (color1Transparent > 255)  
  138.                 {  
  139.                     color1Transparent = 255;  
  140.                     Invalidate();  
  141.                 }  
  142.                 else  
  143.                     Invalidate();  
  144.             }  
  145.         }  
  146.   
  147.         public int Transparent2  
  148.         {  
  149.             get { return color2Transparent; }  
  150.             set  
  151.             {  
  152.                 color2Transparent = value;  
  153.                 if (color2Transparent > 255)  
  154.                 {  
  155.                     color2Transparent = 255;  
  156.                     Invalidate();  
  157.                 }  
  158.                 else  
  159.                     Invalidate();  
  160.             }  
  161.         }  
  162.   
  163.         public int GradientAngle  
  164.         {  
  165.             get { return angle; }  
  166.             set { angle = value; Invalidate(); }  
  167.         }  
  168.   
  169.         public int TextLocation_X  
  170.         {  
  171.             get { return textX; }  
  172.             set { textX = value; Invalidate(); }  
  173.         }  
  174.         public int TextLocation_Y  
  175.         {  
  176.             get { return textY; }  
  177.             set { textY = value; Invalidate(); }  
  178.         }  
  179.   
  180.         public Boolean ShowButtontext  
  181.         {  
  182.             get { return showButtonText; }  
  183.             set { showButtonText = value; Invalidate(); }  
  184.         }  
  185.   
  186.   
  187.         public ButtonZ()  
  188.         {  
  189.             this.Size = new Size(100, 40);  
  190.             this.BackColor = Color.Transparent;  
  191.             this.FlatStyle = FlatStyle.Flat;  
  192.             this.FlatAppearance.BorderSize = 0;  
  193.             this.FlatAppearance.MouseOverBackColor = Color.Transparent;  
  194.             this.FlatAppearance.MouseDownBackColor = Color.Transparent;  
  195.             text = this.Text;  
  196.         }  
  197.   
  198.   
  199.         //method mouse enter  
  200.         protected override void OnMouseEnter(EventArgs e)  
  201.         {  
  202.             base.OnMouseEnter(e);  
  203.             clr1 = color1;  
  204.             clr2 = color2;  
  205.             color1 = m_hovercolor1;  
  206.             color2 = m_hovercolor2;  
  207.         }  
  208.         //method mouse leave  
  209.         protected override void OnMouseLeave(EventArgs e)  
  210.         {  
  211.             base.OnMouseLeave(e);  
  212.             color1 = clr1;  
  213.             color2 = clr2;  
  214.             SetBorderColor(borderColor);  
  215.         }  
  216.   
  217.         protected override void OnMouseDown(MouseEventArgs mevent)  
  218.         {  
  219.             base.OnMouseDown(mevent);  
  220.             color1 = clickcolor1;  
  221.             color2 = clickcolor2;  
  222.               
  223.             int red = borderColor.R - 40;  
  224.             int green = borderColor.G - 40;  
  225.             int blue = borderColor.B - 40;  
  226.             if (red < 0)  
  227.                 red = 0;  
  228.             if (green < 0)  
  229.                 green = 0;  
  230.             if (blue < 0)  
  231.                 blue = 0;  
  232.   
  233.             buttonborder_2 = Color.FromArgb(red, green, blue);  
  234.             buttonborder_1 = borderColor;  
  235.             this.Invalidate();  
  236.         }  
  237.   
  238.         protected override void OnMouseUp(MouseEventArgs mevent)  
  239.         {  
  240.             base.OnMouseUp(mevent);  
  241.             OnMouseLeave(mevent);  
  242.             color1 = clr1;  
  243.             color2 = clr2;  
  244.             SetBorderColor(borderColor);  
  245.             this.Invalidate();  
  246.         }  
  247.       
  248.         protected override void OnLostFocus(EventArgs e)  
  249.         {  
  250.             base.OnLostFocus(e);  
  251.             color1 = clr1;  
  252.             color2 = clr2;  
  253.             this.Invalidate();  
  254.         }  
  255.   
  256.         protected override void OnResize(EventArgs e)  
  257.         {  
  258.             base.OnResize(e);  
  259.             textX = (int)((this.Width / 3) - 1);  
  260.             textY = (int)((this.Height / 3) + 5);  
  261.         }  
  262.   
  263.   
  264.         //draw circular button function  
  265.         void DrawCircularButton(Graphics g)  
  266.         {  
  267.             Color c1 = Color.FromArgb(color1Transparent, color1);  
  268.             Color c2 = Color.FromArgb(color2Transparent, color2);  
  269.   
  270.   
  271.             Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);  
  272.             g.FillEllipse(b, 5, 5, this.Width - 10, this.Height - 10);  
  273.   
  274.   
  275.             for (int i = 0; i < borderWidth; i++)  
  276.             {  
  277.                 g.DrawArc(new Pen(new SolidBrush(buttonborder_1)), 5 + i, 5, this.Width - 10, this.Height - 10, 120, 180);  
  278.                 g.DrawArc(new Pen(new SolidBrush(buttonborder_2)), 5, 5, this.Width - (10 + i), this.Height - 10, 300, 180);  
  279.             }  
  280.   
  281.   
  282.   
  283.   
  284.             if (showButtonText)  
  285.             {  
  286.                 Point p = new Point(textX, textY);  
  287.                 SolidBrush frcolor = new SolidBrush(this.ForeColor);  
  288.                 g.DrawString(text, this.Font, frcolor, p);  
  289.             }  
  290.   
  291.             b.Dispose();  
  292.         }  
  293.   
  294.         //draw rectangular button function  
  295.         void DrawRectangularButton(Graphics g)  
  296.         {  
  297.             Color c1 = Color.FromArgb(color1Transparent, color1);  
  298.             Color c2 = Color.FromArgb(color2Transparent, color2);  
  299.   
  300.   
  301.             Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);  
  302.             g.FillRectangle(b, 0, 0, this.Width, this.Height);  
  303.   
  304.   
  305.             for (int i = 0; i < borderWidth; i++)  
  306.             {  
  307.                 g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), this.Width - i, 0, this.Width - i, this.Height);  
  308.                 g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), 0, this.Height - i, this.Width, this.Height - i);  
  309.   
  310.                 g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0 + i, 0, 0 + i, this.Height);  
  311.                 g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0, 0 + i, this.Width, i);  
  312.             }  
  313.   
  314.   
  315.   
  316.             if (showButtonText)  
  317.             {  
  318.                 Point p = new Point(textX, textY);  
  319.                 SolidBrush frcolor = new SolidBrush(this.ForeColor);  
  320.                 g.DrawString(text, this.Font, frcolor, p);  
  321.             }  
  322.   
  323.             b.Dispose();  
  324.         }  
  325.   
  326.   
  327.         //draw round rectangular button function  
  328.         void DrawRoundRectangularButton(Graphics g)  
  329.         {  
  330.             Color c1 = Color.FromArgb(color1Transparent, color1);  
  331.             Color c2 = Color.FromArgb(color2Transparent, color2);  
  332.   
  333.   
  334.             Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);  
  335.   
  336.             Region region = new System.Drawing.Region(new Rectangle(5, 5, this.Width, this.Height));  
  337.   
  338.             GraphicsPath grp = new GraphicsPath();  
  339.             grp.AddArc(5, 5, 40, 40, 180, 90);  
  340.             grp.AddLine(25, 5, this.Width - 25, 5);  
  341.             grp.AddArc(this.Width - 45, 5, 40, 40, 270, 90);  
  342.             grp.AddLine(this.Width - 5, 25, this.Width - 5, this.Height - 25);  
  343.             grp.AddArc(this.Width - 45, this.Height - 45, 40, 40, 0, 90);  
  344.             grp.AddLine(25, this.Height - 5, this.Width - 25, this.Height - 5);  
  345.             grp.AddArc(5, this.Height - 45, 40, 40, 90, 90);  
  346.             grp.AddLine(5, 25, 5, this.Height - 25);  
  347.   
  348.             region.Intersect(grp);  
  349.   
  350.             g.FillRegion(b, region);  
  351.   
  352.             for (int i = 0; i < borderWidth; i++)  
  353.             {  
  354.                 g.DrawArc(new Pen(buttonborder_1), 5 + i, 5 + i, 40, 40, 180, 90);  
  355.                 g.DrawLine(new Pen(buttonborder_1), 25, 5 + i, this.Width - 25, 5 + i);  
  356.                 g.DrawArc(new Pen(buttonborder_1), this.Width - 45 - i, 5 + i, 40, 40, 270, 90);  
  357.                 g.DrawLine(new Pen(buttonborder_1), 5 + i, 25, 5 + i, this.Height - 25);  
  358.   
  359.   
  360.                 g.DrawLine(new Pen(buttonborder_2), this.Width - 5 - i, 25, this.Width - 5 - i, this.Height - 25);  
  361.                 g.DrawArc(new Pen(buttonborder_2), this.Width - 45 - i, this.Height - 45 - i, 40, 40, 0, 90);  
  362.                 g.DrawLine(new Pen(buttonborder_2), 25, this.Height - 5 - i, this.Width - 25, this.Height - 5 - i);  
  363.                 g.DrawArc(new Pen(buttonborder_2), 5 + i, this.Height - 45 - i, 40, 40, 90, 90);  
  364.   
  365.             }  
  366.   
  367.   
  368.   
  369.             if (showButtonText)  
  370.             {  
  371.                 Point p = new Point(textX, textY);  
  372.                 SolidBrush frcolor = new SolidBrush(this.ForeColor);  
  373.                 g.DrawString(text, this.Font, frcolor, p);  
  374.             }  
  375.   
  376.             b.Dispose();  
  377.         }  
  378.   
  379.   
  380.         protected override void OnPaint(PaintEventArgs e)  
  381.         {  
  382.             base.OnPaint(e);  
  383.   
  384.             switch (buttonShape)  
  385.             {  
  386.                 case ButtonsShapes.Circle:  
  387.                     this.DrawCircularButton(e.Graphics);  
  388.                     break;  
  389.   
  390.                 case ButtonsShapes.Rect:  
  391.                     this.DrawRectangularButton(e.Graphics);  
  392.                     break;  
  393.   
  394.                 case ButtonsShapes.RoundRect:  
  395.                     this.DrawRoundRectangularButton(e.Graphics);  
  396.                     break;  
  397.             }  
  398.         }  
  399.   
  400.   
  401.     }  
  402. }   
Custom Label
 


Class Name: LabelZ
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Drawing.Drawing2D;  
  7. using System.Windows.Forms;  
  8. namespace LabelZ  
  9. {  
  10.    public class LabelZ : System.Windows.Forms.Label  
  11.     {  
  12.         private Color color1 = Color.LightGreen;  
  13.         private Color color2 = Color.DarkBlue;  
  14.         private int color1Transparent = 150;  
  15.         private int color2Transparent = 150;  
  16.         private int angle = 90;  
  17.         private String text = "MyLabelX";  
  18.   
  19.        //Create Properties  
  20.         public String DisplayText  
  21.         {  
  22.             get { return text; }  
  23.             set { text = value; Invalidate(); }  
  24.         }  
  25.   
  26.         public Color StartColor  
  27.         {  
  28.             get { return color1; }  
  29.             set { color1 = value; Invalidate(); }  
  30.         }  
  31.   
  32.         public Color EndColor  
  33.         {  
  34.             get { return color2; }  
  35.             set { color2 = value; Invalidate(); }  
  36.         }  
  37.   
  38.         public int Transparent1  
  39.         {  
  40.             get { return color1Transparent; }  
  41.             set  
  42.             {  
  43.                 color1Transparent = value;  
  44.                 if (color1Transparent > 255)  
  45.                 {  
  46.                     color1Transparent = 255;  
  47.                     Invalidate();  
  48.                 }  
  49.                 else  
  50.                     Invalidate();  
  51.             }  
  52.         }  
  53.   
  54.         public int Transparent2  
  55.         {  
  56.             get { return color2Transparent; }  
  57.             set  
  58.             {  
  59.                 color2Transparent = value;  
  60.                 if (color2Transparent > 255)  
  61.                 {  
  62.                     color2Transparent = 255;  
  63.                     Invalidate();  
  64.                 }  
  65.                 else  
  66.                     Invalidate();  
  67.             }  
  68.         }  
  69.   
  70.         public int GradientAngle  
  71.         {  
  72.             get { return angle; }  
  73.             set { angle = value; Invalidate(); }  
  74.         }  
  75.   
  76.         public LabelZ()  
  77.         {  
  78.             this.ForeColor = Color.Transparent;  
  79.         }  
  80.   
  81.         protected override void OnPaint(PaintEventArgs e)  
  82.         {  
  83.             base.OnPaint(e);  
  84.             this.Text = text;  
  85.             Color c1 = Color.FromArgb(color1Transparent, color1);  
  86.             Color c2 = Color.FromArgb(color2Transparent, color2);  
  87.             //draw a string of text label  
  88.             Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 50, 50), c1, c2, angle);  
  89.             e.Graphics.DrawString(this.Text, this.Font, b, new Point(0, 0));  
  90.         }  
  91.     }  
  92. }   
Custom TabControl
 


Class Name: TabControlZ
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Windows.Forms;  
  6. using System.Drawing.Drawing2D;  
  7. namespace TabControlZ  
  8. {  
  9.    public class TabControlZ : System.Windows.Forms.TabControl  
  10.     {  
  11.         private Color nonactive_color1 = Color.LightGreen;  
  12.         private Color nonactive_color2 = Color.DarkBlue;  
  13.         private Color active_color1 = Color.Yellow;  
  14.         private Color active_color2 = Color.DarkOrange;  
  15.         public Color forecolor = Color.Navy;  
  16.         private int color1Transparent = 150;  
  17.         private int color2Transparent = 150;  
  18.         private int angle = 90;  
  19.         private Color closebuttoncolor = Color.Red;  
  20.   
  21.        //Create Properties to read values  
  22.         public Color ActiveTabStartColor  
  23.         {  
  24.             get { return active_color1; }  
  25.             set { active_color1 = value; Invalidate(); }  
  26.         }  
  27.   
  28.   
  29.         public Color ActiveTabEndColor  
  30.         {  
  31.             get { return active_color2; }  
  32.             set { active_color2 = value; Invalidate(); }  
  33.         }  
  34.   
  35.   
  36.         public Color NonActiveTabStartColor  
  37.         {  
  38.             get { return nonactive_color1; }  
  39.             set { nonactive_color1 = value; Invalidate(); }  
  40.         }  
  41.   
  42.   
  43.         public Color NonActiveTabEndColor  
  44.         {  
  45.             get { return nonactive_color2; }  
  46.             set { nonactive_color2 = value; Invalidate(); }  
  47.         }  
  48.   
  49.   
  50.         public int Transparent1  
  51.         {  
  52.             get { return color1Transparent; }  
  53.             set  
  54.             {  
  55.                 color1Transparent = value;  
  56.                 if (color1Transparent > 255)  
  57.                 {  
  58.                     color1Transparent = 255;  
  59.                     Invalidate();  
  60.                 }  
  61.                 else  
  62.                     Invalidate();  
  63.             }  
  64.         }  
  65.   
  66.   
  67.         public int Transparent2  
  68.         {  
  69.             get { return color2Transparent; }  
  70.             set  
  71.             {  
  72.                 color2Transparent = value;  
  73.                 if (color2Transparent > 255)  
  74.                 {  
  75.                     color2Transparent = 255;  
  76.                     Invalidate();  
  77.                 }  
  78.                 else  
  79.                     Invalidate();  
  80.             }  
  81.         }  
  82.   
  83.   
  84.         public int GradientAngle  
  85.         {  
  86.             get { return angle; }  
  87.             set { angle = value; Invalidate(); }  
  88.         }  
  89.   
  90.   
  91.         public Color TextColor  
  92.         {  
  93.             get { return forecolor; }  
  94.             set { forecolor = value; Invalidate(); }  
  95.         }  
  96.   
  97.         public Color CloseButtonColor  
  98.         {  
  99.             get { return closebuttoncolor; }  
  100.             set { closebuttoncolor = value; Invalidate(); }  
  101.         }  
  102.   
  103.   
  104.   
  105.         public TabControlZ()  
  106.         {  
  107.             this.DrawMode = TabDrawMode.OwnerDrawFixed;  
  108.             this.Padding = new System.Drawing.Point(22, 4);  
  109.         }  
  110.   
  111.         protected override void OnPaint(PaintEventArgs pe)  
  112.         {  
  113.             base.OnPaint(pe);  
  114.         }  
  115.   
  116.   
  117.         //method for drawing tab items   
  118.         protected override void OnDrawItem(DrawItemEventArgs e)  
  119.         {  
  120.             base.OnDrawItem(e);  
  121.             Rectangle rc = GetTabRect(e.Index);  
  122.   
  123.             //if tab is selected  
  124.             if (this.SelectedTab == this.TabPages[e.Index])  
  125.             {  
  126.                 Color c1 = Color.FromArgb(color1Transparent, active_color1);  
  127.                 Color c2 = Color.FromArgb(color2Transparent, active_color2);  
  128.                 using (LinearGradientBrush br = new LinearGradientBrush(rc, c1, c2, angle))  
  129.                 {  
  130.                     e.Graphics.FillRectangle(br, rc);  
  131.                 }  
  132.             }  
  133.             else  
  134.             {  
  135.                 Color c1 = Color.FromArgb(color1Transparent, nonactive_color1);  
  136.                 Color c2 = Color.FromArgb(color2Transparent, nonactive_color2);  
  137.                 using (LinearGradientBrush br = new LinearGradientBrush(rc, c1, c2, angle))  
  138.                 {  
  139.                     e.Graphics.FillRectangle(br, rc);  
  140.                 }  
  141.             }  
  142.   
  143.             this.TabPages[e.Index].BorderStyle = BorderStyle.FixedSingle;  
  144.             this.TabPages[e.Index].ForeColor = SystemColors.ControlText;  
  145.   
  146.             //draw close button on tabs  
  147.   
  148.             Rectangle paddedBounds = e.Bounds;  
  149.             paddedBounds.Inflate(-5, -4);  
  150.             e.Graphics.DrawString(this.TabPages[e.Index].Text, this.Font, new SolidBrush(forecolor), paddedBounds);  
  151.   
  152.             Point pad = this.Padding;  
  153.   
  154.             //drawing close button to tab items  
  155.             e.Graphics.DrawString("X"new Font("Microsoft YaHei UI", 10, FontStyle.Bold), new SolidBrush(closebuttoncolor), e.Bounds.Right + 1 - 18, e.Bounds.Top + pad.Y-2);  
  156.             e.DrawFocusRectangle();  
  157.         }  
  158.   
  159.   
  160.         //action for when mouse click on close button  
  161.         protected override void OnMouseDown(MouseEventArgs e)  
  162.         {  
  163.             base.OnMouseDown(e);  
  164.             for (int i = 0; i < this.TabPages.Count; i++)  
  165.             {  
  166.                 Rectangle r = this.GetTabRect(i);  
  167.                 Rectangle closeButton = new Rectangle(r.Right + 1 - 15, r.Top + 4, 12, 12);  
  168.                 if (closeButton.Contains(e.Location))  
  169.                 {  
  170.                     if (MessageBox.Show("Do you want to Close this Tab ?""Close or Not", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)  
  171.                     {  
  172.                         this.TabPages.RemoveAt(i);  
  173.                         break;  
  174.                     }  
  175.                 }  
  176.             }  
  177.         }  
  178.     }  
  179. }   
Custom MenuStrip
 


In this MenuStrip, I used Black colors. Change colors in the following code as you wish. In MenuStripZ code, I used MyMenuRenderer Class.

Class Name: MenuStripZ
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Drawing;  
  6. using System.Drawing.Drawing2D;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace Custom_Controls_in_CS  
  10. {  
  11.     public class MenuStripZ : MenuStrip  
  12.     {  
  13.         public MenuStripZ()  
  14.         {  
  15.             this.Renderer = new MyMenuRenderer();  
  16.         }  
  17.     }  
  18.   
  19.     public class MyMenuRenderer : ToolStripRenderer  
  20.     {  
  21.         protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)  
  22.         {  
  23.             base.OnRenderMenuItemBackground(e);  
  24.   
  25.             if (e.Item.Enabled)  
  26.             {  
  27.                 if (e.Item.IsOnDropDown == false && e.Item.Selected)  
  28.                 {  
  29.                     var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);  
  30.                     var rect2 = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);  
  31.                     e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, 60, 60)), rect);  
  32.                     e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);  
  33.                     e.Item.ForeColor = Color.White;  
  34.                 }  
  35.                 else  
  36.                 {  
  37.                     e.Item.ForeColor = Color.White;  
  38.                 }  
  39.   
  40.                 if (e.Item.IsOnDropDown && e.Item.Selected)  
  41.                 {  
  42.                     var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);  
  43.                     e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, 60, 60)), rect);  
  44.                     e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect);  
  45.                     e.Item.ForeColor = Color.White;  
  46.                 }  
  47.                 if ((e.Item as ToolStripMenuItem).DropDown.Visible && e.Item.IsOnDropDown == false)  
  48.                 {  
  49.                     var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);  
  50.                     var rect2 = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);  
  51.                     e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(20, 20, 20)), rect);  
  52.                     e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);  
  53.                     e.Item.ForeColor = Color.White;  
  54.                 }  
  55.             }  
  56.         }  
  57.         protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)  
  58.         {  
  59.             base.OnRenderSeparator(e);  
  60.             var DarkLine = new SolidBrush(Color.FromArgb(30, 30, 30));  
  61.             var rect = new Rectangle(30, 3, e.Item.Width - 30, 1);  
  62.             e.Graphics.FillRectangle(DarkLine, rect);  
  63.         }  
  64.   
  65.   
  66.         protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)  
  67.         {  
  68.             base.OnRenderItemCheck(e);  
  69.   
  70.             if (e.Item.Selected)  
  71.             {  
  72.                 var rect = new Rectangle(4, 2, 18, 18);  
  73.                 var rect2 = new Rectangle(5, 3, 16, 16);  
  74.                 SolidBrush b = new SolidBrush(Color.Black);  
  75.                 SolidBrush b2 = new SolidBrush(Color.FromArgb(220, 220, 220));  
  76.   
  77.                 e.Graphics.FillRectangle(b, rect);  
  78.                 e.Graphics.FillRectangle(b2, rect2);  
  79.                 e.Graphics.DrawImage(e.Image, new Point(5, 3));  
  80.             }  
  81.             else  
  82.             {  
  83.                 var rect = new Rectangle(4, 2, 18, 18);  
  84.                 var rect2 = new Rectangle(5, 3, 16, 16);  
  85.                 SolidBrush b = new SolidBrush(Color.White);  
  86.                 SolidBrush b2 = new SolidBrush(Color.FromArgb(255, 80, 90, 90));  
  87.   
  88.                 e.Graphics.FillRectangle(b, rect);  
  89.                 e.Graphics.FillRectangle(b2, rect2);  
  90.                 e.Graphics.DrawImage(e.Image, new Point(5, 3));  
  91.             }  
  92.         }  
  93.   
  94.         protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)  
  95.         {  
  96.             base.OnRenderImageMargin(e);  
  97.   
  98.             var rect = new Rectangle(0, 0, e.ToolStrip.Width, e.ToolStrip.Height);  
  99.             e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(20, 20, 20)), rect);  
  100.   
  101.             var DarkLine = new SolidBrush(Color.FromArgb(20, 20, 20));  
  102.             var rect3 = new Rectangle(0, 0, 26, e.AffectedBounds.Height);  
  103.             e.Graphics.FillRectangle(DarkLine, rect3);  
  104.   
  105.             e.Graphics.DrawLine(new Pen(new SolidBrush(Color.FromArgb(20, 20, 20))), 28, 0, 28, e.AffectedBounds.Height);  
  106.   
  107.             var rect2 = new Rectangle(0, 0, e.ToolStrip.Width - 1, e.ToolStrip.Height - 1);  
  108.             e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);  
  109.         }  
  110.     }  
  111. }    
Custom Panel

Class Name: PanelZ
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Windows.Forms;  
  7.   
  8. namespace PanelZ  
  9. {  
  10.    public class PanelZ : System.Windows.Forms.Panel  
  11.     {  
  12.         private Color color1 = Color.SteelBlue;  
  13.         private Color color2 = Color.DarkBlue;  
  14.         private int color1Transparent = 150;  
  15.         private int color2Transparent = 150;  
  16.         private int angle = 90;  
  17.   
  18.        //Create Properties  
  19.         public Color StartColor  
  20.         {  
  21.             get { return color1; }  
  22.             set { color1 = value; Invalidate(); }  
  23.         }  
  24.   
  25.         public Color EndColor  
  26.         {  
  27.             get { return color2; }  
  28.             set { color2 = value; Invalidate(); }  
  29.         }  
  30.   
  31.         public int Transparent1  
  32.         {  
  33.             get { return color1Transparent; }  
  34.             set  
  35.             {  
  36.                 color1Transparent = value;  
  37.                 if (color1Transparent > 255)  
  38.                 {  
  39.                     color1Transparent = 255;  
  40.                     Invalidate();  
  41.                 }  
  42.                 else  
  43.                     Invalidate();  
  44.             }  
  45.         }  
  46.   
  47.         public int Transparent2  
  48.         {  
  49.             get { return color2Transparent; }  
  50.             set  
  51.             {  
  52.                 color2Transparent = value;  
  53.                 if (color2Transparent > 255)  
  54.                 {  
  55.                     color2Transparent = 255;  
  56.                     Invalidate();  
  57.                 }  
  58.                 else  
  59.                     Invalidate();  
  60.             }  
  61.         }  
  62.   
  63.         public int GradientAngle  
  64.         {  
  65.             get { return angle; }  
  66.             set { angle = value; Invalidate(); }  
  67.         }  
  68.   
  69.         public PanelZ()  
  70.         {  
  71.         }  
  72.   
  73.         protected override void OnPaint(PaintEventArgs e)  
  74.         {  
  75.             base.OnPaint(e);  
  76.             Color c1 = Color.FromArgb(color1Transparent, color1);  
  77.             Color c2 = Color.FromArgb(color2Transparent, color2);  
  78.             Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);  
  79.             //fill rectangle with panel size  
  80.             e.Graphics.FillRectangle(b, ClientRectangle);  
  81.             b.Dispose();  
  82.         }  
  83.     }  
  84. }   
Custom CheckBox & RadioButton 
For customize CheckBox & RadioButton,we will use same code as ButtonZ code only to use ControlPaint.DrawCheckBox()ControlPaint.DrawRadioButton() methods. 
  1. ControlPaint.DrawCheckBox(e.Graphics, rc, this.Checked ? ButtonState.Checked : ButtonState.Normal);   
  1. ControlPaint.DrawRadioButton(e.Graphics, rc, this.Checked ? ButtonState.Checked : ButtonState.Normal);  


Similar Articles