文本编辑器<一>文字输出

1 创建单文档应用程序;

2 插入光标,为View类添加OnCreate函数,加入如下代码

	CClientDC dc(this);

	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);

	CreateSolidCaret( tm.tmAveCharWidth/2,tm.tmHeight );
	ShowCaret();

3 增加字符输入,为View类添加OnChar函数,加入如下代码

	CClientDC dc(this);

	LOGFONT log;
	GetObject( ::GetStockObject(DEFAULT_GUI_FONT),sizeof(log),&log );
	log.lfHeight = 20;
	log.lfWidth = 20;
	log.lfCharSet = GB2312_CHARSET;
	lstrcpy( log.lfFaceName,"黑体" );

	CFont font;
	font.CreateFontIndirect(&log);
	CFont *pOrgFont = dc.SelectObject(&font);

	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	if(nChar==0x0d)         /*回车*/
	{
		m_strText="";
		m_jumpOffPoint.x = 0;
		m_jumpOffPoint.y += tm.tmHeight;
	}
	else if(nChar==0x08)    /*删除*/
	{
		COLORREF color = dc.SetTextColor(dc.GetBkColor());
		dc.TextOut(m_jumpOffPoint.x,m_jumpOffPoint.y,m_strText);
		m_strText = m_strText.Left(m_strText.GetLength()-1);
		dc.SetTextColor(color);
	}
	else
	{
		m_strText += (wchar_t)nChar;
	}

	CSize size = dc.GetTextExtent(m_strText);
	CPoint point;
	point.x = m_jumpOffPoint.x+size.cx;
	point.y = m_jumpOffPoint.y;
	SetCaretPos(point);

	dc.TextOut(m_jumpOffPoint.x,m_jumpOffPoint.y,m_strText);

	dc.SelectObject(pOrgFont);


存在问题:

回车有残影,退格有残影

你可能感兴趣的:(文本编辑器<一>文字输出)