功能:实现文字的输入!要求,回车在下一行输入,而BackSpace则删除一个字符。
void CTest31View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
if(0x0d == nChar)
{
m_str.Empty();
m_point.y += tm.tmHeight;
}
else if(0x08==nChar)
{
COLORREF clr = dc.SetTextColor(dc.GetBkColor());
dc.TextOut(m_point.x,m_point.y,m_str);
m_str = m_str.Left(m_str.GetLength()-1);
dc.SetTextColor(clr);
}
else
{
m_str += nChar;
}
CSize sz = dc.GetTextExtent(m_str);
CPoint pt;
pt.x = m_point.x + sz.cx;
pt.y = m_point.y;
SetCaretPos(pt);
dc.TextOut(m_point.x,m_point.y,m_str);
CView::OnChar(nChar, nRepCnt, nFlags);
}
int CTest31View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);
ShowCaret();
return 0;
} void CTest31View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCaretPos(point);
m_str.Empty();
m_point = point;
CView::OnLButtonDown(nFlags, point);
}
中心思想:m_point表示的是插入符的位置,m_str则表示的是输入的字符串!
对于回车和删除键,则用两个if来判断,具体的实现,参加代码部分,比较简单!!
posted on 2010-02-22 13:55
deercoder 阅读(479)
评论(0) 编辑 收藏 引用 所属分类:
MFC程序设计入门