此节主要是创建插入符,并获取字体大小创建
void CTEXTView::OnDraw(CDC* pDC) { CTEXTDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; //CString str("this is test app"); CString str; str="this is test app"; pDC->TextOut(50,50,str); CSize sz=pDC->GetTextExtent(str); str.LoadString(IDS_XXX); pDC->TextOut(100,100,str); pDC->BeginPath(); pDC->Rectangle(50,50,50+sz.cx,50+sz.cy); pDC->EndPath(); pDC->SelectClipPath(RGN_DIFF); for(int i=0;i<300;i+=10) { pDC->MoveTo(0,i); pDC->LineTo(300,i); pDC->MoveTo(i,0); pDC->LineTo(i,300); } }
int CTEXTView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: 在此添加您专用的创建代码 CClientDC dc(this); TEXTMETRIC tm; dc.GetTextMetrics(&tm); //CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); bitmap.LoadBitmap(IDB_BITMAP1); CreateCaret(&bitmap); ShowCaret(); SetTimer(1,100,NULL); return 0; }
void CTEXTView::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 m_nWidth+=5; CClientDC dc(this); TEXTMETRIC tm; dc.GetTextMetrics(&tm); CRect rect; rect.left=0; rect.top=200; rect.right=m_nWidth; rect.bottom=rect.top+tm.tmHeight; dc.SetTextColor(RGB(255,0,0)); CString str; str.LoadStringA(IDS_XXX); dc.DrawText(str,rect,DT_LEFT); rect.top=150; rect.bottom=rect.top+tm.tmHeight; dc.DrawText(str,rect,DT_RIGHT); CSize sz=dc.GetTextExtent(str); if(m_nWidth>sz.cx) { m_nWidth=0; dc.SetTextColor(RGB(0,255,0)); dc.TextOutA(0,200,str); } CView::OnTimer(nIDEvent); }
void CTEXTView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CClientDC dc(this); CFont font; font.CreatePointFont(300,"华文行楷",NULL); CFont* pOldFont=dc.SelectObject(&font); TEXTMETRIC tm; dc.GetTextMetrics(&tm); if(0x0d==nChar) { m_strLine.Empty(); m_ptOrigin.y+=tm.tmHeight; } else if(0x08==nChar) { COLORREF clr=dc.SetTextColor(dc.GetBkColor()); dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine); m_strLine=m_strLine.Left(m_strLine.GetLength()-1); dc.SetTextColor(clr); } else { m_strLine+=(TCHAR)nChar; } CSize sz=dc.GetTextExtent(m_strLine); 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_strLine); dc.SelectObject(pOldFont); CView::OnChar(nChar, nRepCnt, nFlags); } void CTEXTView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 SetCaretPos(point); m_strLine.Empty(); m_ptOrigin=point; CView::OnLButtonDown(nFlags, point); }