Click here to Skip to main content
12,816,775 members (32,650 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 2 Mar 2010

Windows Ribbon for WinForms, Part 3 - First WinForms Ribbon Application

, 4 Mar 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
In this article, I'll present how to create an empty WinForms application with Windows 7 ribbon support.


This series of CodeProject articles is based on a series of posts I've first published on my blog.

So, let's see how to use the Ribbon class to add ribbon support to an empty WinForms application.

Step 0 - Download Windows Ribbon for WinForms

As I've mentioned in my previous posts, you can download the code for my ribbon wrapper library along with numerous samples (currently, one..) from Also, I remind you to download the helper library, Windows API Code Pack. Update (18.11.2009): The project doesn't depend anymore on Windows API Code Pack.

In this post, I'll review how to create your first, ribbon-enabled WinForms application. The result is the sample application "01-AddingRibbonSupport", included on the site.

Step 1 - Reference Windows Ribbon for WinForms

Create a new C# WinForms application, and add a reference to our developed library, Ribbon.dll.

Also, add to your main form (form1.cs) the following lines:

using RibbonLib;
using RibbonLib.Interop;

public partial class Form1 : Form, IRibbonForm
    private Ribbon _ribbon = new Ribbon();

Step 2 - Add ribbon markup XML file

Add an empty file named RibbonMarkup.xml to the project. Set the file text to the following:

<?xml version='1.0' encoding='utf-8'?>
<Application xmlns=''>


Right click on the editor where RibbonMarkup.xml is opened, and click Properties. Now, set the Schemas property to: C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\UICC.xsd. This will [visual] assist you in the future while editing the ribbon markup file (try Ctrl+Space on the XML editor to see the ribbon markup syntax).

Note: this is the moment where some of you will discover they didn't install Windows 7 SDK, so go ahead and fix it, I'll wait.

Step 3 - Set UI compiler to compile markup during build

Step 3.1 - Set pre-build events

Go to: Project Properties -> Build Events -> Pre-build event command line. Add the following three lines:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0\Bin\UICC.exe" "$(ProjectDir)RibbonMarkup.xml" 
   "$(ProjectDir)RibbonMarkup.bml" /res:"$(ProjectDir)RibbonMarkup.rc"
"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0\Bin\rc.exe" /v "$(ProjectDir)RibbonMarkup.rc" 
cmd /c "("$(DevEnvDir)..\..\VC\bin\vcvars32.bat") && ("$(DevEnvDir)..\..\VC\bin\link.exe" 
  /VERBOSE /NOENTRY /DLL /OUT:"$(ProjectDir)$(OutDir)$(TargetName).ribbon.dll" 


Explanation: The first line compiles the ribbon markup file to a binary compressed format, along with a small RC file that describes it. The second line creates a native Win32 resource file to be attached to the project native resources. The third line creates a resource DLL from the native Win32 resource file.

Step 3.2 - Compile

Compile, in order to invoke the pre-build events.

Update (18.11.2009): Step 3.3 is no longer needed since the resource is loaded from the resource DLL.

Step 3.3 - Add native resource

Go to: Project Properties -> Application -> Resources -> Resource File. Set Resource file to point to: RibbonMarkup.res. When it says it can't find the file, close the Properties window anyway. It will save and find the file. If you wish to be on the safe side, just set the Resource file to the full path of the file (which is dependent on your project location).


Step 4 - Implement IRibbonForm

Have your main form implement the IRibbonForm interface, which is defined in Ribbon.dll; you can follow the following guidelines:

  • The Ribbon framework needs the main form window handle for initialization, hence:
  • public IntPtr WindowHandle
            return this.Handle;
  • The Ribbon framework will partly hide your form, unless you write some code that repositions your controls according to the ribbon current height. A simple way of doing this is adding a SplitContainer control to the main form and setting the following properties (you should change them using the GUI properties, not by code):
  • this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
    this.splitContainer1.IsSplitterFixed = true;
    this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
    this.splitContainer1.SplitterWidth = 1;

    Now, all you need to do is put your form controls inside Panel2 of the SplitContainer and update Panel1 height when the ribbon height changes (Panel1 act as a placeholder for the ribbon). The SplitContainer will automatically reposition your controls accordingly. The actual update is done in the IRibbonForm implementation:

    public void RibbonHeightUpdated(int newHeight)
        this.splitContainer1.SplitterDistance = newHeight;
  • The major part of the communication between the ribbon framework and your application is done by implementing the two following methods for which we'll initially give a default implementation. Update (18.11.2009): These functions should not be implemented by the user anymore, since the Ribbon class now provides a general implementation, thus simplifying the use of the library.
  • public HRESULT Execute(uint commandId, ExecutionVerb verb, 
           PropertyKeyRef key, PropVariantRef currentValue, 
           IUISimplePropertySet commandExecutionProperties)
        return HRESULT.S_OK;
    public HRESULT UpdateProperty(uint commandId, ref PropertyKey key, 
           PropVariantRef currentValue, ref PropVariant newValue)
        return HRESULT.S_OK;

Step 5 - Initialize the Ribbon Framework on application load

To initialize and destroy the ribbon framework, handle both the Load and FormClosed events of the Form, respectively:

private void Form1_Load(object sender, EventArgs e)

private void Form1_FormClosed(object sender, FormClosedEventArgs e)

Enjoy your first Ribbon WinForms application


Note: If you run your application and don't see the ribbon, try to enlarge the window. The Windows ribbon has a feature that if the window size is too small, it doesn't show. Unfortunately, the Visual Studio default form size is too small.

That's it for now.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

You may also be interested in...


Comments and Discussions

QuestionI can not use InitFramework(this) or DestroyFramework() Pin
ahmadmahrous_19-Sep-13 20:30
memberahmadmahrous_19-Sep-13 20:30 
QuestionHow to change Items font Pin
behnam_jan10-Sep-12 12:33
memberbehnam_jan10-Sep-12 12:33 
QuestionOne Improvement and One Problem Pin
xxeonn11-Sep-11 8:10
memberxxeonn11-Sep-11 8:10 
I just want to start by saying that you have done an excellent job here and that this is the best implementation that I have seen so far. However there are a few issues that I had encountered during implementation of your method that could be improved that others after me may not experience the same.

1. Step 3.1 - Set pre-build events – During this step, I copied and pasted the text that was given for the pre-build step, but a direct copy/paste would not work because the formatting of the text caused the compiler to through an error with the code [9009]. It took me awhile to figure out what was happening as build scripts are very difficult to debug as it only returns the last error thrown which can be very vague at times. Good read on debugging build scripts here.

a. Solution – I read the article above and tried to use that method to debug the script but it was of limited help as the codes returned were still vague. What I did instead was to copy the Error Description from the Error List and paste each line in the VS2010 command line program and execute the noting the error codes. That’s when I was able to piece together what each line required. It also would have been nice to see what parameters the commands took.

"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.1\Bin\UICC.exe" "$(ProjectDir)RibbonMarkup.xml" "$(ProjectDir)RibbonMarkup.bml" /res:"$(ProjectDir)RibbonMarkup.rc"

"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.1\Bin\rc.exe" /v "$(ProjectDir)RibbonMarkup.rc" 

cmd /c "("$(DevEnvDir)..\..\VC\bin\vcvars32.bat") && ("$(DevEnvDir)..\..\VC\bin\link.exe" /VERBOSE /NOENTRY /DLL /OUT:"$(ProjectDir)$(OutDir)$(TargetName).ribbon.dll" "$(ProjectDir)RibbonMarkup.res")"

2. Installing on Windows XP – The other issue that I have not yet found a solution for is installing the application on Windows XP. After installing the application and wanting to run it, it says that a SystemNotSupported Exception had occurred.

System.PlatformNotSupportedException: The ribbon framework couldn't be found on this system. ---> System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {926749FA-2615-4987-8845-C33E65F2B957} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). at RibbonLib.Ribbon.CreateRibbonFramework()

I am thinking that the program requires some class in COM in order to run the application but I am not that advanced so would like some assistance.
AnswerRe: One Improvement and One Problem Pin
Arik Poznanski20-Sep-11 3:28
mvpArik Poznanski20-Sep-11 3:28 
NewsMy vote of 5 Pin
Member 783300622-Jul-11 13:23
memberMember 783300622-Jul-11 13:23 
QuestionUICC.xsd Pin
AmericoES24-Mar-10 14:15
memberAmericoES24-Mar-10 14:15 
AnswerRe: UICC.xsd Pin
Arik Poznanski24-Mar-10 21:54
memberArik Poznanski24-Mar-10 21:54 
AnswerRe: UICC.xsd Pin
xxeonn11-Sep-11 8:17
memberxxeonn11-Sep-11 8:17 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 4 Mar 2010
Article Copyright 2010 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid