Changes to MFC in Microsoft Visual Studio 2010

In the previous articles in this series I've wrote about the changes in the build system, IntelliSense and browsing, and C++ compiler.
In this article I will write about the changes to MFC or the IDE in relation to MFC: new MFC toolbar controls, task dialog support, restart manager support and the ribbon designer.

New Toolbar Controls

A couple of years ago when the MFC Feature Pack (later distributed with Visual Studio 2008 SP1 and renamed as VC++ Feature Pack) was released it come with classes for new controls, some of them controls introduced in Windows Vista.
However, the IDE did not support using these new controls with the resource editor. Developers had to create everything manually, during runtime.
The new Visual Studio offers support for these controls in the resource editor. They can be used just like any other controls. However, not all the properties of these controls can be set from the Properties window. Some of them still require explicit coding.

Here is the list of the controls, the class that implement the controls and a short description of the controls:

Shell list (CMFCShellListCtrl): a list control that displays the files and folders from you system just list Windows Explorer list view does

Shell tree (CMFCShellTreeCtrl): a tree control that displays the folder from your system just like the Windows Explorer folder view does

Link control (CMFCLinkCtrl): is a special button that has the appearance of a hyperlink and invokes the target link when pressed

The following images shows a dialog with these controls.

Task Dialog

One new feature introduced with Windows Vista (and available on all the following operating systems) was the task dialog. This is a replacement for the classic dialog box.
This new dialog can display command links, customized buttons, icons, and a footer with an icon (optionally) and text.

The class that implements this dialog is called CTaskDialog and is available in header <afxtaskdialog.h>. It is neither a CDialog nor a CWnd derived class; it's actually derived from CObject.
The task dialog is only supported for UNICODE builds, and only on operating systems newer that Vista. This is enforced with #error directives in the header file.

The task dialog displayed by running the above code looks like this (on Windows 7):

It is possible to instantiate a task dialog without creating a CTaskDialog object and calling DoModal(). The class features a static method called ShowDialog() that does basically the same (not so much configurable).

The entire support is added with a single line of code. Class CWinApp has a member called m_dwRestartManagerSupportFlags, that stores the flags for the restart options. Setting this variable to the appropriate value is all that is necessary.

Restarting after a crash happens only if the application was running for at least 60 seconds. This is done for preventing cyclic restarts. The following image shows the application with an unsaved document restarting:

After restart the document is empty. Since it was not saved it could not be saved. The next image shows the application restarted after a crash, but with the document saved.

Even with document recovery, auto save interval is set to 5 minutes. That means documents are saved only every 5 minutes, and if a crash happens before that interval it cannot be restored. But the interval time can be adjusted to any value. This can be done in the InitInstance() method:

The options for restarting the application and saving and loading the application data (documents) are exposed through virtual methods in the CWinAppEx class. You can override these methods in your application for custom handling of the save and load operations.

virtual void PreLoadState() {} // called before anything is loaded
virtual void LoadCustomState() {} // called after everything is loaded
virtual void PreSaveState() {} // called before anything is saved
virtual void SaveCustomState() {} // called after everything is saved

For customizing MFC document recovery you could read this article by Nick Wienholt.

Ribbon Designer

Another feature that was introduced with VC++ Feature Pack was the Office Fluent Ribbon. The problem was that there was no support for a ribbon designer. You had to create the entire ribbon manually.
Visual Studio 2010 now comes with a ribbon designer. You can choose whether to use a ribbon or a classical menu and toolbar when you create an application.

The ribbon created by default is minimal; it has a Home category and two panels with several commands.

The ribbon can be opened from the resource editor. There is a new category called Ribbon. By default the ribbon resource is called IDR_RIBBON. The description of the ribbon is kept in an XML file called ribbon.mfcribbon-ms, located in the res folder.

The toolbar shows only the controls available for the ribbon when a ribbon is opened in designer.

The ribbon supports several Office 2007 and Windows 7 styles.

The designer provides support for quick testing of the ribbon. On the Ribbon Editor toolbar there is a button called Test Ribbon that opens window with the ribbon. You can quickly see how it will look in the application, however, the commands are not available; clicking on the ribbon commands does not have any effect.

References

About the Author

Marius Bancila

Marius Bancila is a Microsoft MVP for VC++. He works as a software developer for a Norwegian-based company. He is mainly focused on building desktop applications with MFC and VC#. He keeps a blog at www.mariusbancila.ro/blog, focused on Windows programming. He is the co-founder of codexpert.ro, a community for Romanian C++/VC++ programmers.

Comments

please Email the source to me

Posted by bjperry
on 07/12/2011 03:15am

I am very interested in your program, please Email the source to me, thank you!

Top White Papers and Webcasts

Migrating away from Windows Server 2003 is an investment in your organization's future, and there has never been a better time to begin the migration process. Take the next step to transform your datacenter by upgrading your server platform with leading edge Windows Operating Systems and SanDisk flash solutions.

Data integrity and ultra-high performance dictate the success and growth of many companies.
One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …