Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4253465
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-08 10:41:34

工程代码: page135.rar  

  1. 需要键盘捕获消息函数
     WM_CHAR
  2. 左键 插入符 坐标
     OnLButtonDown


在 CTextView 视类中,添加一个 CString 类型的成员变量: m_stLine,专门用来存储输入的字符串 private
    并在 CTextView 构造函数中 初始化 为 空

在CTextView 视类中,添加一个CPoit类型的成员变量用于保存坐标值, m_ptOrigin ,私有。

2 个私有变量



左键 响应函数
  1. void CTextView::OnLButtonDown(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     
  5.     SetCaretPos(point); //把插入符移动到鼠标左键单击点处
  6.     m_strLine.Empty();  //每次移动插入符, 将字符串 清空
  7.     m_ptOrigin = point;// 保存当前坐标

  8.     CView::OnLButtonDown(nFlags, point);
  9. }


  1. void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     
  5.     CClientDC dc(this);
  6.     TEXTMETRIC tm;
  7.     dc.GetTextMetrics(&tm);
  8.     if(nChar == 0x0d)
  9.     {
  10.         m_strLine.Empty();
  11.         m_ptOrigin.y +=tm.tmHeight;
  12.     }
  13.     else if(nChar == 0x08)
  14.     {
  15.         COLORREF clr = dc.SetTextColor(dc.GetBkColor());
  16.         dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);
  17.         m_strLine = m_strLine.Left(m_strLine.GetLength()-1);
  18.         dc.SetTextColor(clr);
  19.     }
  20.     else
  21.     {
  22.         m_strLine += nChar;
  23.     }

  24.     CSize sz = dc.GetTextExtent(m_strLine);
  25.     CPoint pt;
  26.     pt.x = m_ptOrigin.x + sz.cx;
  27.     pt.y = m_ptOrigin.y + sz.cy;
  28.     SetCaretPos(pt);

  29.     dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);
  30.     CView::OnChar(nChar, nRepCnt, nFlags);
  31. }





阅读(736) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~