Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / MFC

Screen Designer Classes

Rate me:
Please Sign up or sign in to vote.
4.93/5 (39 votes)
7 Mar 20045 min read 147.4K   3.9K   109  
Screen Designer Classes for MFC applications
// Copyright � 2002 Oink!
// FILE NAME	: DynControl.cpp
// DESCRIPTION	: Class control dynamically screen created controls
//
// PROGRAMMER	DATE		PATCH	DESCRIPTION
// ============	===========	=======	===============================================

#include "stdafx.h"
#include "DynControl.h"
#include "resource.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDynControl::CDynControl(CWnd* pWnd, int nControlType, int nID, BOOL bDynamic)
{
	// construct a new control

	m_bRectInit = FALSE;

	// font for labels
	LOGFONT logfontDefaultFont;
	GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), 
        &logfontDefaultFont); 
	m_fontDefault.CreateFontIndirect(&logfontDefaultFont);

	// initialise
	m_pParentWnd = pWnd;
	m_nControlType = nControlType; 
	m_nID = nID;
	m_bDynamic = bDynamic;

	m_bVisible = TRUE;
	
	m_strValue = "";
	m_nFieldID = -1;
	m_Group = FALSE;
	m_nTabOrder = 0;
	m_nRadioValue = 0;
	m_strComboValues = "";

	m_pWnd = NULL;

}

CDynControl::~CDynControl()
{
	if (m_pWnd != NULL)
		delete m_pWnd;

}

void CDynControl::EnableWindow(BOOL bEnable)
{
	// Enable Controls

	// different controls are enabled differently
	// so you may need to enhance this in order to put
	// do your own bits!	
	if (bEnable == FALSE)
	{
		m_pParentWnd->GetDlgItem(m_nID)->EnableWindow(FALSE);
	}

	if (bEnable == TRUE)
	{
		m_pParentWnd->GetDlgItem(m_nID)->EnableWindow(TRUE);
	}
}

CRect CDynControl::GetRect()
{
	// Get the rectangle of the control
	m_pParentWnd->GetDlgItem(m_nID)->GetWindowRect(m_Rect);
	m_pParentWnd->ScreenToClient(&m_Rect);
	m_bRectInit = TRUE;
	return m_Rect;
}

void CDynControl::AddControl()
{
	// add a controls where the co-ordinates
	// are already known, calls addcontrol with 0 position
	// and then positions control

	CPoint p(0,0);
	AddControl(&p);
	CRect r(m_x, m_y, m_cx, m_cy);
	MapDialogRect(m_pParentWnd->m_hWnd,r);
	m_pWnd->SetWindowPos(NULL, r.left, r.top, r.right, r.bottom,0);
}

void CDynControl::AddControl(CPoint* ppoint)
{
	// Add new contols to the screen

	if (m_nControlType == ID_DYNBUTTONEDIT) // EDIT
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE;		

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + 21; // of the control

		CEdit* pControl = new CEdit;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(WS_EX_CLIENTEDGE,"EDIT","",nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,
			   NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONEDITMULTI) // MULTI LINE EDIT
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE;		
		nStyle = nStyle | ES_MULTILINE | WS_VSCROLL | ES_WANTRETURN;

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + (21 * 3); // of the control

		CEdit* pControl = new CEdit;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(WS_EX_CLIENTEDGE,"EDIT","",nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,
			   NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONLABEL) // LABEL
	{
		int nStyle = WS_CHILD | WS_VISIBLE;
		if (m_strValue.IsEmpty())
			m_strValue = "Label";

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + 21; // of the control

		CStatic* pControl = new CStatic;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,"STATIC",m_strValue,nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONCOMBO) // COMBO
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL;

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + (21 * 5); // of the control

		CComboBox* pControl = new CComboBox;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,"COMBOBOX","",nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	

		int nLoop = 0;
		// get the valid list of data from the database
		if (!m_strComboValues.IsEmpty())
		{
			CString strValues = m_strComboValues;
			while (!strValues.IsEmpty())
			{
				CString strItem = strValues.Mid(0,strValues.Find("~"));
				strValues = strValues.Mid(strValues.Find("~") + 1);
				pControl->AddString(strItem);
				nLoop++;
				if (nLoop > 1000)
				{
					// opps! gone into an infinite loop
					// what did you put between the entries it should be a ~
					ASSERT(FALSE);
				}
			}
		}
	}

	if (m_nControlType == ID_DYNBUTTONGROUP) // GROUPBOX
	{
		int nStyle = WS_CHILD | WS_VISIBLE | BS_GROUPBOX;
		if (m_strValue.IsEmpty())
			m_strValue = "Label";

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + (21 * 5); // of the control 5 lines by default

		CButton* pControl = new CButton;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,"BUTTON",m_strValue,nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONCHECK) // CHECK
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_AUTOCHECKBOX;
		if (m_strValue.IsEmpty())
			m_strValue = "Check";

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + 21; // of the control

		CButton* pControl = new CButton;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,"BUTTON",m_strValue,nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONDATE) // DATE
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER | DTS_SHOWNONE;		

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 110; // width and height
		rectPos.bottom = rectPos.top + 21; // of the control

		CDateTimeCtrl* pControl = new CDateTimeCtrl;
		
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,DATETIMEPICK_CLASS,"DateTime",nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);	
	}

	if (m_nControlType == ID_DYNBUTTONRADIO) // RADIO
	{
		int nStyle = WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_AUTORADIOBUTTON;

		if (m_Group)
			nStyle = nStyle | WS_GROUP;

		if (m_strValue.IsEmpty())
			m_strValue = "Radio";

		CRect rectPos;
		rectPos.left = ppoint->x;
		rectPos.top = ppoint->y;
		rectPos.right = rectPos.left + 88; // width and height
		rectPos.bottom = rectPos.top + 21; // of the control

		CButton* pControl = new CButton;
		m_pWnd = pControl;
		m_hWnd = pControl->GetSafeHwnd();
		pControl->CreateEx(0,"BUTTON",m_strValue,nStyle,
			   rectPos.left,
			   rectPos.top,
			   rectPos.Width(),
			   rectPos.Height(),
			   m_pParentWnd->m_hWnd,NULL);
		pControl->SetDlgCtrlID(m_nID);
		pControl->SetFont(&m_fontDefault);
	}

	m_bVisible = TRUE;
}

void CDynControl::CopyRect(LPRECT rect)
{
	// copy the rectangle into the rect object

	m_Rect.CopyRect(rect);
}

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
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions