Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easy Customize Title Bar

0.00/5 (No votes)
11 Sep 2009 1  
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...

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:

Private void lblClose_Click(object sender, EventArgs e)
{
	this.ParentForm.Close();   
}
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:

[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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here