MFC画文字DrawText,GetTextExtent,GetTextMetrics

// 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_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTextView

IMPLEMENT_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 commands
	ON_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/destruction

CTextView::CTextView()
{
	// TODO: add construction code here
	m_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 cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTextView drawing

void CTextView::OnDraw(CDC* pDC)
{
	CTextDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);	

	CString str("吴xxabc Hello World");
	pDC->TextOut(0,200,str);
}

/////////////////////////////////////////////////////////////////////////////
// CTextView printing

BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return 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 _DEBUG
void 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 handlers

int 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){			//Enter
		TEXTMETRIC mt;
		dc.GetTextMetrics(&mt);
		m_ptStart.y += mt.tmHeight;
		m_strLine.Empty();
	}
	else if(0x08 == nChar){		//BackSpace		
		COLORREF 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);
}

posted @ 2011-02-06 17:04  庚武  Views(2778)  Comments(0Edit  收藏  举报