Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / MFC

Encapsulating interacting controls

Rate me:
Please Sign up or sign in to vote.
4.32/5 (9 votes)
31 Aug 20035 min read 52.1K   1K   19  
How to get controls to interact with each other without help from their parent dialog box.
// CustomControl.cpp : implementation file
//

#include "stdafx.h"
#include "cc4.h"
#include "CustomControl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define CRH_CUSTOMCONTROL_CLASSNAME    _T("CRH_CustomControl")  // Window class name

/////////////////////////////////////////////////////////////////////////////
// CRH_CustomControl

CRH_CustomControl::CRH_CustomControl()
{
    RegisterWindowClass();
}

CRH_CustomControl::~CRH_CustomControl()
{
}


BEGIN_MESSAGE_MAP(CRH_CustomControl, CWnd)
	//{{AFX_MSG_MAP(CRH_CustomControl)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CRH_CustomControl message handlers

BOOL CRH_CustomControl::RegisterWindowClass()
{
    WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();

    if (!(::GetClassInfo(hInst, CRH_CUSTOMCONTROL_CLASSNAME, &wndcls)))
    {
        // otherwise we need to register a new class
        wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc      = ::DefWindowProc;
        wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
        wndcls.hInstance        = hInst;
        wndcls.hIcon            = NULL;
        wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
        wndcls.lpszMenuName     = NULL;
        wndcls.lpszClassName    = CRH_CUSTOMCONTROL_CLASSNAME;

        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Retired
United Kingdom United Kingdom
I've been programming computers since about 1968. I started at school with Algol 60 on an Elliott 803. From there I progressed through the Z80 and other microprocessors to the PC, DOS and Windows, Pascal, C and C++.

My other interests include astronomy and classical music. All of my contributions to Code Project have arisen from programs I've written in these areas.

Comments and Discussions