Click here to Skip to main content
15,888,579 members
Articles / Desktop Programming / Windows Forms

Easily Add a Ribbon into a WinForms Application

7 Aug 2019Ms-PL2 min read 2.3M   38.1K   618   655
Easily add a ribbon to a WinForms application

Style 2007

Image 1

Style 2010

Image 2

Style 2013

Image 3

Background

The ribbon that is going to be used in this article is an open source project created by Jose Menendez Poo. However, the original author of the ribbon has stopped support of it. A group of fans of this ribbon re-host and continue to develop/enhance and support the ribbon.

The original ribbon creator has posted an article explaining what this ribbon is all about at [A Professional Ribbon You Will Use (Now with orb!)]. However, that article doesn't describe how to use it in your project. Therefore, this article will show how to use it.

How to Use this Ribbon Control

The ribbon class library can be added via Nuget. Right click your project and go to "Manage Nuget Packages..."

Image 4

Search "RibbonWinForms" and install the nuget package.

Image 5

Drag the ribbon control into the form.

Image 6

Click "Add Tab".

Image 7

Click "Add Panel".

Image 8

Showing the commands of the "Panel":

Image 9

Click the command and add the controls you want.

Image 10

Changing the icon and button text label.

Image 11

Adding the "Click" event.

Image 12

Type the commands that you wish to do in the event block.

Image 13

Now, try run the application.

Image 14

Example of using RibbonForm style.

Image 15

Replace the inheritance of "Form":

Image 16

to "RibbonForm".

Image 17

Note

Some behaviour of "RibbonForm" might not be working properly in some unknown circumstances. We recommended that you to perform the test in your environment before delivering your application into production.

Alternative Ribbon Control

Windows Ribbon Framework
https://docs.microsoft.com/en-us/windows/win32/windowsribbon/-uiplat-windowsribbon-entry 
Support Windows 7 onwards

Krypton WinForms components for .NET
https://github.com/ComponentFactory/Krypton

RibbonLib
https://github.com/ennerperez/RibbonLib 
https://www.nuget.org/packages/RibbonLib

Windows Ribbon for WinForms
https://github.com/ennerperez/RibbonLib 
http://blogs.microsoft.co.il/arik/2010/11/08/windows-ribbon-for-winforms-v26-released/ 
http://www.codeproject.com/Articles/55599/Windows-Ribbon-for-WinForms-Part-0-Table-of-Conten

Ribbon by Juan Pablo G.C.
http://www.codeproject.com/Articles/18449/An-easy-way-to-add-a-Ribbon-Panel-Office-2007-styl 
http://www.codeproject.com/Articles/19044/The-new-RibbonForm-RibbonRoundButton-and-FastMenu

History

  • 8th August, 2019: Publish Nuget Package, several bug fixes
  • 12th April, 2012: Initial version

License

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


Written By
Product Manager
United States United States
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is product, user interface, and usability.
- TypeScript / React expert

@geeksplainer

Written By
Software Developer
Other Other
Programming is an art.

Written By
Software Developer (Senior)
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Software Developer (Senior)
United States United States
Lead Software Engineer on a paperless office / workflow system. I specialize in building custom user controls and components that are using in my everyday software. I've also worked with Neural Networks for character recognition on documents in my paperless office software.

Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFlashImage Pin
paintbox0019-Feb-13 4:15
paintbox0019-Feb-13 4:15 
AnswerRe: FlashImage Pin
toATwork19-Feb-13 4:32
toATwork19-Feb-13 4:32 
GeneralRe: FlashImage Pin
paintbox0019-Feb-13 4:36
paintbox0019-Feb-13 4:36 
SuggestionRe: FlashImage Pin
toATwork19-Feb-13 4:38
toATwork19-Feb-13 4:38 
GeneralRe: FlashImage Pin
paintbox0019-Feb-13 4:41
paintbox0019-Feb-13 4:41 
AnswerRe: FlashImage Pin
toATwork19-Feb-13 5:10
toATwork19-Feb-13 5:10 
GeneralRe: FlashImage Pin
paintbox0019-Feb-13 5:16
paintbox0019-Feb-13 5:16 
Questionnext release can solve the handle on xp and server2003??? when? Pin
mywaster@sina.cn18-Feb-13 3:17
mywaster@sina.cn18-Feb-13 3:17 
///
/// Processes the WndProc for a form with a Ribbbon. Returns true if message has been handled
///

/// <param name="m" />Message to process
/// <returns><c>true if message has been handled. <c>false otherwise
public virtual bool WndProc(ref Message m)
{
if (DesignMode) return false;

bool handled = false;

if (WinApi.IsVista)
{
#region Checks if DWM processes the message
IntPtr result;
int dwmHandled = WinApi.DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, out result);

if (dwmHandled == 1)
{
m.Result = result;
handled = true;
}
#endregion
}

//if (m.Msg == WinApi.WM_NCLBUTTONUP)
//{
// UpdateRibbonConditions();
//}

