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# ?
Customizing Windows Forms in C#
Watch video for how to create custom controls in C# using following codes
https://www.youtube.com/watch?v=CFL3I3oMok4&t=48s
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. namespace Custom_Controls_in_CS
  9. {
  10. public class ButtonZ : System.Windows.Forms.Button
  11. {
  12. Color clr1, clr2;
  13. private Color color1 = Color.DodgerBlue;
  14. private Color color2 = Color.MidnightBlue;
  15. private Color m_hovercolor1 = Color.Turquoise;
  16. private Color m_hovercolor2 = Color.DarkSlateGray;
  17. private int color1Transparent = 250;
  18. private int color2Transparent = 250;
  19. private Color clickcolor1 = Color.Yellow;
  20. private Color clickcolor2 = Color.Red;
  21. private int angle = 90;
  22. private int textX = 100;
  23. private int textY = 25;
  24. private String text = "";
  25. public Color buttonborder_1 = Color.FromArgb(220, 220, 220);
  26. public Color buttonborder_2 = Color.FromArgb(150, 150, 150);
  27. public Boolean showButtonText = true;
  28. public int borderWidth = 2;
  29. public Color borderColor = Color.Transparent;
  30. public enum ButtonsShapes
  31. {
  32. Rect,
  33. RoundRect,
  34. Circle
  35. }
  36. ButtonsShapes buttonShape;
  37. public ButtonsShapes ButtonShape
  38. {
  39. get { return buttonShape; }
  40. set
  41. {
  42. buttonShape = value; Invalidate();
  43. }
  44. }
  45. public String ButtonText
  46. {
  47. get { return text; }
  48. set { text = value; Invalidate(); }
  49. }
  50. public int BorderWidth
  51. {
  52. get { return borderWidth; }
  53. set { borderWidth = value; Invalidate(); }
  54. }
  55. void SetBorderColor(Color bdrColor)
  56. {
  57. int red = bdrColor.R - 40;
  58. int green = bdrColor.G - 40;
  59. int blue = bdrColor.B - 40;
  60. if (red < 0)
  61. red = 0;
  62. if (green < 0)
  63. green = 0;
  64. if (blue < 0)
  65. blue = 0;
  66. buttonborder_1 = Color.FromArgb(red, green, blue);
  67. buttonborder_2 = bdrColor;
  68. }
  69. public Color BorderColor
  70. {
  71. get { return borderColor; }
  72. set
  73. {
  74. borderColor = value;
  75. if (borderColor == Color.Transparent)
  76. {
  77. buttonborder_1 = Color.FromArgb(220, 220, 220);
  78. buttonborder_2 = Color.FromArgb(150, 150, 150);
  79. }
  80. else
  81. {
  82. SetBorderColor(borderColor);
  83. }
  84. }
  85. }
  86. public Color StartColor
  87. {
  88. get { return color1; }
  89. set { color1 = value; Invalidate(); }
  90. }
  91. public Color EndColor
  92. {
  93. get { return color2; }
  94. set { color2 = value; Invalidate(); }
  95. }
  96. public Color MouseHoverColor1
  97. {
  98. get { return m_hovercolor1; }
  99. set { m_hovercolor1 = value; Invalidate(); }
  100. }
  101. public Color MouseHoverColor2
  102. {
  103. get { return m_hovercolor2; }
  104. set { m_hovercolor2 = value; Invalidate(); }
  105. }
  106. public Color MouseClickColor1
  107. {
  108. get { return clickcolor1; }
  109. set { clickcolor1 = value; Invalidate(); }
  110. }
  111. public Color MouseClickColor2
  112. {
  113. get { return clickcolor2; }
  114. set { clickcolor2 = value; Invalidate(); }
  115. }
  116. public int Transparent1
  117. {
  118. get { return color1Transparent; }
  119. set
  120. {
  121. color1Transparent = value;
  122. if (color1Transparent > 255)
  123. {
  124. color1Transparent = 255;
  125. Invalidate();
  126. }
  127. else
  128. Invalidate();
  129. }
  130. }
  131. public int Transparent2
  132. {
  133. get { return color2Transparent; }
  134. set
  135. {
  136. color2Transparent = value;
  137. if (color2Transparent > 255)
  138. {
  139. color2Transparent = 255;
  140. Invalidate();
  141. }
  142. else
  143. Invalidate();
  144. }
  145. }
  146. public int GradientAngle
  147. {
  148. get { return angle; }
  149. set { angle = value; Invalidate(); }
  150. }
  151. public int TextLocation_X
  152. {
  153. get { return textX; }
  154. set { textX = value; Invalidate(); }
  155. }
  156. public int TextLocation_Y
  157. {
  158. get { return textY; }
  159. set { textY = value; Invalidate(); }
  160. }
  161. public Boolean ShowButtontext
  162. {
  163. get { return showButtonText; }
  164. set { showButtonText = value; Invalidate(); }
  165. }
  166. public ButtonZ()
  167. {
  168. this.Size = new Size(100, 40);
  169. this.BackColor = Color.Transparent;
  170. this.FlatStyle = FlatStyle.Flat;
  171. this.FlatAppearance.BorderSize = 0;
  172. this.FlatAppearance.MouseOverBackColor = Color.Transparent;
  173. this.FlatAppearance.MouseDownBackColor = Color.Transparent;
  174. text = this.Text;
  175. }
  176. //method mouse enter
  177. protected override void OnMouseEnter(EventArgs e)
  178. {
  179. base.OnMouseEnter(e);
  180. clr1 = color1;
  181. clr2 = color2;
  182. color1 = m_hovercolor1;
  183. color2 = m_hovercolor2;
  184. }
  185. //method mouse leave
  186. protected override void OnMouseLeave(EventArgs e)
  187. {
  188. base.OnMouseLeave(e);
  189. color1 = clr1;
  190. color2 = clr2;
  191. SetBorderColor(borderColor);
  192. }
  193. protected override void OnMouseDown(MouseEventArgs mevent)
  194. {
  195. base.OnMouseDown(mevent);
  196. color1 = clickcolor1;
  197. color2 = clickcolor2;
  198. int red = borderColor.R - 40;
  199. int green = borderColor.G - 40;
  200. int blue = borderColor.B - 40;
  201. if (red < 0)
  202. red = 0;
  203. if (green < 0)
  204. green = 0;
  205. if (blue < 0)
  206. blue = 0;
  207. buttonborder_2 = Color.FromArgb(red, green, blue);
  208. buttonborder_1 = borderColor;
  209. this.Invalidate();
  210. }
  211. protected override void OnMouseUp(MouseEventArgs mevent)
  212. {
  213. base.OnMouseUp(mevent);
  214. OnMouseLeave(mevent);
  215. color1 = clr1;
  216. color2 = clr2;
  217. SetBorderColor(borderColor);
  218. this.Invalidate();
  219. }
  220. protected override void OnLostFocus(EventArgs e)
  221. {
  222. base.OnLostFocus(e);
  223. color1 = clr1;
  224. color2 = clr2;
  225. this.Invalidate();
  226. }
  227. protected override void OnResize(EventArgs e)
  228. {
  229. base.OnResize(e);
  230. textX = (int)((this.Width / 3) - 1);
  231. textY = (int)((this.Height / 3) + 5);
  232. }
  233. //draw circular button function
  234. void DrawCircularButton(Graphics g)
  235. {
  236. Color c1 = Color.FromArgb(color1Transparent, color1);
  237. Color c2 = Color.FromArgb(color2Transparent, color2);
  238. Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
  239. g.FillEllipse(b, 5, 5, this.Width - 10, this.Height - 10);
  240. for (int i = 0; i < borderWidth; i++)
  241. {
  242. g.DrawArc(new Pen(new SolidBrush(buttonborder_1)), 5 + i, 5, this.Width - 10, this.Height - 10, 120, 180);
  243. g.DrawArc(new Pen(new SolidBrush(buttonborder_2)), 5, 5, this.Width - (10 + i), this.Height - 10, 300, 180);
  244. }
  245. if (showButtonText)
  246. {
  247. Point p = new Point(textX, textY);
  248. SolidBrush frcolor = new SolidBrush(this.ForeColor);
  249. g.DrawString(text, this.Font, frcolor, p);
  250. }
  251. b.Dispose();
  252. }
  253. //draw rectangular button function
  254. void DrawRectangularButton(Graphics g)
  255. {
  256. Color c1 = Color.FromArgb(color1Transparent, color1);
  257. Color c2 = Color.FromArgb(color2Transparent, color2);
  258. Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
  259. g.FillRectangle(b, 0, 0, this.Width, this.Height);
  260. for (int i = 0; i < borderWidth; i++)
  261. {
  262. g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), this.Width - i, 0, this.Width - i, this.Height);
  263. g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), 0, this.Height - i, this.Width, this.Height - i);
  264. g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0 + i, 0, 0 + i, this.Height);
  265. g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0, 0 + i, this.Width, i);
  266. }
  267. if (showButtonText)
  268. {
  269. Point p = new Point(textX, textY);
  270. SolidBrush frcolor = new SolidBrush(this.ForeColor);
  271. g.DrawString(text, this.Font, frcolor, p);
  272. }
  273. b.Dispose();
  274. }
  275. //draw round rectangular button function
  276. void DrawRoundRectangularButton(Graphics g)
  277. {
  278. Color c1 = Color.FromArgb(color1Transparent, color1);
  279. Color c2 = Color.FromArgb(color2Transparent, color2);
  280. Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
  281. Region region = new System.Drawing.Region(new Rectangle(5, 5, this.Width, this.Height));
  282. GraphicsPath grp = new GraphicsPath();
  283. grp.AddArc(5, 5, 40, 40, 180, 90);
  284. grp.AddLine(25, 5, this.Width - 25, 5);
  285. grp.AddArc(this.Width - 45, 5, 40, 40, 270, 90);
  286. grp.AddLine(this.Width - 5, 25, this.Width - 5, this.Height - 25);
  287. grp.AddArc(this.Width - 45, this.Height - 45, 40, 40, 0, 90);
  288. grp.AddLine(25, this.Height - 5, this.Width - 25, this.Height - 5);
  289. grp.AddArc(5, this.Height - 45, 40, 40, 90, 90);
  290. grp.AddLine(5, 25, 5, this.Height - 25);
  291. region.Intersect(grp);
  292. g.FillRegion(b, region);
  293. for (int i = 0; i < borderWidth; i++)
  294. {
  295. g.DrawArc(new Pen(buttonborder_1), 5 + i, 5 + i, 40, 40, 180, 90);
  296. g.DrawLine(new Pen(buttonborder_1), 25, 5 + i, this.Width - 25, 5 + i);
  297. g.DrawArc(new Pen(buttonborder_1), this.Width - 45 - i, 5 + i, 40, 40, 270, 90);
  298. g.DrawLine(new Pen(buttonborder_1), 5 + i, 25, 5 + i, this.Height - 25);
  299. g.DrawLine(new Pen(buttonborder_2), this.Width - 5 - i, 25, this.Width - 5 - i, this.Height - 25);
  300. g.DrawArc(new Pen(buttonborder_2), this.Width - 45 - i, this.Height - 45 - i, 40, 40, 0, 90);
  301. g.DrawLine(new Pen(buttonborder_2), 25, this.Height - 5 - i, this.Width - 25, this.Height - 5 - i);
  302. g.DrawArc(new Pen(buttonborder_2), 5 + i, this.Height - 45 - i, 40, 40, 90, 90);
  303. }
  304. if (showButtonText)
  305. {
  306. Point p = new Point(textX, textY);
  307. SolidBrush frcolor = new SolidBrush(this.ForeColor);
  308. g.DrawString(text, this.Font, frcolor, p);
  309. }
  310. b.Dispose();
  311. }
  312. protected override void OnPaint(PaintEventArgs e)
  313. {
  314. base.OnPaint(e);
  315. switch (buttonShape)
  316. {
  317. case ButtonsShapes.Circle:
  318. this.DrawCircularButton(e.Graphics);
  319. break;
  320. case ButtonsShapes.Rect:
  321. this.DrawRectangularButton(e.Graphics);
  322. break;
  323. case ButtonsShapes.RoundRect:
  324. this.DrawRoundRectangularButton(e.Graphics);
  325. break;
  326. }
  327. }
  328. }
  329. }
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. //Create Properties
  19. public String DisplayText
  20. {
  21. get { return text; }
  22. set { text = value; Invalidate(); }
  23. }
  24. public Color StartColor
  25. {
  26. get { return color1; }
  27. set { color1 = value; Invalidate(); }
  28. }
  29. public Color EndColor
  30. {
  31. get { return color2; }
  32. set { color2 = value; Invalidate(); }
  33. }
  34. public int Transparent1
  35. {
  36. get { return color1Transparent; }
  37. set
  38. {
  39. color1Transparent = value;
  40. if (color1Transparent > 255)
  41. {
  42. color1Transparent = 255;
  43. Invalidate();
  44. }
  45. else
  46. Invalidate();
  47. }
  48. }
  49. public int Transparent2
  50. {
  51. get { return color2Transparent; }
  52. set
  53. {
  54. color2Transparent = value;
  55. if (color2Transparent > 255)
  56. {
  57. color2Transparent = 255;
  58. Invalidate();
  59. }
  60. else
  61. Invalidate();
  62. }
  63. }
  64. public int GradientAngle
  65. {
  66. get { return angle; }
  67. set { angle = value; Invalidate(); }
  68. }
  69. public LabelZ()
  70. {
  71. this.ForeColor = Color.Transparent;
  72. }
  73. protected override void OnPaint(PaintEventArgs e)
  74. {
  75. base.OnPaint(e);
  76. this.Text = text;
  77. Color c1 = Color.FromArgb(color1Transparent, color1);
  78. Color c2 = Color.FromArgb(color2Transparent, color2);
  79. //draw a string of text label
  80. Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 50, 50), c1, c2, angle);
  81. e.Graphics.DrawString(this.Text, this.Font, b, new Point(0, 0));
  82. }
  83. }
  84. }
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. //Create Properties to read values
  21. public Color ActiveTabStartColor
  22. {
  23. get { return active_color1; }
  24. set { active_color1 = value; Invalidate(); }
  25. }
  26. public Color ActiveTabEndColor
  27. {
  28. get { return active_color2; }
  29. set { active_color2 = value; Invalidate(); }
  30. }
  31. public Color NonActiveTabStartColor
  32. {
  33. get { return nonactive_color1; }
  34. set { nonactive_color1 = value; Invalidate(); }
  35. }
  36. public Color NonActiveTabEndColor
  37. {
  38. get { return nonactive_color2; }
  39. set { nonactive_color2 = value; Invalidate(); }
  40. }
  41. public int Transparent1
  42. {
  43. get { return color1Transparent; }
  44. set
  45. {
  46. color1Transparent = value;
  47. if (color1Transparent > 255)
  48. {
  49. color1Transparent = 255;
  50. Invalidate();
  51. }
  52. else
  53. Invalidate();
  54. }
  55. }
  56. public int Transparent2
  57. {
  58. get { return color2Transparent; }
  59. set
  60. {
  61. color2Transparent = value;
  62. if (color2Transparent > 255)
  63. {
  64. color2Transparent = 255;
  65. Invalidate();
  66. }
  67. else
  68. Invalidate();
  69. }
  70. }
  71. public int GradientAngle
  72. {
  73. get { return angle; }
  74. set { angle = value; Invalidate(); }
  75. }
  76. public Color TextColor
  77. {
  78. get { return forecolor; }
  79. set { forecolor = value; Invalidate(); }
  80. }
  81. public Color CloseButtonColor
  82. {
  83. get { return closebuttoncolor; }
  84. set { closebuttoncolor = value; Invalidate(); }
  85. }
  86. public TabControlZ()
  87. {
  88. this.DrawMode = TabDrawMode.OwnerDrawFixed;
  89. this.Padding = new System.Drawing.Point(22, 4);
  90. }
  91. protected override void OnPaint(PaintEventArgs pe)
  92. {
  93. base.OnPaint(pe);
  94. }
  95. //method for drawing tab items
  96. protected override void OnDrawItem(DrawItemEventArgs e)
  97. {
  98. base.OnDrawItem(e);
  99. Rectangle rc = GetTabRect(e.Index);
  100. //if tab is selected
  101. if (this.SelectedTab == this.TabPages[e.Index])
  102. {
  103. Color c1 = Color.FromArgb(color1Transparent, active_color1);
  104. Color c2 = Color.FromArgb(color2Transparent, active_color2);
  105. using (LinearGradientBrush br = new LinearGradientBrush(rc, c1, c2, angle))
  106. {
  107. e.Graphics.FillRectangle(br, rc);
  108. }
  109. }
  110. else
  111. {
  112. Color c1 = Color.FromArgb(color1Transparent, nonactive_color1);
  113. Color c2 = Color.FromArgb(color2Transparent, nonactive_color2);
  114. using (LinearGradientBrush br = new LinearGradientBrush(rc, c1, c2, angle))
  115. {
  116. e.Graphics.FillRectangle(br, rc);
  117. }
  118. }
  119. this.TabPages[e.Index].BorderStyle = BorderStyle.FixedSingle;
  120. this.TabPages[e.Index].ForeColor = SystemColors.ControlText;
  121. //draw close button on tabs
  122. Rectangle paddedBounds = e.Bounds;
  123. paddedBounds.Inflate(-5, -4);
  124. e.Graphics.DrawString(this.TabPages[e.Index].Text, this.Font, new SolidBrush(forecolor), paddedBounds);
  125. Point pad = this.Padding;
  126. //drawing close button to tab items
  127. 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);
  128. e.DrawFocusRectangle();
  129. }
  130. //action for when mouse click on close button
  131. protected override void OnMouseDown(MouseEventArgs e)
  132. {
  133. base.OnMouseDown(e);
  134. for (int i = 0; i < this.TabPages.Count; i++)
  135. {
  136. Rectangle r = this.GetTabRect(i);
  137. Rectangle closeButton = new Rectangle(r.Right + 1 - 15, r.Top + 4, 12, 12);
  138. if (closeButton.Contains(e.Location))
  139. {
  140. if (MessageBox.Show("Do you want to Close this Tab ?", "Close or Not", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  141. {
  142. this.TabPages.RemoveAt(i);
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
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. namespace Custom_Controls_in_CS
  9. {
  10. public class MenuStripZ : MenuStrip
  11. {
  12. public MenuStripZ()
  13. {
  14. this.Renderer = new MyMenuRenderer();
  15. }
  16. }
  17. public class MyMenuRenderer : ToolStripRenderer
  18. {
  19. protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
  20. {
  21. base.OnRenderMenuItemBackground(e);
  22. if (e.Item.Enabled)
  23. {
  24. if (e.Item.IsOnDropDown == false && e.Item.Selected)
  25. {
  26. var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);
  27. var rect2 = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);
  28. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, 60, 60)), rect);
  29. e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);
  30. e.Item.ForeColor = Color.White;
  31. }
  32. else
  33. {
  34. e.Item.ForeColor = Color.White;
  35. }
  36. if (e.Item.IsOnDropDown && e.Item.Selected)
  37. {
  38. var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);
  39. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, 60, 60)), rect);
  40. e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect);
  41. e.Item.ForeColor = Color.White;
  42. }
  43. if ((e.Item as ToolStripMenuItem).DropDown.Visible && e.Item.IsOnDropDown == false)
  44. {
  45. var rect = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);
  46. var rect2 = new Rectangle(0, 0, e.Item.Width - 1, e.Item.Height - 1);
  47. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(20, 20, 20)), rect);
  48. e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);
  49. e.Item.ForeColor = Color.White;
  50. }
  51. }
  52. }
  53. protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
  54. {
  55. base.OnRenderSeparator(e);
  56. var DarkLine = new SolidBrush(Color.FromArgb(30, 30, 30));
  57. var rect = new Rectangle(30, 3, e.Item.Width - 30, 1);
  58. e.Graphics.FillRectangle(DarkLine, rect);
  59. }
  60. protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
  61. {
  62. base.OnRenderItemCheck(e);
  63. if (e.Item.Selected)
  64. {
  65. var rect = new Rectangle(4, 2, 18, 18);
  66. var rect2 = new Rectangle(5, 3, 16, 16);
  67. SolidBrush b = new SolidBrush(Color.Black);
  68. SolidBrush b2 = new SolidBrush(Color.FromArgb(220, 220, 220));
  69. e.Graphics.FillRectangle(b, rect);
  70. e.Graphics.FillRectangle(b2, rect2);
  71. e.Graphics.DrawImage(e.Image, new Point(5, 3));
  72. }
  73. else
  74. {
  75. var rect = new Rectangle(4, 2, 18, 18);
  76. var rect2 = new Rectangle(5, 3, 16, 16);
  77. SolidBrush b = new SolidBrush(Color.White);
  78. SolidBrush b2 = new SolidBrush(Color.FromArgb(255, 80, 90, 90));
  79. e.Graphics.FillRectangle(b, rect);
  80. e.Graphics.FillRectangle(b2, rect2);
  81. e.Graphics.DrawImage(e.Image, new Point(5, 3));
  82. }
  83. }
  84. protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
  85. {
  86. base.OnRenderImageMargin(e);
  87. var rect = new Rectangle(0, 0, e.ToolStrip.Width, e.ToolStrip.Height);
  88. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(20, 20, 20)), rect);
  89. var DarkLine = new SolidBrush(Color.FromArgb(20, 20, 20));
  90. var rect3 = new Rectangle(0, 0, 26, e.AffectedBounds.Height);
  91. e.Graphics.FillRectangle(DarkLine, rect3);
  92. e.Graphics.DrawLine(new Pen(new SolidBrush(Color.FromArgb(20, 20, 20))), 28, 0, 28, e.AffectedBounds.Height);
  93. var rect2 = new Rectangle(0, 0, e.ToolStrip.Width - 1, e.ToolStrip.Height - 1);
  94. e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black)), rect2);
  95. }
  96. }
  97. }
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. namespace PanelZ
  8. {
  9. public class PanelZ : System.Windows.Forms.Panel
  10. {
  11. private Color color1 = Color.SteelBlue;
  12. private Color color2 = Color.DarkBlue;
  13. private int color1Transparent = 150;
  14. private int color2Transparent = 150;
  15. private int angle = 90;
  16. //Create Properties
  17. public Color StartColor
  18. {
  19. get { return color1; }
  20. set { color1 = value; Invalidate(); }
  21. }
  22. public Color EndColor
  23. {
  24. get { return color2; }
  25. set { color2 = value; Invalidate(); }
  26. }
  27. public int Transparent1
  28. {
  29. get { return color1Transparent; }
  30. set
  31. {
  32. color1Transparent = value;
  33. if (color1Transparent > 255)
  34. {
  35. color1Transparent = 255;
  36. Invalidate();
  37. }
  38. else
  39. Invalidate();
  40. }
  41. }
  42. public int Transparent2
  43. {
  44. get { return color2Transparent; }
  45. set
  46. {
  47. color2Transparent = value;
  48. if (color2Transparent > 255)
  49. {
  50. color2Transparent = 255;
  51. Invalidate();
  52. }
  53. else
  54. Invalidate();
  55. }
  56. }
  57. public int GradientAngle
  58. {
  59. get { return angle; }
  60. set { angle = value; Invalidate(); }
  61. }
  62. public PanelZ()
  63. {
  64. }
  65. protected override void OnPaint(PaintEventArgs e)
  66. {
  67. base.OnPaint(e);
  68. Color c1 = Color.FromArgb(color1Transparent, color1);
  69. Color c2 = Color.FromArgb(color2Transparent, color2);
  70. Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
  71. //fill rectangle with panel size
  72. e.Graphics.FillRectangle(b, ClientRectangle);
  73. b.Dispose();
  74. }
  75. }
  76. }
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);