Irregular Shaped Bitmap Dialog

The CBitmapDialog class allows you to create a dialog that has a bitmap background, and to have a specified color in the background be transparent. This allows the dialog to be in other shapes than a box, like the one shown above.

Usage

Use this class like CDialog, but there are several extra functions that you can use:

// ** Constructors **

// Use the normal CDialog constructors plus the following
// as the last four parameters:
//
// LPCTSTR lpszResourceName
// UINT nIDResource
// LPCTSTR lpszFilename
// CBitmap *pBitmap
//
// Example:
// CDialog(int,CWnd*)):CBitmapDialog(IDD_MAINDIALOG,
this,
NULL,
IDB_BITMAP,
NULL,
NULL);
//
// Use at most one of these parameters and leave the rest
// NULL or 0. If you do not want to set a bitmap in the
// constructor, look below.

// ** Bitmap Setting **

// Loads a bitmap from a resource string or filename.
// Leave either lpszResourceName or lpszFilename NULL.
BOOL LoadBitmap (LPCTSTR lpszResourceName,
LPCTSTR lpszFilename);

// Loads a bitmap from a resource ID
BOOL LoadBitmap (int nIDResource);

// Makes a copy of a given bitmap and uses the copy.
// This is like SetBitmap but you are not responsible
// for handling the bitmap.
BOOL CopyBitmapFrom (CBitmap *pBitmap);

// Uses the bitmap you specify. NOTE: You are responsible
// for handling the bitmap.
void SetBitmap (CBitmap *pBitmap);

// ** Transparency **

// Specifies whether the window should use transparency.
// TRUE for transparent, FALSE for opaque. (default is
// FALSE)
void SetTransparent (BOOL bTransparent);
BOOL GetTransparent ();

// Specifies the color that you want to be transparent.
void SetTransColor (COLORREF col);
COLORREF GetTransColor ();

// Specifies whether static (text) controls have a
// transparent background.
// TRUE for transparent, FALSE for opaque. (default
// is TRUE)
void SetStaticTransparent (BOOL bTransparent);
BOOL GetStaticTransparent ();

// ** Dragging **

// With a transparent dialog, you usually won’t have a
// caption (like my example).
// These functions make it so that you can drag anywhere
// on the dialog to move it. TRUE is on and FALSE is off.
// (default is FALSE)
void SetClickAnywhereMove (BOOL bMove);
BOOL GetClickAnywhereMove ();

Tips

* In the resource editor, you can’t tell exactly how big
the dialog will need to be to hold the entire bitmap. You
could either try out different sizes, or if you don’t have
“show window contents while dragging” (windows option) enabled,
you could drag the dialog and see how much extra dialog you
have on the edge when it shows you the frame.

* Dialogs that use transparency usually look best without a
caption or a border. In the resource editor for your dialog,
uncheck “title bar” and set “border” to “none.”

How it works

It overrides CWnd::OnEraseBackground() to draw the bitmap as the background.
It overrides CWnd::OnCtlColor() to make static controls transparent.
It makes parts of the window transparent by combining many small window regions. (look at CBitmapDialog::MakeWindowRgn())
It allows you to drag anywhere to move by making Windows think that you clicked on the caption (even if there is no caption bar).

Downloads

Download demo project (include Release build) – 100 Kb

Download source – 5 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read