-
Notifications
You must be signed in to change notification settings - Fork 19
Changing Ribbon Colors
Introduction to the feature The feature I want to talk about today is how to change the ribbon general colors. Note that you can’t change the colors of a specific ribbon item, only the entire ribbon.
There are 3 colors we can change:
-
Background Color
-
Highlight Color
-
Text Color
Here is an example of a colored ribbon:
How to do it? I’ve added a new method to the RibbonLib.Ribbon class in my Windows Ribbon for WinForms library. Following is an example of how to use it:
private void Form1_Load(object sender, EventArgs e)
{
// set ribbon colors
_ribbon.SetColors(Color.Wheat, Color.IndianRed, Color.BlueViolet);
}
Behind the scenes What the SetColors method actually does is:
-
Get IPropertyStore interface from the IUIFramework (which represents the ribbon framework)
-
Create 3 PropVariant variables that will hold the 3 colors we want to set
-
Convert the colors: RGB –> HSL –> HSB –> uint, see next section.
-
Set the relevant properties with the converted colors values
public void SetColors(Color background, Color highlight, Color text)
{
if (_framework == null)
{
return;
}
IPropertyStore propertyStore = (IPropertyStore)_framework;
PropVariant backgroundColorProp = new PropVariant();
PropVariant highlightColorProp = new PropVariant();
PropVariant textColorProp = new PropVariant();
uint backgroundColor = ColorHelper.RGBToUInt32(background);
uint highlightColor = ColorHelper.RGBToUInt32(highlight);
uint textColor = ColorHelper.RGBToUInt32(text);
backgroundColorProp.SetUInt(backgroundColor);
highlightColorProp.SetUInt(highlightColor);
textColorProp.SetUInt(textColor);
propertyStore.SetValue(ref RibbonProperties.UI_PKEY_GlobalBackgroundColor,
ref backgroundColorProp);
propertyStore.SetValue(ref RibbonProperties.UI_PKEY_GlobalHighlightColor,
ref highlightColorProp);
propertyStore.SetValue(ref RibbonProperties.UI_PKEY_GlobalTextColor,
ref textColorProp);
propertyStore.Commit();
}
Color Format I didn’t dive into the colors format world, so I’ll just give a quick reference:
RGB is the well-known color format that us, developers, like and understand.
RGB can be converted to HSL. This should be a common transformation. or as Microsoft wrote here, “easily accomplished with most photo editing software”. In this page Microsoft also gives the formulas for converting HSL to HSB.
You can find code for transforming RGB to HSL and vice versa at W3C or Wikipedia. The transformations are not lossless.
Finally, HSB is converted to uint by just OR-ing the values, much like RGB to uint conversion. I’ve encapsulated all these details in a helper class named RibbonLib.ColorHelper.
A new sample, named 07-RibbonColor, summarize the details of this post. Find it on the project site.
-
Basics
- Introduction, Background on the windows ribbon
- Basic Ribbon Wrapper Basic .NET wrappers for windows ribbon.
- Quickstart Tutorial
- First WinForms Ribbon Application How to create an empty WinForms application with ribbon support.
-
Working with Ribbon Controls
- Application Menu with Buttons How to use the ribbon application menu.
- Application Menu with SplitButton and DropDownButton How to use the ribbon application menu with ribbon split button and ribbon dropdown button controls.
- Tabs, Groups and HelpButton How to use ribbon tabs, groups and the ribbon help button control.
- Spinner How to use the ribbon spinner control.
- ComboBox How to use the ribbon combo box control.
- DropDownGallery, SplitButtonGallery and InRibbonGallery How to use the ribbon drop down gallery, split button gallery and in ribbon gallery controls.
- CheckBox and ToggleButton How to use the ribbon check box and toggle button controls.
- DropDownColorPicker How to use the ribbon drop down color picker control.
- FontControl How to use the ribbon font control.
- ContextualTabs How to work with ribbon contextual tabs.
- ContextPopup How to work with ribbon context popup.
- RecentItems How to work with ribbon recent items control.
- QuickAccessToolbar How to work with the ribbon quick access toolbar.
- The Ribbon Class How to work with the ribbon class. Methods, Properties, Events
- EventLogger Since Windows 8: Logging ribbon events
- UICollectionChangedEvent How to work with the ChangedEvent in an IUICollection
-
Working with miscellany Ribbon features
- ApplicationModes How to work with ribbon application modes.
- SizeDefinition How to define custom size definitions for ribbon group elements.
- Localization How to localize a ribbon.
- Changing Ribbon Colors How to change the ribbon colors.
- Working with Images How to work with images in the ribbon.
- Use Ribbon as External DLL How to load ribbon resources from external DLLs.
- Wrapper class RibbonItems An auto generated wrapper class from the ribbon markup.
-
Designing, building, previewing Windows Ribbon with RibbonTools
- RibbonTools basics Settings, Command line, ...
- Create a new project Create a WordPad sample project
- Preview the Ribbon
- Specifying Ribbon Commands
- Designing Ribbon Views
- Convert Images to Alpha Bitmaps
-
Modeling Guidelines
-
How to ...