A Custom MDI Client Class

Environment: The code has been tested using VC5 and VC6 under Win98 and WinNT 4.0 and it works fine.

What’s this?

The CMDIClient is a quite simple CWnd-derived class that allows you to display any
bitmap image in MDIClient area or choose any background color. You can load the image from
file as well as from the resource and you can choose the mode what the image will be
displayed in. Available modes are: tile, center, stretch and custom. In the last mode you
can set manually left-top coordinates of the image and display it at any position you
wish. Additionally, the class provides two functions for saving/restoring current settings
to the registry. The saving/restoring can be done automatically (on WM_DESTROY and
PreSubclassWindow() ) if m_bAutoSaveRestore flag is set to TRUE (in fact, that’s set by
default). When the bitmap is loaded from the resource, you can map some colors to the
system colors using the COLORMAP structure, (e.g. you can map light gray to COLOR_3DFACE
and so on, see the demo project for more details).

The bitmap is drawn in CMDIClient’s OnPaint() message handler (the original OnPaint()
isn’t called at all). There’s no palette support in this version so don’t expect good
effects when you are in 256 (or less) color mode and you try to display a 256 color
bitmap. The demo project includes a simple dialog, which allows you to set all properties
at runtime, so just try it!

CMDIClient properties

Set or get the background color. For painting the background we use FillRect (not
FillSolidRect for some reasons), so we need a brush. The brush is created based on a
supplied color when SetBkColor is called.

COLORREF GetBkColor() const;
void SetBkColor( COLORREF clrValue );

Load background bitmap from given file.

BOOL SetBitmap( LPCTSTR lpszFileName, UINT uFlags = LR_LOADMAP3DCOLORS );

Load background bitmap from resource. You can map some colors using the COLORMAP struct (see
LoadMappedBitmap in MSDN for details how to use it).

BOOL SetBitmap( UINT nBitmapID, COLORMAP* pClrMap = NULL, int nCount = 0 );

Set or get the current display mode (dispTile, dispCenter, dispStretch or dispCustom).

void SetDisplayMode( DisplayModesEnum eDisplayMode );
DisplayModesEnum GetDisplayMode() const;

Specify or retrieve the coordinates of image top-left corner. Used when dispCustom is selected. If
bRedraw is set to TRUE, the entire window is invalidated.

void SetOrigin( int x, int y, BOOL bRedraw = TRUE );
void SetOrigin( const CPoint& point, BOOL bRedraw = TRUE );
const CPoint& GetOrigin() const;

Return the current image size. If there’s no image, (0,0) is returned.

const CSize& GetImageSize() const;

Return the filename of the bitmap. Empty string if there’s no image or if the image was loaded from the resource.

const CString& GetFileName() const;

If set to TRUE, the current state is automatically saved on WM_DESTROY and restored on
PreSubclassWindow(). If you would like to control this manually, set it to FALSE and call
SaveState()/RestoreState() when you want.

void SetAutoSaveRestore( BOOL bNewValue );
BOOL GetAutoSaveRestore() const;

CMDIClient methods

Restore original (system-wide) settings for the MDIClient and repaint the window.

void Reset();

Load/store current settings in the registry

void SaveState();
void RestoreState();

For further information look at the CMDIClient header and implementation files.

How to use it?

First, add to your CMainFrame class implementation file a member
variable of type CMDIClient. A good practice is to keep the variables protected (or
private) and uses an inline functions to gain access to them, if needed. Then within
OnCreate() handler add a following code.

// Subclass the MDI client window
VERIFY( GetMDIClient().SubclassWindow( m_hWndMDIClient ) );

After that you can set the desired MDIClient properties by calling appropriate functions.

Download demo project – 37 KB

Download source – 5 KB

Date Last Updated: April 3, 1999

More by Author

Must Read