Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / MFC

Using Custom Controls in a Dialog Bar

Rate me:
Please Sign up or sign in to vote.
4.81/5 (25 votes)
9 Nov 20077 min read 213.6K   5.4K   94  
How to implement owner drawn or subclassed controls in a Dialog Bar
// NewButton.cpp : implementation file
//

#include "stdafx.h"
#include "DerivedDB.h"
#include "NewButton.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNewButton

CNewButton::CNewButton()
{
	m_bOverControl = FALSE;
    m_nTimerID     = 1;
}

CNewButton::~CNewButton()
{
}


BEGIN_MESSAGE_MAP(CNewButton, CButton)
	//{{AFX_MSG_MAP(CNewButton)
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewButton message handlers

void CNewButton::OnMouseMove(UINT nFlags, CPoint point) 
{
   if (!m_bOverControl)                    // Cursor has just moved over control
    {
        TRACE0("Entering control\n");

        m_bOverControl = TRUE;              // Set flag telling us the mouse is in
        Invalidate();                       // Force a redraw

        SetTimer(m_nTimerID, 100, NULL);    // Keep checking back every 1/10 sec
    }
	
	CButton::OnMouseMove(nFlags, point);
}

void CNewButton::OnTimer(UINT nIDEvent) 
{
    // Where is the mouse?
    CPoint p(GetMessagePos());
    ScreenToClient(&p);

    // Get the bounds of the control (just the client area)
    CRect rect;
    GetClientRect(rect);

    // Check the mouse is inside the control
    if (!rect.PtInRect(p))
    {
        TRACE0("Leaving control\n");

        // if not then stop looking...
        m_bOverControl = FALSE;
        KillTimer(m_nTimerID);

        // ...and redraw the control
        Invalidate();
    }
	
	CButton::OnTimer(nIDEvent);
}

void CNewButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rect = lpDrawItemStruct->rcItem;
    UINT state = lpDrawItemStruct->itemState;

    CString strText;
/*  GetWindowText(strText);	<<<<<< gets text from button assigned in editor
								   We don't need this as we are assigning custom
								   text a few lines down */

    // draw the control edges (DrawFrameControl is handy!)
    if (state & ODS_SELECTED)
        pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_FLAT | DFCS_PUSHED);
    else
        pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_FLAT);

    // Deflate the drawing rect by the size of the button's edges
    rect.DeflateRect( CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
    
    // Fill the interior color if necessary
    if (m_bOverControl)
	{
		strText = "Yippee! Click Me Now!";
        pDC->FillSolidRect(rect, RGB(234, 230, 221)); // off-white
	}
	else
	{
		strText = "Cool Subclassed Button";
	//	pDC->FillSolidRect(rect, RGB(255, 255, 0)); //yellow
	}

    // Draw the text
    if (!strText.IsEmpty())
    {
        CSize Extent = pDC->GetTextExtent(strText);
        CPoint pt( rect.CenterPoint().x - Extent.cx/2, 
        rect.CenterPoint().y - Extent.cy/2 );

        if (state & ODS_SELECTED) 
            pt.Offset(1,1);

        int nMode = pDC->SetBkMode(TRANSPARENT);

        if (state & ODS_DISABLED)
            pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
        else
            pDC->TextOut(pt.x, pt.y, strText);

        pDC->SetBkMode(nMode);
    }
	
}

void CNewButton::PreSubclassWindow() 
{
	CButton::PreSubclassWindow();

	ModifyStyle(0, BS_OWNERDRAW);	// make the button owner drawn
}

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
United States United States
I have no biography, but then, I don't have an obituary yet either -- Thank God!!

Comments and Discussions