Programming Applications for Microsoft Windows (Microsoft Programming Series)

[Previous] [Next]

The API macros simplify certain common operations, such as creating a new font, selecting the font into a device context, and saving the handle of the original font. The code looks something like this:

HFONT hfontOrig = (HFONT) SelectObject(hdc, (HGDIOBJ) hfontNew);

This statement requires two casts to get a warning-free compilation. One of the macros in WindowsX.h was designed for exactly this purpose:

#define SelectFont(hdc, hfont) \ ((HFONT) SelectObject( (hdc), (HGDIOBJ) (HFONT) (hfont)))

If you use this macro, the line of code in your program becomes the following:

HFONT hfontOrig = SelectFont(hdc, hfontNew);

This code is easier to read and is much less error-prone.

Several other API macros in WindowsX.h help with common Windows tasks. I urge you to examine them and use them.

Категории