onchar

时间:2022-11-19 20:22:29

void CMfcView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)//Windows响应函数
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);//指向当前活动视图窗口
TEXTMETRIC tm;
CFont font;//创建字体
font.CreatePointFont(300,"黑体",NULL);//null用于表示从屏幕设别来完成这种转化 创建大小 字体类型
CFont *oldfront=dc.SelectObject(&font);
dc.GetTextMetrics(&tm);//用于获得当前视图中窗口的特定字符串的长度和宽度
if(0X0D==nChar)
{
MessageBox("回车是个系统bug!");
m_strline.Empty();//删除原来字符串储存的内容
m_orignlocation.y+=tm.tmHeight;//纵坐标进行移动位置
}
else if (0x08==nChar)//接受删除键
{
COLORREF clr=dc.SetTextColor(dc.GetBkColor());//获取背景色
dc.TextOut(m_orignlocation.x,m_orignlocation.y,m_strline);
m_strline=m_strline.Left(m_strline.GetLength()-1);
dc.SetTextColor(clr);//恢复背景色
}
else
{
m_strline+=nChar;
}
CSize sz=dc.GetTextExtent(m_strline);
pt.x=sz.cx+m_orignlocation.x;
pt.y=m_orignlocation.y;
SetCaretPos(pt);
dc.TextOut(m_orignlocation.x,m_orignlocation.y,m_strline);
dc.SelectObject(oldfront);
CView::OnChar(nChar, nRepCnt, nFlags);
}

相关文章