Converting An MFC Ribbon To Designer Format
Hello, I’m Pat Brenner, a developer on the Visual C++ Libraries team, and I primarily work on MFC.
In Visual Studio 2010, a ribbon designer was added which allows you to visually edit the ribbon used in your MFC application. This doesn’t help you, however, if you adopted the ribbon user interface with Visual Studio 2008 SP1 and you’re creating your ribbon UI entirely with code in your application. We did consider that scenario, however, and added functionality to MFC to allow conversion of a “code” ribbon to an XML ribbon resource. I’ve been meaning to post an article on how to do this, so here it is.
I’ll use the conversion of the ribbon in the MSOffice2007Demo sample as an example:
- Download (if necessary) and open the MSOffice2007Demo sample solution. It can be found at this link (http://archive.msdn.microsoft.com/vcsamplesmfc ) under the “MFC Samples – Visual C++ 2008 Feature Pack” section. You may want to make a copy first if you want to keep the original sample around for reference.
- Build and run the solution. Check the contents of the different tabs of the ribbon. Exit the application.
- Open MainFrm.cpp and go to line 196 (where CreateStatusBar is called).
- Insert the following code at line 196 (immediately following the call to CreateRibbonBar):m_wndRibbonBar.SaveToXMLFile(“”);
- Right click on the file tab and select “Copy Full Path” to get the full path name for the file.
- Put the cursor between the double-quotes and insert the filename. Then modify the path to write the file to MSOffice2007Demo.mfcribbon-ms in the RES folder.
- Build and run the application again. As soon as the application starts up, you can exit the application. This will leave the XML file created on disk in the RES folder (with the mfcribbon-ms extension).
- Open the MSOffice2007Demo.mfcribbon-ms XML file and reformat it, using the “Format the whole document” button in the “XML Editor” group on the toolbar (or use the Ctrl+K, Ctrl+D keyboard shortcut to format the file).
- Edit the resource.h file using the text edit and add the ribbon resource ID:#define IDR_RIBBON 33000
- Edit the MSOffice2007Demo.rc file using the text editor and add the ribbon resource (since the ribbon is not a native resource type, 28 is used as the number for the ribbon resource type):IDR_RIBBON 28 DISCARDABLE “res\\MSOffice2007Demo.mfcribbon-ms”
- Remove the call to SaveToXMLFile from MainFrm.cpp (that you added earlier in steps 5 and 6).
- Remove lines 362-373 from MainFrm.cpp, the code to add ribbon tabs in code (except for the Developer tab).
- Add one line at line 362:m_wndRibbonBar.LoadFromResource(IDR_RIBBON);
- Build and run the application again. Verify that all the ribbon UI looks the same as it did in step 2.
- You can now open the RC file using the Resource View and edit the ribbon resource using the ribbon designer.
I hope you find this information helpful.
Pat Brenner Visual C++ Libraries Development