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

Adding mouse functionality to any control

Rate me:
Please Sign up or sign in to vote.
4.74/5 (14 votes)
19 Jan 20025 min read 84.8K   3.6K   44  
An article on adding mouse based functionality to any CWnd derived control
// Text.cpp : implementation file
//

#include "stdafx.h"
#include "mouse.h"
#include "Text.h"

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

/////////////////////////////////////////////////////////////////////////////
// CText

CText::CText()
{
}

CText::~CText()
{
}


BEGIN_MESSAGE_MAP(CText, CMouseAction)
	//{{AFX_MSG_MAP(CText)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CText message handlers

void CText::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	CRect m_rect;
	GetClientRect(&m_rect);

	CBrush StaticBrush;
	CPen StaticPen;
	StaticBrush.CreateStockObject(HOLLOW_BRUSH);
	StaticPen.CreatePen(PS_SOLID, 0, RGB(255, 0, 0));

	const char str = ' ';
	CSize size = dc.GetTextExtent(&str,1);
	CRect m_textrect = m_rect;

	CFont m_font;
	if (!(HFONT)m_font)
	{
		// first time init: create font
		LOGFONT lf;
		GetFont()->GetObject(sizeof(lf), &lf);
		m_font.CreateFontIndirect(&lf);
	}
	dc.SelectObject(&m_font);

	{
		int y = (m_rect.bottom / size.cy);
		{
			dc.SetBkMode(TRANSPARENT);
			dc.SetTextColor(RGB(255, 255, 255));
			if (m_bHover)
				dc.SetTextColor(RGB(255, 0, 0));
			dc.SelectObject(&StaticBrush);
			dc.SelectObject(&StaticPen);
			{
				m_textrect.bottom = size.cy;
				m_textrect.top = m_textrect.bottom-size.cy;
				CString szStr;
				GetWindowText(szStr);
				dc.ExtTextOut(0, m_textrect.top,ETO_CLIPPED,&m_textrect,szStr,NULL);
			}
		}
	}

	// Do not call CMouseAction::OnPaint() for painting messages
}

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 States United States
Programming using MFC and ATL for almost 12 years now. Currently studying Operating System implementation as well as Image processing. Previously worked on DSP and the use of FFT for audio application. Programmed using ADO, ODBC, ATL, COM, MFC for shell interfacing, databasing tasks, Internet items, and customization programs.

Comments and Discussions