Improving MFC for Windows Vista

Before its release, many wondered if Microsoft Visual Studio 2008 (or Version 9.0), the latest version of one of the most popular IDE and compilers, would include enough updates to motivate corporations and developers to make the transition. This article will take a look at the differences between it and the previous version, with special attention paid to the Microsoft Foundation Class library.

A successful program is worth the attention of developers when it improves its features and what you can do with it. In theory you should get real substance, but in practice, sometimes all you get is marketing: an increase in version count, an attempt to resolve a few bugs, give it an interface lift, and voila! It is sold as a new, improved program. We’ve all seen this happen more than once.

In this way marketers lure anyone who has already bought earlier versions to spend even more money on an upgrade despite the fact that they won’t really benefit from it. So here we have the new version of probably any developer’s most important tool: a compiler and source code editor all in one application (IDE meaning Integrated Development Environment).

With this launch Microsoft also decided to go back to taking two years to develop a new version, with the associated advantages and disadvantages of this approach. While it’s obvious that in this way you will get many new technologies that are launched daily in our hectic IT departments more quickly, this also results in less time for the development teams to properly improve their product.

As result many additions are launched later on as a pack that you can install separately after downloading it from the Microsoft site. But you also shouldn’t forget that you’ll have to pay for the upgrade more often.

Microsoft Foundation Class Library (or MFC, as it is widely known) is one of those items that seems to get relatively short shrift from Microsoft, as lately they seem more interested in promoting their .Net platform via the C# language, to say nothing of their flagship operating system. 

MFC is basically a library that facilitates easy control of programming applications for the ubiquitous Microsoft OS: Windows. As the last OS was released back in 2001 (the ominous Windows XP), all of the controls it had have been integrated into the compiler released after that. Happily enough, a new OS took its place in the meantime in 2007, and it came with a major face lift, so plenty of improvements can be made.

Also the there was a rumor spread in the world of the ID that native development wouldn’t be an option for the future. This in and of itself makes any MFC improvements redundant, although many companies still look at it as a strong option, offering an easy control of the UI. And all of this coming from C++ instead of C#. It is a known fact that C++ offers a more accurate, in-depth control/connection with the operating system than traditional C# methods.

Happily the new interface is created by the GPU; it’s more stylish and also takes up more system resources, but has given birth to new controls. This means that native development is cranking right along. As to whether this is good or bad, thousands of words have been written on that topic here on the Dev Shed network so feel free to look into it.

Microsoft eventually made clear that they hadn’t forgotten about MFC users and came up with a totally rewritten, improved version of the MFC. This has been made by one of the most influential companies in this segment and includes tons of new improvements, the appearance of new controls and so on (which, of course, didn’t made it into the initial release, so the download pack/install is needed).

But for now we aren’t interested in this. We only want to see what we can obtain with just a recompile of our source code (as we may have written the application for the previous OS under, let’s say, Visual Studio 2005 – version 8) or very little modification. The new controls will be presented in a future article, so for now let’s get started.

{mospagebreak title=First Impressions}

For a start let’s examine how a simple project made by Visual Studio 2005 looked. We will ignore any improvements offered by Vista itself as we are only interested in what changes are brought by Visual Studio 2008 itself, not what additions will automatically be implemented by the Vista OS. To test this I used Vista Ultimate Edition with some custom styling based on the Aero Interface.


-> Basic with VS2005 under Vista <-


-> Basic with VS2008 under Vista <-


Remember that all we did was recompile the source code after Studio converted the solution from version 8 to 9. The first and only obvious change added to it is the toolbar, which takes the Systems style with the version 9 compiler.

Also, the new version offers the option of using higher-quality icons, giving a 48X48 resolution and two versions, 8 bits or 24 bits. This way the icons will look nicer on those high resolution computers we all tend to have these days.

Basically this is all you can squeeze from this part with just a recompile. It’s not much, however I’m sure you observed that IE 7 and the new Media Player from Microsoft don’t have the menu bar on by default. It just pops up once you push the ALT key.

This little trick can be implemented easily with one line of code in any SDI application. The reason these work for SDI only is that the Visual C++ developing team found that it was unclear what should happen with a MDI application with the child Windows close/minimize/maximize button as Sarita Banta expressed in a forum post over the MSDN forum.

