Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2631128
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Windows平台

2013-08-04 17:17:28


点击(此处)折叠或打开

  1. // TextView.cpp : implementation of the CTextView class
  2. //

  3. #include "stdafx.h"
  4. #include "Text.h"

  5. #include "TextDoc.h"
  6. #include "TextView.h"

  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif

  12. /////////////////////////////////////////////////////////////////////////////
  13. // CTextView

  14. IMPLEMENT_DYNCREATE(CTextView, CView)

  15. BEGIN_MESSAGE_MAP(CTextView, CView)
  16.     //{{AFX_MSG_MAP(CTextView)
  17.     ON_WM_CREATE()
  18.     ON_WM_CHAR()
  19.     ON_WM_LBUTTONDOWN()
  20.     ON_WM_TIMER()
  21.     //}}AFX_MSG_MAP
  22.     // Standard printing commands
  23.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  24.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  25.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  26. END_MESSAGE_MAP()

  27. /////////////////////////////////////////////////////////////////////////////
  28. // CTextView construction/destruction

  29. CTextView::CTextView()
  30. {
  31.     // TODO: add construction code here
  32.     cs_textStr = "";
  33.     m_CPMouse.x = 0;
  34.     m_CPMouse.y = 0;
  35.     n_Width = 0;

  36.     textId = 0;
  37.     n_TextID_Array[0] = IDS_STRING_TEST;
  38.     n_TextID_Array[1] = IDS_STRING_TEST1;
  39.     n_TextID_Array[2] = IDS_STRING_TEST2;
  40.     n_TextID_Array[3] = IDS_STRING_TEST3;
  41. }

  42. CTextView::~CTextView()
  43. {
  44. }

  45. BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs)
  46. {
  47.     // TODO: Modify the Window class or styles here by modifying
  48.     // the CREATESTRUCT cs

  49.     return CView::PreCreateWindow(cs);
  50. }

  51. /////////////////////////////////////////////////////////////////////////////
  52. // CTextView drawing

  53. void CTextView::OnDraw(CDC* pDC)
  54. {
  55.     CTextDoc* pDoc = GetDocument();
  56.     ASSERT_VALID(pDoc);
  57.     // TODO: add draw code for native data here
  58. }

  59. /////////////////////////////////////////////////////////////////////////////
  60. // CTextView printing

  61. BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)
  62. {
  63.     // default preparation
  64.     return DoPreparePrinting(pInfo);
  65. }

  66. void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  67. {
  68.     // TODO: add extra initialization before printing
  69. }

  70. void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  71. {
  72.     // TODO: add cleanup after printing
  73. }

  74. /////////////////////////////////////////////////////////////////////////////
  75. // CTextView diagnostics

  76. #ifdef _DEBUG
  77. void CTextView::AssertValid() const
  78. {
  79.     CView::AssertValid();
  80. }

  81. void CTextView::Dump(CDumpContext& dc) const
  82. {
  83.     CView::Dump(dc);
  84. }

  85. CTextDoc* CTextView::GetDocument() // non-debug version is inline
  86. {
  87.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));
  88.     return (CTextDoc*)m_pDocument;
  89. }
  90. #endif //_DEBUG

  91. /////////////////////////////////////////////////////////////////////////////
  92. // CTextView message handlers

  93. int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  94. {
  95.     if (CView::OnCreate(lpCreateStruct) == -1)
  96.         return -1;
  97.     
  98.     // TODO: Add your specialized creation code here
  99.     ///< 创建插入符
  100.     
  101.     /*光标插入符*/
  102.     /*
  103.     TEXTMETRIC textMetric;
  104.     CClientDC ccdc(this);
  105.     if (!ccdc.GetTextMetrics(&textMetric))
  106.     {
  107.         return -1;
  108.     }
  109.     CreateSolidCaret(textMetric.tmAveCharWidth/8, textMetric.tmHeight);
  110.     ShowCaret();
  111.     */

  112.     /*位图插入符*/
  113.     bitmap.LoadBitmap(IDB_BITMAP_CARATE);
  114.     CreateCaret(&bitmap);
  115.     ShowCaret();
  116.     
  117.     SetTimer(TIMER_COLOR_IDEVENT, 100, NULL);

  118.     return 0;
  119. }

  120. void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  121. {
  122.     // TODO: Add your message handler code here and/or call default
  123.     TEXTMETRIC textMetric;
  124.     CClientDC ccdc(this);
  125.     if (!ccdc.GetTextMetrics(&textMetric))
  126.     {
  127.         return;
  128.     }
  129.     
  130.     if (0x0d == nChar)            ///< 换行
  131.     {
  132.         cs_textStr.Empty();
  133.         m_CPMouse.y += textMetric.tmHeight;
  134.     }
  135.     else if (0x08 == nChar)        ///< 退格
  136.     {
  137.         ///< 字体背景涂为白色,返回当前字体颜色
  138.         COLORREF clr = ccdc.SetTextColor(ccdc.GetBkColor());
  139.         ccdc.TextOut(m_CPMouse.x, m_CPMouse.y, cs_textStr);
  140.         ///< 截断字符串
  141.         cs_textStr = cs_textStr.Left(cs_textStr.GetLength() - 1);
  142.         ///< 设置回字体颜色
  143.         ccdc.SetTextColor(clr);
  144.     }
  145.     else
  146.     {
  147.         cs_textStr += nChar;
  148.     }

  149.     /*移动光标*/
  150.     CSize sz = ccdc.GetTextExtent(cs_textStr);
  151.     CPoint cpt;
  152.     cpt.x = m_CPMouse.x + sz.cx;
  153.     cpt.y = m_CPMouse.y;    
  154.     SetCaretPos(cpt);

  155.     ccdc.TextOut(m_CPMouse.x, m_CPMouse.y, cs_textStr);

  156.     CView::OnChar(nChar, nRepCnt, nFlags);
  157. }

  158. void CTextView::OnLButtonDown(UINT nFlags, CPoint point)
  159. {
  160.     // TODO: Add your message handler code here and/or call default
  161.     
  162.     /*移动光标*/
  163.     SetCaretPos(point);
  164.     /*清空字符缓存*/
  165.     cs_textStr.Empty();
  166.     /*重置光标位置*/
  167.     m_CPMouse = point;

  168.     CView::OnLButtonDown(nFlags, point);
  169. }

  170. void CTextView::OnTimer(UINT nIDEvent)
  171. {
  172.     // TODO: Add your message handler code here and/or call default
  173.     n_Width += 5;
  174.     
  175.     TEXTMETRIC textMetric;
  176.     CClientDC ccdc(this);
  177.     if (!ccdc.GetTextMetrics(&textMetric))
  178.     {
  179.         return;
  180.     }
  181.     CRect rect;
  182.     rect.left = 0;
  183.     rect.top = 200;
  184.     rect.right = n_Width;
  185.     rect.bottom = rect.top + textMetric.tmHeight;

  186.     ccdc.SetTextColor(RGB(255, 0, 0));
  187.     CString cst;
  188.     cst.LoadString(n_TextID_Array[textId]);
  189.     ccdc.DrawText(cst, &rect, DT_RIGHT);

  190.     CSize sz = ccdc.GetTextExtent(cst);
  191.     if (n_Width > sz.cx)
  192.     {
  193.         ///< 取下一个字符串
  194.         ++textId;    
  195.         if (4 == textId)
  196.         {
  197.             textId = 0;
  198.         }
  199.         
  200.         n_Width = 0;
  201.         ccdc.SetTextColor(RGB(0, 255, 0));
  202.         ccdc.TextOut(0, 200, cst);
  203.     }

  204.     CView::OnTimer(nIDEvent);
  205. }
    

     


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