这节主要讲的是文本编程
1 // CTextView 绘制 2 3 void CTextView::OnDraw(CDC* pDC) 4 { 5 CTextDoc* pDoc = GetDocument(); 6 ASSERT_VALID(pDoc); 7 if (!pDoc) 8 return; 9 10 // TODO: 在此处为本机数据添加绘制代码 11 //CString str("南京熊猫电子装备有限公司"); 12 CString str; 13 str = "南京熊猫电子装备有限公司"; 14 pDC->TextOutA(50, 50, str); 15 16 CSize sz = pDC->GetTextExtent(str); 17 18 str.LoadString(IDS_XIONGMAO); 19 pDC->TextOutA(0, 200, str); 20 21 pDC->BeginPath(); 22 pDC->Rectangle(50, 50, 50+sz.cx, 50+sz.cy); 23 pDC->EndPath(); 24 pDC->SelectClipPath(RGN_DIFF); 25 //pDC->SelectClipPath(RGN_AND); 26 for(int i = 0; i < 300; i+=10) { 27 pDC->MoveTo(0, i); 28 pDC->LineTo(300, i); 29 pDC->MoveTo(i, 0); 30 pDC->LineTo(i, 300); 31 } 32 }
1 // CTextView 消息处理程序 2 3 int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) 4 { 5 if (CView::OnCreate(lpCreateStruct) == -1) 6 return -1; 7 8 // TODO: 在此添加您专用的创建代码 9 CClientDC dc(this); 10 TEXTMETRIC tm; 11 dc.GetTextMetrics(&tm); 12 // CreateSolidCaret(tm.tmAveCharWidth/8, tm.tmHeight); 13 14 bitmap.LoadBitmap(IDB_BITMAP1); 15 CreateCaret(&bitmap); 16 17 ShowCaret(); 18 19 SetTimer(1, 100, NULL); 20 return 0; 21 }
1 void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 2 { 3 // TODO: 在此添加消息处理程序代码和/或调用默认值 4 CClientDC dc(this); 5 CFont font; 6 font.CreatePointFont(300, "华文行楷", NULL); 7 CFont *pOldFont = dc.SelectObject(&font); 8 9 TEXTMETRIC tm; 10 dc.GetTextMetrics(&tm); 11 if (0x0d == nChar) { 12 m_strLine.Empty(); 13 m_ptOrigin.y += tm.tmHeight; 14 } 15 else if (0x08 == nChar) { 16 COLORREF clr = dc.SetTextColor(dc.GetBkColor()); 17 dc.TextOutA(m_ptOrigin.x, m_ptOrigin.y, m_strLine); 18 m_strLine = m_strLine.Left(m_strLine.GetLength()-1); 19 dc.SetTextColor(clr); 20 } 21 else { 22 m_strLine += (wchar_t)nChar; 23 } 24 CSize sz = dc.GetTextExtent(m_strLine); 25 26 CPoint pt; 27 pt.x = m_ptOrigin.x + sz.cx; 28 pt.y = m_ptOrigin.y; 29 30 SetCaretPos(pt); 31 dc.TextOutA(m_ptOrigin.x, m_ptOrigin.y, m_strLine); 32 33 dc.SelectObject(pOldFont); 34 35 CView::OnChar(nChar, nRepCnt, nFlags); 36 }
1 void CTextView::OnLButtonDown(UINT nFlags, CPoint point) 2 { 3 // TODO: 在此添加消息处理程序代码和/或调用默认值 4 SetCaretPos(point); 5 m_strLine.Empty(); 6 m_ptOrigin = point; 7 CView::OnLButtonDown(nFlags, point); 8 }
1 void CTextView::OnTimer(UINT_PTR nIDEvent) 2 { 3 // TODO: 在此添加消息处理程序代码和/或调用默认值 4 m_nWidth += 5; 5 6 CClientDC dc(this); 7 TEXTMETRIC tm; 8 dc.GetTextMetrics(&tm); 9 CRect rect; 10 rect.left = 0; 11 rect.top = 200; 12 rect.right = m_nWidth; 13 rect.bottom = rect.top + tm.tmHeight; 14 15 dc.SetTextColor(RGB(255, 0, 0)); 16 CString str; 17 str.LoadStringA(IDS_XIONGMAO); 18 dc.DrawText(str, rect, DT_LEFT); 19 20 rect.top = 150; 21 rect.bottom = rect.top + tm.tmHeight; 22 //dc.DrawText(str, rect, DT_RIGHT); 23 dc.DrawText(str, rect, DT_CENTER); 24 25 CSize sz = dc.GetTextExtent(str); 26 if (m_nWidth > sz.cx) { 27 m_nWidth = 0; 28 dc.SetTextColor(RGB(0, 255, 0)); 29 dc.TextOut(0, 200, str); 30 } 31 32 CView::OnTimer(nIDEvent); 33 }
时间: 2024-10-03 14:45:10