Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WTL

The Psychology of a WTL Project: Lowercase Cause (a typing program)

Rate me:
Please Sign up or sign in to vote.
4.99/5 (32 votes)
20 Apr 200524 min read 63.6K   2.7K   25  
A psychological journey into a project crafted from start to finish.
#include "StdAfx.h"
#include ".\texttypedctrl.h"

CTextTypedCtrl::CTextTypedCtrl(void)
{
}

LRESULT CTextTypedCtrl::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	CPaintDC dc(m_hWnd);	
	dc.SelectFont(settings::get_fixed_font());

	// find out how much of the text identical to the target text
	std::string good_text;
	for(int n = _current_text.size(); n >= 0; n--)
	{
		std::string search_text = _current_text.substr(0, n);
		if(_target_text.find(search_text) == 0)
		{
			good_text = search_text;
			break;
		}
	}
		
	CRect rect;
	GetClientRect(&rect);
	
	// paint our background
	// --------------------
	CPen pen;
	pen.CreatePen(PS_NULL, 1, RGB(0, 0, 0));

	CBrush brush;
	brush.CreateSolidBrush(_back_color);

	dc.SelectPen(pen);
	dc.SelectBrush(brush);
	dc.Rectangle(rect);


	// draw the text
	// -------------
	// adjust our rect for the text
	rect.InflateRect(-2, -2);

	dc.SetBkColor(_back_color);
		
	// determine if our color is within the red spectrum, if so
	// we want to use blue instead of red for errors
	if(GetRValue(_back_color) == 255 && GetGValue(_back_color) < 255)
		dc.SetTextColor(RGB(0, 0, 255));
	else
		dc.SetTextColor(RGB(255, 0, 0));

	// first pass draw all the text in the color which represents
	// incorrect
	dc.DrawText(_current_text.c_str(), _current_text.size(), &rect, 
		DT_NOPREFIX | DT_WORDBREAK | DT_EDITCONTROL | DT_EXPANDTABS);

	// now we overlay our text with the text that is good on top of it
	// turning the good black and the bad stays below which may become
	// uncovered if good_text is not _current_text
	dc.SetTextColor(RGB(0, 0, 0));
	dc.DrawText(good_text.c_str(), good_text.size(), &rect, 
		DT_NOPREFIX | DT_WORDBREAK | DT_EDITCONTROL | DT_EXPANDTABS);

	bHandled = TRUE;
	return 0;
}

LRESULT CTextTypedCtrl::OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	return 1;
}

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

Comments and Discussions