Introduction

I wanted to create Custom windows forms in C#. Before working on this code, i started to find any code that could help me to create custom windows forms in C# on internet. but unfortunately i couldn't find any simple and useful also looks good form code. I found some code but they all are complicated. I also found some codes that are simple but not useful. When i started working on solving this problem, i thought to use Custom Panel/Panel as a border of a Frame for change the size of Form such as Width, Height etc and use Custom Buttons as a Control Box of a Frame.
Use Custom Panel as a Top layered Title border of a frame and use label to display text of a frame.
To display icon on a frame we will set background image to added panel.

Applying Mouse Events to panels and change properties of Form.

And it works perfectly.

You require Visual Studio Professional 2013 or higher version and .NET framework 4.5 or higher.




Perform following all Steps correctly to create custom windows forms.

We can also create extended advanced forms using only panels.(see above images,image 1 & 2)

Download the source code to view code for those forms.

See above image for better understanding of resizing our customized form and BlackForm.cs in file.

In this article we will create Blue colored resizable custom windows form, change colors of panels and buttons as you wish.
You can use Custom Controls to design a form.
Dont have Custom Controls ?

Start

Step 1 : Start Visual Studio and Create new Windows Forms Application project in C#.
I created CustomWindowsForm project.
Download the source code and view the code for BlueForm.cs.
Now set following Properties to created Form (Form1).
ControlBox false
BackColor 30,60,150
FormBorderStyle None
Size 684,461
To Add icon you can add again a panel and set background image to it used as a window icon.
Step 2 : Now, Go to Project -> Add Class
Enter Name ButtonZ and click Add.
Now Copy and Paste following ButtonZ code into your created class ButtonZ code.
this button code is for create our Close & Minimize buttons of the form.
  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 CustomWindowsForm
  8. {
  9. public class ButtonZ : System.Windows.Forms.Button
  10. {
  11. Color clr1;
  12. private Color color = Color.Teal;
  13. private Color m_hovercolor = Color.FromArgb(0, 0, 140);
  14. private Color clickcolor = Color.FromArgb(160, 180, 200);
  15. private int textX = 6;
  16. private int textY = -20;
  17. private String text = "_";
  18. public String DisplayText
  19. {
  20. get { return text; }
  21. set { text = value; Invalidate(); }
  22. }
  23. public Color BZBackColor
  24. {
  25. get { return color; }
  26. set { color = value; Invalidate(); }
  27. }
  28. public Color MouseHoverColor
  29. {
  30. get { return m_hovercolor; }
  31. set { m_hovercolor = value; Invalidate(); }
  32. }
  33. public Color MouseClickColor1
  34. {
  35. get { return clickcolor; }
  36. set { clickcolor = value; Invalidate(); }
  37. }
  38. public int TextLocation_X
  39. {
  40. get { return textX; }
  41. set { textX = value; Invalidate(); }
  42. }
  43. public int TextLocation_Y
  44. {
  45. get { return textY; }
  46. set { textY = value; Invalidate(); }
  47. }
  48. public ButtonZ()
  49. {
  50. this.Size = new System.Drawing.Size(31, 24);
  51. this.ForeColor = Color.White;
  52. this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  53. this.Font = new System.Drawing.Font("Microsoft YaHei UI", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  54. this.Text = "_";
  55. text = this.Text;
  56. }
  57. //method mouse enter
  58. protected override void OnMouseEnter(EventArgs e)
  59. {
  60. base.OnMouseEnter(e);
  61. clr1 = color;
  62. color = m_hovercolor;
  63. }
  64. //method mouse leave
  65. protected override void OnMouseLeave(EventArgs e)
  66. {
  67. base.OnMouseLeave(e);
  68. color = clr1;
  69. }
  70. protected override void OnMouseDown(MouseEventArgs mevent)
  71. {
  72. base.OnMouseDown(mevent);
  73. color = clickcolor;
  74. }
  75. protected override void OnMouseUp(MouseEventArgs mevent)
  76. {
  77. base.OnMouseUp(mevent);
  78. color = clr1;
  79. }
  80. protected override void OnPaint(PaintEventArgs pe)
  81. {
  82. base.OnPaint(pe);
  83. text = this.Text;
  84. if (textX == 100 && textY == 25)
  85. {
  86. textX = ((this.Width) / 3) + 10;
  87. textY = (this.Height / 2) - 1;
  88. }
  89. Point p = new Point(textX, textY);
  90. pe.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
  91. pe.Graphics.DrawString(text, this.Font, new SolidBrush(this.ForeColor), p);
  92. }
  93. }
  94. }
Step 3 : Now, Go to Project -> Add Class
Enter Name MinMaxButton and click Add.
Now Copy and Paste following MinMaxButton code into your created class MinMaxButton code.
this code is for to create our maximize & restore down button when form is maximized.
  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 CustomWindowsForm
  8. {
  9. public class MinMaxButton : System.Windows.Forms.Button
  10. {
  11. Color clr1;
  12. private Color color = Color.Gray;
  13. private Color m_hovercolor = Color.FromArgb(180, 200, 240);
  14. private Color clickcolor = Color.FromArgb(160, 180, 200);
  15. private int textX = 6;
  16. private int textY = -20;
  17. private String text = "_";
  18. public enum CustomFormState
  19. {
  20. Normal,
  21. Maximize
  22. }
  23. CustomFormState _customFormState;
  24. public CustomFormState CFormState
  25. {
  26. get { return _customFormState; }
  27. set { _customFormState = value; Invalidate(); }
  28. }
  29. public String DisplayText
  30. {
  31. get { return text; }
  32. set { text = value; Invalidate(); }
  33. }
  34. public Color BZBackColor
  35. {
  36. get { return color; }
  37. set { color = value; Invalidate(); }
  38. }
  39. public Color MouseHoverColor
  40. {
  41. get { return m_hovercolor; }
  42. set { m_hovercolor = value; Invalidate(); }
  43. }
  44. public Color MouseClickColor1
  45. {
  46. get { return clickcolor; }
  47. set { clickcolor = value; Invalidate(); }
  48. }
  49. public int TextLocation_X
  50. {
  51. get { return textX; }
  52. set { textX = value; Invalidate(); }
  53. }
  54. public int TextLocation_Y
  55. {
  56. get { return textY; }
  57. set { textY = value; Invalidate(); }
  58. }
  59. public MinMaxButton()
  60. {
  61. this.Size = new System.Drawing.Size(31, 24);
  62. this.ForeColor = Color.White;
  63. this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  64. this.Text = "_";
  65. text = this.Text;
  66. }
  67. //method mouse enter
  68. protected override void OnMouseEnter(EventArgs e)
  69. {
  70. base.OnMouseEnter(e);
  71. clr1 = color;
  72. color = m_hovercolor;
  73. }
  74. //method mouse leave
  75. protected override void OnMouseLeave(EventArgs e)
  76. {
  77. base.OnMouseLeave(e);
  78. color = clr1;
  79. }
  80. protected override void OnMouseDown(MouseEventArgs mevent)
  81. {
  82. base.OnMouseDown(mevent);
  83. color = clickcolor;
  84. }
  85. protected override void OnMouseUp(MouseEventArgs mevent)
  86. {
  87. base.OnMouseUp(mevent);
  88. color = clr1;
  89. }
  90. protected override void OnPaint(PaintEventArgs pe)
  91. {
  92. base.OnPaint(pe);
  93. switch (_customFormState)
  94. {
  95. case CustomFormState.Normal:
  96. pe.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
  97. //draw and fill thw rectangles of maximized window
  98. for (int i = 0; i < 2; i++)
  99. {
  100. pe.Graphics.DrawRectangle(new Pen(this.ForeColor), textX + i + 1, textY, 10, 10);
  101. pe.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX + 1, textY - 1, 12, 4);
  102. }
  103. break;
  104. case CustomFormState.Maximize:
  105. pe.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
  106. //draw and fill thw rectangles of maximized window
  107. for (int i = 0; i < 2; i++)
  108. {
  109. pe.Graphics.DrawRectangle(new Pen(this.ForeColor), textX + 5, textY, 8, 8);
  110. pe.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX + 5, textY - 1, 9, 4);
  111. pe.Graphics.DrawRectangle(new Pen(this.ForeColor), textX + 2, textY + 5, 8, 8);
  112. pe.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX + 2, textY + 4, 9, 4);
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }

Step 4 : Run your Form and exit it.For to create library of above code for darg & drop components.

Step 5 : Drag & Drop Panel onto your Form (TopBorderPanel)
Set following Properties to dropped panel.

Name TopBorderPanel
BackColor 10,20,50
Cursor SizeNS
Dock Top
Size 684,2
Step 6 : Drag & Drop Panel onto your Form (TopPanel)
Set following Properties to dropped panel.
Name TopPanel
BackColor 20,40,80
Cursor Default
Dock Top
Size 680,35

Step 7 : Drag & Drop Panel onto your Form (LeftPanel)
Set following Properties to dropped panel.

Name LeftPanel
BackColor 10,20,50
Cursor SizeWE
Dock Left
Size 2,459

Step 8 : Drag & Drop Panel onto your Form (RightPanel)
Set following Properties to dropped panel.
Name RightPanel
BackColor 10,20,50
Cursor SizeWE
Dock Right
Size 2,459

Step 9 : Drag & Drop Panel onto your Form (BottomPanel)
Set following Properties to dropped panel.

Name BottomPanel
BackColor 10,20,50
Cursor SizeNS
Dock Bottom
Size 680,2

Step 10 : Drag & Drop ToolTip onto your form

Step 11 : Drag & Drop ButtonZ onto TopPanel (_CloseButton)
Set following Properties to dropped button.

Name _CloseButton
Anchor Top,Right
BZBackColor 20,40,80
DisplayText X
Font Microsoft YaHei UI, 11.25pt, style=Bold
ForeColor White
Location 639,3
MouseClickColor1 150,0,0
MouseHoverColor 40,80,180
Size 35,35
Text X
TextLocation_X 10
TextLocation_Y 4
ToolTip on toolTip1 Close
Adjust button location as you wish.
Step 12 : Drag & Drop ButtonZ onto TopPanel (_MinButton)
Set following Properties to dropped button.
Name _MinButton
Anchor Top,Right
BZBackColor 20,40,80
DisplayText _
Font Microsoft YaHei UI, 18pt, style=Bold
ForeColor White
Location 569,3
MouseClickColor1 10,20,60
MouseHoverColor 40,80,180
Size 35,35
Text _
TextLocation_X 8
TextLocation_Y -14
ToolTip on toolTip1 Minimize
Step 13 : Drag & Drop MinMaxButton onto TopPanel (_MaxButton)
Set following Properties to dropped button.
Name _MaxButton
Anchor Top,Right
BZBackColor 20,40,80
CFormState Normal
ForeColor White
Location 604,3
MouseClickColor1 10,20,60
MouseHoverColor 40,80,180
Size 35,25
TextLocation_X 11
TextLocation_Y 8
ToolTip on toolTip1 Maximize
For text of our custom for, you can add Label and use it as a text of a form.
Step 14 : Add following variables to code of Form1 globally
  1. bool isTopPanelDragged = false;
  2. bool isLeftPanelDragged = false;
  3. bool isRightPanelDragged = false;
  4. bool isBottomPanelDragged = false;
  5. bool isTopBorderPanelDragged = false;
  6. bool isWindowMaximized = false;
  7. Point offset;
  8. Size _normalWindowSize;
  9. Point _normalWindowLocation = Point.Empty;

isTopPanelDragged is to check that mouse down event triggered by top panel or not.
Same for Left,Right,Bottom,TopBorder panels.
isWindowMaximized is to check whether _MaxButton click event occured or not.
offset is temporary variable to store location of our form.
_normalWindowSize is to hold normal window size after clicking on _MaxButton for go to normal window size
Same as for _normalWindowLocation only just to store form location.


Step 15 : Now add Events to Panels and Buttons in Events Box
MouseDown,MouseMove & MouseUp events to Panels.

TopBorderPanel :

  • TopBorderPanel_MouseDown
  • TopBorderPanel_MouseMove
  • TopBorderPanel_MouseUp

TopPanel :

  • TopPanel_MouseDown
  • TopPanel_MouseMove
  • TopPanel_MouseUp

LeftPanel :
  • LeftPanel_MouseDown
  • LeftPanel_MouseMove
  • LeftPanel_MouseUp

RightPanel :
  • RightPanel_MouseDown
  • RightPanel_MouseMove
  • RightPanel_MouseUp

BottomPanel :
  • BottomPanel_MouseDown
  • BottomPanel_MouseMove
  • BottomPanel_MouseUp

_MinButton :
  • _MinButton_Click

_MaxButton :
  • _MaxButton_Click

_CloseButton :
  • _CloseButton_Click


Step 16 : Once you added all above events to all components then replace your Form1.cs code with following code. Just make changes in classes, namespaces etc. Download the source code for view simple blue colored custom form.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace CustomWindowsForm
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. bool isTopPanelDragged = false;
  19. bool isLeftPanelDragged = false;
  20. bool isRightPanelDragged = false;
  21. bool isBottomPanelDragged = false;
  22. bool isTopBorderPanelDragged = false;
  23. bool isWindowMaximized = false;
  24. Point offset;
  25. Size _normalWindowSize;
  26. Point _normalWindowLocation = Point.Empty;
  27. private void TopBorderPanel_MouseDown(object sender, MouseEventArgs e)
  28. {
  29. if (e.Button == MouseButtons.Left)
  30. {
  31. isTopBorderPanelDragged = true;
  32. }
  33. else
  34. {
  35. isTopBorderPanelDragged = false;
  36. }
  37. }
  38. private void TopBorderPanel_MouseMove(object sender, MouseEventArgs e)
  39. {
  40. if (e.Y < this.Location.Y)
  41. {
  42. if (isTopBorderPanelDragged)
  43. {
  44. if (this.Height < 50)
  45. {
  46. this.Height = 50;
  47. isTopBorderPanelDragged = false;
  48. }
  49. else
  50. {
  51. this.Location = new Point(this.Location.X, this.Location.Y + e.Y);
  52. this.Height = this.Height - e.Y;
  53. }
  54. }
  55. }
  56. }
  57. private void TopBorderPanel_MouseUp(object sender, MouseEventArgs e)
  58. {
  59. isTopBorderPanelDragged = false;
  60. }
  61. private void TopPanel_MouseDown(object sender, MouseEventArgs e)
  62. {
  63. if (e.Button == MouseButtons.Left)
  64. {
  65. isTopPanelDragged = true;
  66. Point pointStartPosition = this.PointToScreen(new Point(e.X, e.Y));
  67. offset = new Point();
  68. offset.X = this.Location.X - pointStartPosition.X;
  69. offset.Y = this.Location.Y - pointStartPosition.Y;
  70. }
  71. else
  72. {
  73. isTopPanelDragged = false;
  74. }
  75. if (e.Clicks == 2)
  76. {
  77. isTopPanelDragged = false;
  78. _MaxButton_Click(sender, e);
  79. }
  80. }
  81. private void TopPanel_MouseMove(object sender, MouseEventArgs e)
  82. {
  83. if (isTopPanelDragged)
  84. {
  85. Point newPoint = TopPanel.PointToScreen(new Point(e.X, e.Y));
  86. newPoint.Offset(offset);
  87. this.Location = newPoint;
  88. if (this.Location.X > 2 || this.Location.Y > 2)
  89. {
  90. if (this.WindowState == FormWindowState.Maximized)
  91. {
  92. this.Location = _normalWindowLocation;
  93. this.Size = _normalWindowSize;
  94. toolTip1.SetToolTip(_MaxButton, "Maximize");
  95. _MaxButton.CFormState = MinMaxButton.CustomFormState.Normal;
  96. isWindowMaximized = false;
  97. }
  98. }
  99. }
  100. }
  101. private void TopPanel_MouseUp(object sender, MouseEventArgs e)
  102. {
  103. isTopPanelDragged = false;
  104. if (this.Location.Y <= 5)
  105. {
  106. if (!isWindowMaximized)
  107. {
  108. _normalWindowSize = this.Size;
  109. _normalWindowLocation = this.Location;
  110. Rectangle rect = Screen.PrimaryScreen.WorkingArea;
  111. this.Location = new Point(0, 0);
  112. this.Size = new System.Drawing.Size(rect.Width, rect.Height);
  113. toolTip1.SetToolTip(_MaxButton, "Restore Down");
  114. _MaxButton.CFormState = MinMaxButton.CustomFormState.Maximize;
  115. isWindowMaximized = true;
  116. }
  117. }
  118. }
  119. private void LeftPanel_MouseDown(object sender, MouseEventArgs e)
  120. {
  121. if (this.Location.X <= 0 || e.X < 0)
  122. {
  123. isLeftPanelDragged = false;
  124. this.Location = new Point(10, this.Location.Y);
  125. }
  126. else
  127. {
  128. if (e.Button == MouseButtons.Left)
  129. {
  130. isLeftPanelDragged = true;
  131. }
  132. else
  133. {
  134. isLeftPanelDragged = false;
  135. }
  136. }
  137. }
  138. private void LeftPanel_MouseMove(object sender, MouseEventArgs e)
  139. {
  140. if (e.X < this.Location.X)
  141. {
  142. if (isLeftPanelDragged)
  143. {
  144. if (this.Width < 100)
  145. {
  146. this.Width = 100;
  147. isLeftPanelDragged = false;
  148. }
  149. else
  150. {
  151. this.Location = new Point(this.Location.X + e.X, this.Location.Y);
  152. this.Width = this.Width - e.X;
  153. }
  154. }
  155. }
  156. }
  157. private void LeftPanel_MouseUp(object sender, MouseEventArgs e)
  158. {
  159. isLeftPanelDragged = false;
  160. }
  161. private void RightPanel_MouseDown(object sender, MouseEventArgs e)
  162. {
  163. if (e.Button == MouseButtons.Left)
  164. {
  165. isRightPanelDragged = true;
  166. }
  167. else
  168. {
  169. isRightPanelDragged = false;
  170. }
  171. }
  172. private void RightPanel_MouseMove(object sender, MouseEventArgs e)
  173. {
  174. if (isRightPanelDragged)
  175. {
  176. if (this.Width < 100)
  177. {
  178. this.Width = 100;
  179. isRightPanelDragged = false;
  180. }
  181. else
  182. {
  183. this.Width = this.Width + e.X;
  184. }
  185. }
  186. }
  187. private void RightPanel_MouseUp(object sender, MouseEventArgs e)
  188. {
  189. isRightPanelDragged = false;
  190. }
  191. private void BottomPanel_MouseDown(object sender, MouseEventArgs e)
  192. {
  193. if (e.Button == MouseButtons.Left)
  194. {
  195. isBottomPanelDragged = true;
  196. }
  197. else
  198. {
  199. isBottomPanelDragged = false;
  200. }
  201. }
  202. private void BottomPanel_MouseMove(object sender, MouseEventArgs e)
  203. {
  204. if (isBottomPanelDragged)
  205. {
  206. if (this.Height < 50)
  207. {
  208. this.Height = 50;
  209. isBottomPanelDragged = false;
  210. }
  211. else
  212. {
  213. this.Height = this.Height + e.Y;
  214. }
  215. }
  216. }
  217. private void BottomPanel_MouseUp(object sender, MouseEventArgs e)
  218. {
  219. isBottomPanelDragged = false;
  220. }
  221. private void _CloseButton_Click(object sender, EventArgs e)
  222. {
  223. this.Close();
  224. }
  225. private void _MaxButton_Click(object sender, EventArgs e)
  226. {
  227. if (isWindowMaximized)
  228. {
  229. this.Location = _normalWindowLocation;
  230. this.Size = _normalWindowSize;
  231. toolTip1.SetToolTip(_MaxButton, "Maximize");
  232. _MaxButton.CFormState = MinMaxButton.CustomFormState.Normal;
  233. isWindowMaximized = false;
  234. }
  235. else
  236. {
  237. _normalWindowSize = this.Size;
  238. _normalWindowLocation = this.Location;
  239. Rectangle rect = Screen.PrimaryScreen.WorkingArea;
  240. this.Location = new Point(0, 0);
  241. this.Size = new System.Drawing.Size(rect.Width, rect.Height);
  242. toolTip1.SetToolTip(_MaxButton, "Restore Down");
  243. _MaxButton.CFormState = MinMaxButton.CustomFormState.Maximize;
  244. isWindowMaximized = true;
  245. }
  246. }
  247. private void _MinButton_Click(object sender, EventArgs e)
  248. {
  249. this.WindowState = FormWindowState.Minimized;
  250. }
  251. }
  252. }
Here is the output of above all procedures
Well we have created a custom forms,so you directly cannot add some controls to a form like MenuStrip, SplitContainer etc. To add these controls first you need to add panels and add them onto that panel so that our customize form will not be changed.