Since the introduction of the .NET Framework in 2002, MFC has not been an area that has seen many new features added or a lot of focus from Microsoft. Much of the slow-down in MFC can be attributed to the lack of a major new release of the Windows operating system during this period, but there was also an undercurrent that native development was dead, and this made any MFC improvements redundant. With the release of Visual Studio 2008 and the thousands of new native APIs in Windows Vista, native development is alive and well, and thankfully MFC has benefited from this native renaissance with a range of new controls and improvements to existing controls that support the new look-and-feel of Vista.
For new MFC applications created with Visual C++ 2008, all MFC controls that are added to a dialog will automatically take advantage of the new Vista controls when the MFC application is executed on Vista or above. In much the same way, taking an application that was developed with an earlier version of Visual C++ and recompiling it with Visual C++ 2008 will result in an executable that will take advantage of Vista common controls when run on Vista. When executed on Windows Server 2003 or earlier, MFC-based applications will gracefully downgrade to the older common controls. The File Save dialog that is produced by taking the Scribble sample application that shipped with Visual C++ 6, re-compiling it with Visual C++ 2008, and running the application on Windows Vista is shown in Figure 1.
Figure 1: Scribble Compiled with Visual C++ 2008 running on Vista
The same executable will look like Figure 2 when executed on Windows Server 2003.
Figure 2: Scribble Compiled with Visual C++ 2008 running on Windows Server 2003
For the sake of completeness, Figure 3 shows the same Scribble application compiled using Visual C++ 2005 running on Windows Vista.
Figure 3: Scribble Compiled with Visual C++ 2005 running on Vista
In the case of the Scribble application, the MFC document-view architecture is automatically handling the display of the file dialogs, but if a CFileDialog-derived dialog is being created explicitly in code, a new optional parameter has been added to the constructor to specify whether Vista-style dialog boxes should be used, as shown in Figure 4.
Figure 4: CFileDialog constructor parameter list
Simply re-compiling a code base using Visual C++ 2008 will go some way to giving the application a Vista look-and-feel, but it will still be necessary to manually change a number of settings to fully take advantage of Vista. One of the first manual checks is to make sure the fonts in the various UI controls have a font setting that will display well on Vista. The font setting for the Pen Width dialog in Scribble is MS San Serif, which is a bitmap font that can’t be anti-aliased using Vista’s ClearType technology. The use of a non-ClearType font looks slightly out of place when executed on Vista, as shown in Figure 5.
Figure 5: Scribble Pen Width Dialog with MS San Serif font
By using the Properties Window of the Pen Width Dialog, the Use System Font property can be set to true. This setting means that the dialog will use the pseudo-font MS Shell Dlg. When this pseudo-font is used, a Registry key located at HKEY_LOCAL_MACHINESoftwareMicrosoftWindows NTCurrent VersionFontSubstitutes is used to translate this to a real font, which in the case of a Vista installation that uses English will be Microsoft Sans Serif. In contrast to the similarly named MS San Serif, Microsoft Sans Serif is an OpenType font format, and OpenType fonts can be anti-aliased with ClearType technology. Although the difference is subtle, as shown in Figure 6, the difference is apparent on Vista where ClearType display is the norm.
Figure 6: Scribble Pen Width Dialog on Vista with System Font set to True
The next issue to address is to upgrade Scribble to use Version 6 of the Windows Common Controls that shipped with Windows XP. Because of breaking changes between previous versions of Common Controls, Version 6 will not be used unless explicitly requested. The easiest way to upgrade an old MFC application to use the new Common Controls is to add the following code to stdafx.h.
#pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='220.127.116.11' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"")
This pragma comment adds a section in the manifest file that tells Windows to use Version 6 of Common Controls, giving the much-improved Pen Width dialog shown in Figure 7.
Figure 7: Scribble Pen Width Dialog on Vista with Common Controls V6
The final upgrade to Scribble for this article is to auto-hide the menu bar and have it appear only when the ALT key is held down. The CFrameWnd class has a new method called SetMenuBarVisibility that controls whether the menu bar is visible by default. The three possible settings for menu bar visibility are AFX_MBV_KEEPVISIBLE, AFX_MBV_DISPLAYONFOCUS (menu is shown when the Alt key is pressed), and AFX_MBV_DISPLAYONF10 (menu is displayed on F10). The Scribble sample application is a MDI application, and CMDIFrameWnd overrides SetMenuBarVisibility to prevent the menu bar from being hidden. This was done because “it was unclear what should be happening with minimize/restore/close buttons on menu bar associated with the child window. Also, most MDI apps have complex menus and Vista UX guidelines do not encourage hiding complex menu bars” according to Sarita Bafna from the Visual C++ Team is this MSDN forum posting.
To allow Scribble to have hidden menu bars, it needs to be converted to a SDI application; this can be accomplished by changing various MFC base classes used in the Scribble code base from the MDI version to the SDI equivalent. As a final touch, an upgraded set of icons for the main frame (IDR_MAINFRAME) has been copied from a Visual C++ 2008-generated MFC application into Scribble. The updated icons have an added higher-resolution image that is 48 by 48 pixels, and also includes 8-bit and 24-bit versions of all icons. The final updated form of Scribble is shown in Figure 8.
Figure 8: The Final Product – Scribble with updated icons and auto-hidden menu bar
Whereas upgrading the interface of an application to support the latest operating system look-and-feel is typically not the favourite exercise for a Visual C++ developer, the improvements to MFC 9 make the task much easier. Simply by re-compiling an application with Visual C++ 2008, a number of the Vista updates are automatically applied to the application, and with a few additional method calls, many other enhancements are possible. MFC takes on a lot of the upgrade heavy lifting, but for applications with complex user interfaces, and particularly those that make significant use of owner-drawn controls, the interface upgrade process will not be a trivial undertaking.
About the Author
Nick Wienholt is a Windows and .NET consultant based in Sydney, Australia. He has worked on a variety of IT projects over the last decade and continues to stay involved in the developer community. Nick is the co-founder and president of the Sydney Deep .NET User group, writes technical articles for Pinnacle Publishing and the Microsoft Developer Network, and is a participant in many .NET-related newsgroups. Nick’s most recent book is Maximizing .NET Performance.