文本编程

响应键盘输入的信息,相当于简单记事本:

建立工程h-1

向CH-1view类添加

先添加CPoint m_ptOrigin

CString m_strInput变量;

添加函数ON_Create()

代码如下:

int CH_1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CView::OnCreate(lpCreateStruct) == -1)
  return -1;
  CClientDC dc(this);
 CFont fn;
 fn.CreatePointFont(200,"华文行楷");
 dc.SelectObject(&fn);
 TEXTMETRIC tm;
 dc.GetTextMetrics(&tm);
 CreateSolidCaret(tm.tmAveCharWidth /8,tm.tmHeight);
 ShowCaret();
 // TODO: Add your specialized creation code here
 
 return 0;
}

添加OnChar()

代码如下:

void CH_1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
 // TODO: Add your message handler code here and/or call default
 CClientDC dc(this);
  CFont fn;
 fn.CreatePointFont(200,"华文行楷");
 CFont *pold=dc.SelectObject(&fn);
 TEXTMETRIC tm;
 dc.GetTextMetrics(&tm);

 if(0x0d==nChar)
 {
  m_ptOrigin.x=0;
  m_ptOrigin.y+=tm.tmHeight;
 
  m_strInput.Empty();
 }
 else if(0x08==nChar)
 {
 COLORREF oldClr;
  oldClr=dc.SetTextColor(dc.GetBkColor());//将字体颜色设为背景色
 
  dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strInput);//输出
   m_strInput=m_strInput.Left(m_strInput.GetLength()-1);//删除最后一个字符
   dc.SetTextColor(oldClr);
 
  
 
 
  
 }
 else
 {
  m_strInput+=nChar;
 

 }

   CSize sz=dc.GetTextExtent(m_strInput);
  CPoint pt;
  pt.x=m_ptOrigin.x+sz.cx;
  pt.y=m_ptOrigin.y;
  SetCaretPos(pt);
 dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strInput);
  dc.SelectObject(pold);
 CView::OnChar(nChar, nRepCnt, nFlags);}
其他功能还好,就是退格符不如人意:

出现了多个光标,待解决!

 

 

你可能感兴趣的:(文本编程)