Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / MFC

CRegionButton - A multidirectional button

Rate me:
Please Sign up or sign in to vote.
4.32/5 (22 votes)
17 May 20043 min read 117.9K   1.3K   58  
A class for making a button appear as though it has many regions.
#include "stdafx.h"

#include "RegionButton.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRegionButton

CRegionButton::CRegionButton()
{
    m_pRegion = NULL;
}

CRegionButton::~CRegionButton()
{
    delete [] m_pRegion;
    m_pRegion = NULL;
}


BEGIN_MESSAGE_MAP(CRegionButton, CBitmapButton)
	//{{AFX_MSG_MAP(CRegionButton)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegionButton message handlers

void CRegionButton::OnClicked() 
{
    CPoint  pt;
    DWORD   dwPos;


    // where was the mouse clicked
    dwPos = GetMessagePos();
    
    // get the X/Y coordinates
    pt.x = LOWORD(dwPos);
    pt.y = HIWORD(dwPos);

    // convert them to client coordinates
    ScreenToClient(&pt);    

    // see if the mouse click is within any of the regions
    for (UINT x = 0; x < m_uRegionCount; x++)
    {
        // if so, send a message to the parent
        if (m_pRegion[x].PtInRect(pt) != FALSE)
        {
            GetParent()->SendMessage(UDM_REGION_CLICKED, x);
            break;
        }
    }
}

//====================================================================

bool CRegionButton::CalculateRegions( const UINT uRegionCount )
{
    CRect   rect;
    bool    bReturn = false;


    SizeToContent();

    GetClientRect(rect);

    // at this point, we are only concerned with perfect squares
    if (4 == uRegionCount || 9 == uRegionCount)
    {
        m_uRegionCount = uRegionCount;
        
        if (NULL != m_pRegion)
            delete [] m_pRegion;
        
        m_pRegion = new CRect[m_uRegionCount];
        if (NULL != m_pRegion)
        {
            if (4 == m_uRegionCount)
            {
                m_pRegion[0].left   = 0;
                m_pRegion[0].top    = 0;
                m_pRegion[0].right  = (LONG) floor((double) rect.right * 0.5);
                m_pRegion[0].bottom = (LONG) floor((double) rect.bottom * 0.5);
    
                m_pRegion[1].left   = (LONG) ceil((double) rect.right * 0.5);
                m_pRegion[1].top    = 0;
                m_pRegion[1].right  = (LONG) rect.right;
                m_pRegion[1].bottom = (LONG) floor((double) rect.bottom * 0.5);
    
                m_pRegion[2].left   = 0;
                m_pRegion[2].top    = (LONG) ceil((double) rect.bottom * 0.5);
                m_pRegion[2].right  = (LONG) floor((double) rect.right * 0.5);
                m_pRegion[2].bottom = (LONG) rect.bottom;
    
                m_pRegion[3].left   = (LONG) ceil((double) rect.right * 0.5);
                m_pRegion[3].top    = (LONG) ceil((double) rect.bottom * 0.5);
                m_pRegion[3].right  = (LONG) rect.right;
                m_pRegion[3].bottom = (LONG) rect.bottom;
            }
            else if (9 == m_uRegionCount)
            {
                m_pRegion[0].left   = 0;
                m_pRegion[0].top    = 0;
                m_pRegion[0].right  = (LONG) floor((double) rect.right * (1.0 / 3.0));
                m_pRegion[0].bottom = (LONG) floor((double) rect.bottom * (1.0 / 3.0));

                m_pRegion[1].left   = (LONG) ceil((double) rect.right * (1.0 / 3.0));
                m_pRegion[1].top    = 0;
                m_pRegion[1].right  = (LONG) floor((double) rect.right * (2.0 / 3.0));
                m_pRegion[1].bottom = (LONG) floor((double) rect.bottom * (1.0 / 3.0));

                m_pRegion[2].left   = (LONG) ceil((double) rect.right * (2.0 / 3.0));
                m_pRegion[2].top    = 0;
                m_pRegion[2].right  = (LONG) rect.right;
                m_pRegion[2].bottom = (LONG) floor((double) rect.bottom * (1.0 / 3.0));

                m_pRegion[3].left   = 0;
                m_pRegion[3].top    = (LONG) ceil((double) rect.bottom * (1.0 / 3.0));
                m_pRegion[3].right  = (LONG) floor((double) rect.right * (1.0 / 3.0));
                m_pRegion[3].bottom = (LONG) floor((double) rect.bottom * (2.0 / 3.0));

                m_pRegion[4].left   = (LONG) ceil((double) rect.right * (1.0 / 3.0));
                m_pRegion[4].top    = (LONG) ceil((double) rect.bottom * (1.0 / 3.0));
                m_pRegion[4].right  = (LONG) floor((double) rect.right * (2.0 / 3.0));
                m_pRegion[4].bottom = (LONG) floor((double) rect.bottom * (2.0 / 3.0));

                m_pRegion[5].left   = (LONG) ceil((double) rect.right * (2.0 / 3.0));
                m_pRegion[5].top    = (LONG) ceil((double) rect.bottom * (1.0 / 3.0));
                m_pRegion[5].right  = (LONG) rect.right;
                m_pRegion[5].bottom = (LONG) floor((double) rect.bottom * (2.0 / 3.0));

                m_pRegion[6].left   = 0;
                m_pRegion[6].top    = (LONG) ceil((double) rect.bottom * (2.0 / 3.0));
                m_pRegion[6].right  = (LONG) floor((double) rect.right * (1.0 / 3.0));
                m_pRegion[6].bottom = (LONG) rect.bottom;

                m_pRegion[7].left   = (LONG) ceil((double) rect.right * (1.0 / 3.0));
                m_pRegion[7].top    = (LONG) ceil((double) rect.bottom * (2.0 / 3.0));
                m_pRegion[7].right  = (LONG) floor((double) rect.right * (2.0 / 3.0));
                m_pRegion[7].bottom = (LONG) rect.bottom;

                m_pRegion[8].left   = (LONG) ceil((double) rect.right * (2.0 / 3.0));
                m_pRegion[8].top    = (LONG) ceil((double) rect.bottom * (2.0 / 3.0));
                m_pRegion[8].right  = (LONG) rect.right;
                m_pRegion[8].bottom = (LONG) rect.bottom;
            }

            bReturn = true;
        }
        else
            AfxThrowMemoryException();
    }
    else
        ASSERT(FALSE);

    return (bReturn);
}

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
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

Comments and Discussions