Click here to Skip to main content
15,881,757 members
Articles / Productivity Apps and Services / Microsoft Office

Office 2010 Button

Rate me:
Please Sign up or sign in to vote.
3.60/5 (5 votes)
29 May 2011CPOL1 min read 34.7K   2.7K   23   4
This article describes how to extend a system button to achieve the Office 2010 button look and feel.

Office2010ButtonPreview.png

Introduction

This article demonstrates extending the system button control to achieve an Office 2010 button look and feel.

Basics

The first step in creating a user interface is to choose the right base component that offers complete functionality and extendibility. The ButtonBase class would be the right choice for implementing a button control; this sample uses Button as the base class for simplicity.

Key points:

  • inherit from the base.
  • override the essential methods and properties for custom implementations.

This example inherits from the Button class and overrides the Paint method to make the Office 2010 button appearance.

Using the code

The cardinal part of the OfficeButton class is the overridden Paint method, which completely ignores the paint implementation of the base and provides its own implementation.

C#
protected override void OnPaint(PaintEventArgs pevent)
{
    PaintButtonBackground(pevent);
    PaintImageAndText(pevent);
    PaintButtonBorder(pevent);
}

Image and text are available in all the three states of the button and are painted in the same way all the time with PaintEventArgs, whereas background painting is different for all the three states. The GetBackgroundGradientBrush method gets the appropriate brush and fills the background.

C#
private void PaintButtonBackground(PaintEventArgs e)
{
    ButtonState state = GetButtonState();

    using (LinearGradientBrush brush = GetBackgroundGradientBrush(state))
    {
        e.Graphics.FillRectangle(brush, this.ClientRectangle);
    }
}

GetBackgroundGradientBrush returns the appropriate blended brush based on the button state.

C#
private LinearGradientBrush GetBackgroundGradientBrush(ButtonState buttonState)
{
    Color gradientBegin = Color.Empty;
    Color gradientEnd = Color.Empty;

    switch (buttonState)
    {
        ...
        ...
        ...
                
    case ButtonState.Selected:
        LinearGradientBrush brush = new LinearGradientBrush(
          this.ClientRectangle, gradientEnd, gradientBegin,
        LinearGradientMode.Vertical);
        this.SelectionBlend.Colors = this.InterpolationColors;
        brush.InterpolationColors = this.SelectionBlend;
        return brush;
    }
    
    return new LinearGradientBrush(this.ClientRectangle, gradientEnd,
        gradientBegin, LinearGradientMode.Vertical);
}

Hope this gives you a better idea of extending system controls to meet your requirements.

License

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


Written By
Technical Lead
India India
I code, learn, read and listen.

Comments and Discussions

 
GeneralMy vote of 3 Pin
R. Hoffmann31-May-11 7:28
professionalR. Hoffmann31-May-11 7:28 
I also think this article would be better with more detail.
GeneralRe: My vote of 3 Pin
VallarasuS1-Jun-11 7:17
VallarasuS1-Jun-11 7:17 
GeneralMy vote of 2 Pin
Mario Majčica30-May-11 1:58
professionalMario Majčica30-May-11 1:58 
GeneralRe: My vote of 2 Pin
VallarasuS1-Jun-11 7:19
VallarasuS1-Jun-11 7:19 

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.