Creating the Monitor Custom Control

Environment: VC6 Win95/NT3.51 or later

In an attempt to imitate the famous monitor of the MS Windows Display properties dialog box, I’ve built this custom control to provide the functionality of representing the screen display with a bitmap image background.

The control is implemented as a CMonitor class publicly derived from CWnd, in an MFC regular Dll project, but of course the class can be extracted—together with the relevant resources—to be directly included in your MFC exe project. Also, an MFC dialog-based project was built dependent on the basic DLL project, to test and demonstrate the behaviour of the monitor custom control.

By using the VC++ ZoomIn tool, I was able to capture the monitor image from the MS Windows Display properties dialog box. The image was included in the project together with an identical monochrome bitmap of the monitor to act as a mask for laying the monitor transparently against any background.

The monitor behaviour, as demonstrated by the TestMonitor Dialog box, can be summarized as:

  • Laying itself transparently against any background. This can be shown by changing the combobox background selection and also by moving the monitor against the selected background, although the normal practice is using the monitor control in a plain color dialog box without being moved.
  • Transparency here is meant to expose the selected background through the monitor display area when the background image is not occupying the full display area, and of course to naturally display the monitor, just as an icon.
  • The main purpose of the control is to display a background image, centered, stretched, or tiled, within the monitor control display area that represents the screen display. On creation, the control is able to detect the screen dimensions by using the API function GetSystemMetrics() with the SM_CXSCREEN and SM_CYSCREEN parameters. Those dimensions are stored as data members and the user is given the ability to change them, which affects the displayed image that adjusts its size to match the new dimensions.
// SOURCE CODE HINTS

// a public static member function used for registering the
// control window class

static BOOL CMonitor::RegisterWndClass(HINSTANCE hInstance);

// a global function defined and used for transparently
// displaying the monitor

extern "C" __declspec(dllexport)
BOOL MaskImage(HDC hDC, int nXDest, int nYDest, int nWidth,
               int nHeight, HBITMAP hbmImage, int nXSrc,
               int nYSrc, HBITMAP hbmMask);

// the global control window procedure function

LRESULT CALLBACK AFX_EXPORT
CMonitorWndProc(HWND hWnd, UINT message, WPARAM wParam,
                                         LPARAM lParam);

// a set of user defined Windows control messages to
// communicate with the control

#define MM_SETIMAGE              WM_USER + 0
#define MM_GETIMAGE              WM_USER + 1
#define MM_SETDISPLAYSTYLE       WM_USER + 2
#define MM_GETDISPLAYSTYLE       WM_USER + 3
#define MM_SETREF                WM_USER + 4
#define MM_GETREF                WM_USER + 5

// a dummy function is used to be called from the client program
// to ensure the dll loading

extern "C" __declspec(dllexport)
void MonitorDllEntry() {}        // dummy function

I hope that this code can be useful. I’ll appreciate any comments.

Downloads

Download demo project – 16 Kb
Download source – 56 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read