Introduction
First of all, I want to say that I am a novice in C# and this is my first article. So, I would like to apologize to the experts if they do not find anything useful in this article. But I think there may be someone out there who needs this article.
We know that it is not a very easy task to customize the caption bar and the border of a form. You can see that Microsoft-Office 2007 or Yahoo Messenger for Windows Vista displays images on its caption bar. This article chronicles the way to customize the caption bar and set the border width and border color of the form as per your needs.
How I Did It
I set the border style of the form to none and used some picture boxes to draw the borders. I made a user control with three buttons to minimize, maximize and close the form. I used a panel on the top and a picture box on the top-left corner of the form to create the caption bar.
Some Properties and Methods
Here is a list of some properties and methods that you can use in your code:
int BorderWidth
: Gets or sets the width of the form border
Color BorderColor
: Sets the border color of the form
bool MinimizeButton
: Shows the minimize button if true
else, hides the minimize button
bool MaximizeButton
: Shows the maximize button if true
else, hides the maximize button
bool CloseButton
: Shows the close button if true
else, hides the close button
bool MinMaxCloseButtons
: Shows all the three buttons if true
else, hides all the three buttons
int MinMaxCloseHeight
: Gets or sets the height of the min, max and close buttons
void HideBorders()
: Hides form border
void ShowBorders()
: Shows form border
Using the Code
Using this code is very simple. Add CustomizedForms.cs and Min_Max_Close.cs along with their designer and resource files to your project. Inherit your form from CustomizedForms
instead of System.Windows.Forms.Form
.
Your class declaration would be:
public partial class CustomizeFormExample : CustomizedForm
You can use panels, picture boxes or any other control to design your caption bar. To make your form pan-able, you just add the following codes in the mouse down, mouse move and mouse up events of the controls that you are using for your caption bar.
bool Active = false;
int X = 0,Y=0;
private void CaptionBar_MouseDown(object sender,MouseEventArgs e)
{
Active = true;
X = e.X;
Y = e.Y;
}
private void CaptionBar_MouseMove(object sender, MouseEventArgs e)
{
if (Active)
{
this.Location = new Point(this.Left + e.X-X, this.Top + e.Y-Y);
Refresh();
}
}
private void CaptionBar_MouseUp(object sender, MouseEventArgs e)
{
Active = false;
}
History
- 11th April, 2007: Initial post