Additionally “most of the MDI apps have complex menus and Vista UX guidelines do not encourage hiding complex menu bars.” So this function has been overwritten in the MDI-based application, so if you pass any argument other than "keep always on" you’ll get an assertion failure.

The argument I’m talking about is  AFX_MBV_KEEPVISIBLE, which tells the compiler to maintain the old-style menu and will be applied by default. The argument can also be set to AFX_MBV_DISPLAYONFOCUS (for ALT toggle of the menu bar) and AFX_MBV_DISPLAYONF10 (for an F10 key toggle). Of course these can be combined by applying a bitwise or operation.

All you have to do is call the:


virtual void SetMenuBarVisibility( DWORD nStyle );


Like this for an ALT toggle:


SetMenuBarVisibility(AFX_MBV_DISPLAYONFOCUS);


To go from a window like this compiled under VS 8.0:



To a more stylish:



Or even a less busy version if we deactivate the toolbar also… see below.



One more tip: you may want to make sure to use the Clear Type technology implemented in Vista. This allows you to use anti-aliasing on the fonts for a more stylish feeling to them, and also to make sure that some texts (mainly in Dialogs) don’t look slightly off.

To do so you only need to use a font that supports anti-aliasing. Just about any free types that use bitmap images can be applied to it, but to make sure they are a little more synchronized with the system, choose the Use System font in the dialogs Properties window.:


This will lead to getting the current system that is saved under the registry key in the: HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsNTCurrentVersion FontSubstitutes path. With a Vista with English installation this will be Microsoft Sans Serif.

{mospagebreak title=The Open/Save Dialog}

One of the most impressive changes made to Vista that you’ll observe once you get to mess around a little is the improved Explorer. With it there are also some more stylish and practical open/save dialogs.

In MFC you can pop up these kinds of dialogs by simply declaring a CFileDialog type and calling its DoModal() function. So implementing this can be done on the fly and resolved by the container. Once you recompile an older solution with the new one you’ll automatically replace the old ones with the more stylish/practical ones.

To maintain compatibility with older applications and still leave a path for the user to choose which one to use, the solution was to simply extend the constructor in the new library with a extra parameter that signals whether or not to use the Vista style open dialog.


explicit CFileDialog(BOOL bOpenFileDialog,

LPCTSTR lpszDefExt = NULL,

LPCTSTR lpszFileName = NULL,

DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

LPCTSTR lpszFilter = NULL,

CWnd* pParentWnd = NULL,

DWORD dwSize = 0,

BOOL bVistaStyle = TRUE);


So we get the following new Open/Save dialog with a simpler Save As… that initially doesn’t show the folder view tree, but lets you choose the save directory by choosing the Browse Folders button.


->Old type open <-

->New type open<-


->Old type Save As <-

->New type Save As <-


{mospagebreak title=Final Thoughts}

All of this can be realized with just a recompile or a small addition of code, with minimal effort. It won’t give you some astonishing result, but it can still make all the difference when you are using the software every day, to give you the feeling of a new, modern application. 

The beauty of all this is that if you run the application on a previous OS (for example, Windows XP), of course the OS doesn’t have these advanced controls, so it will downgrade gracefully to the old ones.

This alone may be a small improvement as far as motivating the purchase of a new compiler — and with it, the text editor, because all in all Visual Studio is this and nothing more: an advanced text editor with a compiler in it. But this is just a small fragment of the new features it brings to the world of MFC.

Of course if you have a more complex application with a lot of controls that have been heavily modified, the conversion to the new version might prove a little more troublesome, as they may conflict with some new features. But the new MFC improvement brings many helpful additions.

Some of them were already implemented by many user-made classes that you can find at the CodeProject or CodeGuru websites. The only problem is that not all creators of these classes can invest the time in correcting bugs, while the ones in the new MFC pack have been written by MS and are heavily bug tracked. So why not take advantage of them?

I will talk about additions that require a little more work than just a recompile or a line added/changed in a future article, so stick around if you want to find out more. Also I invite you to express your thoughts/ideas here on the blog or join our friendly forum over the DevHardware. Have a great day until then…

[gp-comments width="770" linklove="off" ]