CheckFrame Control

.

Sometimes it can be handy to exclude a set of options based on the users choice. For instance if the user do not want to save a log file, then it should be signalled, to the user, which options are relevant.


fig.1 The checkframe control are disabled, and so is its the contained controls.


fig. 2 The check frame is enabled and the controls can be used.

 

The header file: CheckFrm.h

BOOL IsRectContainedInRect(CRect &rcChild, CRect &rcMother);

class CCheckFrame : public CObject
{
public:
	virtual void Enable(BOOL bEnable);
	void Set(CWnd *pParentWnd,UINT nFrmCtl);
	
	CWnd* m_pFrame; 
	CWnd* m_pDialog;
	CRect m_rFrm;
	CDWordArray m_adwWndHandles;
	
	
protected:
};

The impementation file: CheckFrm.cpp
The code are futher commented in the source files.

#include "stdafx.h"
#include "Checkfrm.h"

//////////////////////////////////////
// EnumChildWnds Callback
BOOL CALLBACK EnumChildWnds(HWND hwnd, CCheckFrame *pObj)
{
	CRect R;
	GetWindowRect(hwnd, &R);
	pObj->m_pDialog->ScreenToClient(&R);
	
	if (hwnd != pObj->m_pFrame->m_hWnd)
	{
		if (IsRectContainedInRect(R, pObj->m_rFrm))
			pObj->m_adwWndHandles.Add((DWORD)hwnd);
	}
	
	return TRUE;
}


//////////////////////////////////////
// Function name	: Set
void CCheckFrame::Set(CWnd *pParentWnd, UINT nFrmCtl)
{
	m_pDialog = pParentWnd;
	
	// Calculate the size of the rect that should contain the controls
	m_pFrame = m_pDialog->GetDlgItem(nFrmCtl);
	if (!m_pFrame)
	{
		AfxMessageBox("Unable to find frame control");
		return;
	}
	m_pFrame->GetWindowRect(&m_rFrm);
	m_pDialog->ScreenToClient(&m_rFrm);
	
	// Make sure the array is empty
	m_adwWndHandles.RemoveAll();
	
	// The (control) windows contained within the boundaries of the frame control
	// are enumerated and their associated window-handles collected in an array.
	EnumChildWindows(m_pDialog->m_hWnd, (WNDENUMPROC)EnumChildWnds, (LONG)(CCheckFrame *)this);
	
}

//////////////////////////////////////
// Function name	: CCheckFrame::Enable
void CCheckFrame::Enable(BOOL bEnable)
{
	for (int i=0 ; i<m_adwWndHandles.GetSize() ; i++)
		EnableWindow((HWND)m_adwWndHandles[i], bEnable);
}

//////////////////////////////////////
// Function name	: IsRectContainedInRect
BOOL IsRectContainedInRect(CRect &rcChild, CRect &rcMother)
{	
	if (rcMother.PtInRect(CPoint(rcChild.left, rcChild.top)) &&
	rcMother.PtInRect(CPoint(rcChild.left, rcChild.bottom)) &&
	rcMother.PtInRect(CPoint(rcChild.right, rcChild.top)) &&
	rcMother.PtInRect(CPoint(rcChild.right, rcChild.bottom)))
		return TRUE;
	
	return FALSE;
}

Known problems:
If the checkbox control it self is disabled when the progrma exits, try to move it some pixels away from the border of the frame control.

Download the sample project (31 kb)

Posted : March 12, 98



Comments

  • Very useful some suggestions

    Posted by Legacy on 06/23/2003 12:00am

    Originally posted by: Colin Undery

    Thanks for a useful piece of code, I've just put it in my project.
    
    The rectangle for the frame includes the original frame caption text, so I modified
    CCheckFrame::Set(...)
    {
    ...
    m_pDialog->ScreenToClient(&m_rFrm);
    m_rFrm.top+=5;
    ...
    }
    to avoid the check box disabling itself (depending how high up you put it).
    VC7 compiler generates warnings about truncating HWNDs into DWORDS, so I replaced
    CDWordArray m_adwWndHandles;
    with
    CArray<HWND,HWND>m_adwWndHandles;
    and the (DWORD) typecasting can be removed.

    finally EnumChildWindows(...) seems happier with
    (LPARAM)this
    for the third parameter.

    Thanks again for a useful piece of code

    Reply
  • A much easier way

    Posted by Legacy on 10/16/2002 12:00am

    Originally posted by: Yair Konfino

    1. using the resource editor , position your check box over
    the frame.
    2. set an id for the frame . something other then
    IDC_STATIC ( like IDC_STATIC_FRAME ).
    3. inside OnInitDialog() do the follwing :
    GetDlgItem(IDC_STATIC_FRAME)->
    ModifyStyle(0,WS_CLIPSIBLINGS);

    thats it ...
    Enjoy

    Yair

    Reply
  • Excellent Piece of work

    Posted by Legacy on 05/27/2002 12:00am

    Originally posted by: Joe Sonderegger

    This is excellent piece of work.
    I have used it also for disabling a complete dialog (by making the frame invisible.
    Thanx

    Joe Sonderegger

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds