Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / Windows Forms

Easy Customize Title Bar

Rate me:
Please Sign up or sign in to vote.
4.92/5 (11 votes)
11 Sep 2009CPOL4 min read 115.4K   15.4K   62   8
Create your own customized title bar instead of using the traditional Windows title bar

dialog.jpg

Introduction

Hi friends. I would like to give you another sweet title bar which will make your applications beautiful. I have spent a lot of time on the web to find such a title bar, but I couldn't. But while searching, I have picked some key points to make my own title bar. I am sorry I could not remember where I picked some of those points from. But I should pay my great respect for them. If I find them again, I will put those references in my article.

With my findings and using my knowledge, I will present to you a customizable title bar control which will save you time. Another thing is, this is not fully customizable yet. This is to make you understand how easy it is to create such a control in C# .NET. In this creation, I have used my previous control of control box to create this title bar. I will explain how to create a control box for a form. Then I will move on with the title bar which easier to develop.

Using the Code

Though you have the code with this article, you can change the appearance as you want. This control provides the same functionality as the Windows control box does.

Create a C# “Windows Control Library” project and you can do all these wonders. Put three label controls on to the control form and assign images for those. To highlight the button when the cursor moves over, we have to capture the mouse move event of each label control and add the following code segment...

C#
private void lblMinimize_MouseMove(object sender, MouseEventArgs e) 
{  
     lblMinimize.Image = global::window_control_box.Properties.Resources.minimize_sele;}

... where you have to set another image to the control which reflects mouse over effect in label control. For these things, you can use any image editing/creation software.

I am not going into details because this is quite simple. So you can add these code segments for all three labels.

Next is how to create those actions of minimizing, maximizing and close. For this, you have to add the following code segment on the click event of the label control:

C#
Private void lblClose_Click(object sender, EventArgs e)
{
	this.ParentForm.Close();   
}
C#
private void lblMaximize_Click(object sender, EventArgs e) 
{
        if (this.ParentForm.WindowState == FormWindowState.Maximized)
        {
	       this.ParentForm.WindowState = FormWindowState.Normal;
        }
        else if(this.ParentForm.WindowState == FormWindowState.Normal)
        {
               this.ParentForm.WindowState = FormWindowState.Maximized;
        }
        this.ParentForm.Show();
}

Here, what we are doing is rather than calling the close method of the control form, we are calling the parent form of where the control is. This will do the stuff for us.

I have made some small changes in the maximize click event to behave the same as how the Windows maximize button behaves.

To enable those Maximize, Minimize and Close buttons as properties for the user, we only need to add the following code segments:

C#
[Category("Appearance")] 
[Description("Gets or sets maximize button visibility")]
public bool Maximize
{
    set
    {
        lblMaximize.Visible = value;  
    }
    get
    {
        return lblMaximize.Visible; 
    } 
}

[Category("Appearance")]
[Description("Gets or sets minimize button visibility")]
public bool Minimize
{ 
    set
    {
        lblMinimize.Visible = value;
    }
    get
    {
        return lblMinimize.Visible;
    } 
}  

[Category("Appearance")]
[Description("Gets or sets close button visibility")]
public bool Close
{
    set
    {
        lblClose.Visible = value;
    }
    get
    {
        return lblClose.Visible;
    } 
} 

This is all you have to perform in order to create a “control box” control. Once you compile, you will get a DLL. You can simply use that DLL in your next control creation project as a visual control. What you have to do is right click on the tool box pane in C# and click on “choose items…”. From there, you browse this DLL and click OK. Then drag and drop this control on to your project Windows form.

Here I have attached both projects which create a title bar and control box. With the help of both, I am quite sure you can create your own title bar.

How to Use the Title Bar in your Application

Copy the two DLL files to your application source folder which was produced as the compilation of TitleBarControl project. As I explained above, add the control to your toolbox using "choose items...".

Drag and drop the title bar control to your application and set the Doc property of the control to "top". In your property dialog box, you can see Title, Title Font, Title Forecolor, TitleBackColor properties for the title bar control. You can set those properties and can customize as you want.

There is a limitation of displaying the text. Displaying area of the title bar is fixed. I will provide an enhanced version in the near future. Because you have the code of the title bar control, you can easily overcome this situation by making some modifications.

Once you set the Doc property to "Top", you have to remove the traditional windows title using the FormBorderStyle Property. You can choose form border style none or you can do some tricks to remove the title bar like setting:

  • ControlBox = false
  • FormBorderStyle = FixedSingle
  • Text = "" (Should be blank)

Once you set the form properties as shown above, you will get the form with your own title bar.

If you want to change the appearance of the control, You have to modify images used in FormTitleBar and Control Box controls.

Few more tricks.

How to Apply to an MDI Form

If there is a menu in the MDI form, drag and drop “FormTitileBar” control on to the menu. Then it will get the position just below the main menu. To move it up, right click on the “FormTitleBar” control and click on “Send to Back” option. This will do the job.

In all the forms you apply this title bar, you have to take out windows title bar by adjusting properties of the form.

History

  • 11th September, 2009: Initial post

License

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


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
I am working as a Tech Lead. I love VC++.
I am trying to get into new technologies coming with VC++ and also in other areas too.

Currently I am working in C# .Net as well...

Now I have sound knowledge in C# as well as in VC++.

Comments and Discussions

 
QuestionAdd additional Button? Pin
JeffOstrosser31-Jan-18 4:51
JeffOstrosser31-Jan-18 4:51 
QuestionExcellent code, some design ideas Pin
szandras252423-Feb-13 10:58
szandras252423-Feb-13 10:58 
QuestionNice job Pin
PritCoder2-Nov-11 1:52
PritCoder2-Nov-11 1:52 
Generalawesome Pin
garkin12312-Sep-10 21:51
garkin12312-Sep-10 21:51 
Questionsound knoledge???? Pin
mojimoj20-Oct-09 2:03
mojimoj20-Oct-09 2:03 
AnswerRe: sound knoledge???? Pin
venura c.p.w. goonatillake20-Oct-09 2:49
venura c.p.w. goonatillake20-Oct-09 2:49 
GeneralFor Compatibility Pin
The_Mega_ZZTer11-Sep-09 4:56
The_Mega_ZZTer11-Sep-09 4:56 
GeneralRe: For Compatibility Pin
venura c.p.w. goonatillake11-Sep-09 5:23
venura c.p.w. goonatillake11-Sep-09 5:23 

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

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