MFC畫文字DrawText,GetTextExtent,GetTextMetrics

來源:互聯網
上載者:User
// TextView.cpp : implementation of the CTextView class//#include "stdafx.h"#include "Text.h"#include "TextDoc.h"#include "TextView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CTextViewIMPLEMENT_DYNCREATE(CTextView, CView)BEGIN_MESSAGE_MAP(CTextView, CView)//{{AFX_MSG_MAP(CTextView)ON_WM_CREATE()ON_WM_CHAR()ON_WM_LBUTTONDBLCLK()ON_WM_TIMER()//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CTextView construction/destructionCTextView::CTextView(){// TODO: add construction code herem_strLine="";m_ptStart=CPoint(0,0);m_iWidth=0;}CTextView::~CTextView(){}BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs){// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CTextView drawingvoid CTextView::OnDraw(CDC* pDC){CTextDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);CString str("吳xxabc Hello World");pDC->TextOut(0,200,str);}/////////////////////////////////////////////////////////////////////////////// CTextView printingBOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn DoPreparePrinting(pInfo);}void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add extra initialization before printing}void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add cleanup after printing}/////////////////////////////////////////////////////////////////////////////// CTextView diagnostics#ifdef _DEBUGvoid CTextView::AssertValid() const{CView::AssertValid();}void CTextView::Dump(CDumpContext& dc) const{CView::Dump(dc);}CTextDoc* CTextView::GetDocument() // non-debug version is inline{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));return (CTextDoc*)m_pDocument;}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////// CTextView message handlersint CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CView::OnCreate(lpCreateStruct) == -1)return -1;CClientDC dc(this);TEXTMETRIC metric;dc.GetTextMetrics(&metric);//CreateSolidCaret(metric.tmAveCharWidth,metric.tmHeight);m_bmpCaret.LoadBitmap(IDB_WU);CreateCaret(&m_bmpCaret);ShowCaret();SetTimer(1,100,NULL);return 0;}void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {CFont font;font.CreatePointFont(300,"華文行楷",NULL);CClientDC dc(this);CFont *pOldFont = dc.SelectObject(&font);if(0x0d==nChar){//EnterTEXTMETRIC mt;dc.GetTextMetrics(&mt);m_ptStart.y += mt.tmHeight;m_strLine.Empty();}else if(0x08 == nChar){//BackSpaceCOLORREF colorOld = dc.SetTextColor( dc.GetBkColor() );dc.TextOut(m_ptStart.x,m_ptStart.y,m_strLine);m_strLine=m_strLine.Left( m_strLine.GetLength()-1 );dc.SetTextColor(colorOld);}else{m_strLine+=nChar;}dc.TextOut(m_ptStart.x,m_ptStart.y,m_strLine);CSize sz = dc.GetTextExtent(m_strLine);SetCaretPos(CPoint(m_ptStart.x+sz.cx,m_ptStart.y));dc.SelectObject( pOldFont );CView::OnChar(nChar, nRepCnt, nFlags);}void CTextView::OnLButtonDblClk(UINT nFlags, CPoint point) {SetCaretPos( m_ptStart=point );m_strLine.Empty();CView::OnLButtonDblClk(nFlags, point);}void CTextView::OnTimer(UINT nIDEvent) {if(nIDEvent==1){m_iWidth+=5;CClientDC dc(this);CString str("吳XXabc Hello World");CSize sz = dc.GetTextExtent(str);CRect rect(0,200,m_iWidth,200+sz.cy);dc.SetTextColor(RGB(0xff,0,0));dc.DrawText(str,&rect,DT_LEFT);}CView::OnTimer(nIDEvent);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.