if (!handled)
{
if (m.Msg == WinApi.WM_NCCALCSIZE && (int)m.WParam == 1) //0x83
{
#region Catch the margins of what the client area would be
WinApi.NCCALCSIZE_PARAMS nccsp = (WinApi.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(WinApi.NCCALCSIZE_PARAMS));

if (!MarginsChecked)
{
//Set what client area would be for passing to DwmExtendIntoClientArea
SetMargins(new Padding(
nccsp.rect2.Left - nccsp.rect1.Left,
nccsp.rect2.Top - nccsp.rect1.Top,
nccsp.rect1.Right - nccsp.rect2.Right,
nccsp.rect1.Bottom - nccsp.rect2.Bottom));

MarginsChecked = true;
}

Marshal.StructureToPtr(nccsp, m.LParam, false);

m.Result = IntPtr.Zero;
handled = true;
#endregion
}
else if (m.Msg == WinApi.WM_NCACTIVATE && Ribbon != null && Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
{
Ribbon.Invalidate(); handled = true;
}
else if ((m.Msg == WinApi.WM_ACTIVATE || m.Msg == WinApi.WM_PAINT) && WinApi.IsVista) //0x06 - 0x000F
{
m.Result = (IntPtr)1; handled = false;
}
else if (m.Msg == WinApi.WM_NCHITTEST && (int)m.Result == 0) //0x84
{
#region Check the Non-client area hit test

m.Result = new IntPtr(Convert.ToInt32(NonClientHitTest(new Point(WinApi.LoWord((int)m.LParam), WinApi.HiWord((int)m.LParam)))));
handled = true;

if (Ribbon != null && Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
{
//Kevin Carbis - this refresh call caused severe cpu usage while moving the mouse over the
//caption bar. I have been trying to find a way to make a less demanding call to paint.
//Form.Refresh();
//WinApi.InvalidateWindow(Form.Handle);
//Form.Invalidate();
//Ribbon.Invalidate();
Ribbon.RedrawArea(Ribbon.ClientRectangle);
}
//Console.WriteLine("Msg " + m.Msg.ToString() + " " + m.Msg.ToString("X"));
#endregion
}
else if (m.Msg == WinApi.WM_NCACTIVATE && Ribbon != null && Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
{
Ribbon.Invalidate(); handled = true;
if (m.WParam == IntPtr.Zero) // if could be removed because result is ignored if WParam is TRUE
m.Result = (IntPtr)1;
}
else if (
(Ribbon != null && Ribbon.ActualBorderMode != RibbonWindowMode.NonClientAreaCustomDrawn) &&
(m.Msg == WinApi.WM_SYSCOMMAND || m.Msg == WinApi.WM_WINDOWPOSCHANGING || m.Msg == WinApi.WM_WINDOWPOSCHANGED))
{
Ribbon.Invalidate();
}
else if (m.Msg == WinApi.WM_NCMOUSELEAVE)
{
}
}

return handled;
}



======================
add the code is right?????
AnswerRe: next release can solve the handle on xp and server2003??? when? Pin
toATwork18-Feb-13 3:25
toATwork18-Feb-13 3:25 
Questioncoult not messagebox Pin
mywaster@sina.cn17-Feb-13 14:15
mywaster@sina.cn17-Feb-13 14:15 
AnswerRe: coult not messagebox Pin
toATwork17-Feb-13 20:36
toATwork17-Feb-13 20:36 
GeneralRe: coult not messagebox Pin
mywaster@sina.cn17-Feb-13 20:59
mywaster@sina.cn17-Feb-13 20:59 
AnswerRe: coult not messagebox Pin
toATwork17-Feb-13 21:03
toATwork17-Feb-13 21:03 
GeneralRe: coult not messagebox Pin
mywaster@sina.cn17-Feb-13 21:17
mywaster@sina.cn17-Feb-13 21:17 
AnswerRe: coult not messagebox Pin
toATwork17-Feb-13 21:35
toATwork17-Feb-13 21:35 
GeneralRe: coult not messagebox Pin
mywaster@sina.cn17-Feb-13 21:43
mywaster@sina.cn17-Feb-13 21:43 
GeneralRe: coult not messagebox Pin
mywaster@sina.cn17-Feb-13 21:58
mywaster@sina.cn17-Feb-13 21:58 
GeneralRe: coult not messagebox Pin
mywaster@sina.cn18-Feb-13 1:52
mywaster@sina.cn18-Feb-13 1:52 
GeneralRe: coult not messagebox Pin
toATwork18-Feb-13 1:54
toATwork18-Feb-13 1:54 
QuestionNo Keyboard control ? Pin
gogogoggg17-Feb-13 7:27
professionalgogogoggg17-Feb-13 7:27 
AnswerRe: No Keyboard control ? Pin
toATwork17-Feb-13 7:37
toATwork17-Feb-13 7:37 
QuestionRibbonForm Pin
gogogoggg14-Feb-13 9:32
professionalgogogoggg14-Feb-13 9:32 
QuestionRe: RibbonForm Pin
toATwork14-Feb-13 21:15
toATwork14-Feb-13 21:15 
AnswerRe: RibbonForm Pin
gogogoggg14-Feb-13 22:54
professionalgogogoggg14-Feb-13 22:54 
QuestionRibbonTab/Orb Style Pin
ccdn14-Feb-13 0:20
ccdn14-Feb-13 0:20 

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.