Click here to Skip to main content
Click here to Skip to main content

A Form with Customized Caption Bar and Flexible Border

, 10 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on how to customize caption bar (e.g. displaying image on caption bar) and change border width and border color.
Screenshot - customizedForms.jpg

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arijit Datta
Web Developer
India India
Arijit is a young Programmer living in Kolkata
who started his career as a C# developer. He is very humorous and friendly and a great dreamer.He loves wild life, his friends and his family.
Arijit was born in a small and beautiful town
named Cooch Behar in India.He started programing
when he was in high school.
Arijit is presently working in Citytech Solutions
as a software engineer and he likes to share his thoughts and views with others.
To contact Arijit, email him at arijit.datta82@gmail.com

Comments and Discussions

 
GeneralGood Work Pinmemberdickinson.jonathan9-Sep-07 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 11 Apr 2007
Article Copyright 2007 by Arijit Datta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid