fontandtextexamappview.cpp

来自「国内著名嵌入式培训机构内部资料,内含一些实例代码,包括技术专题书籍」· C++ 代码 · 共 174 行

CPP
174
字号
/*
 ============================================================================
 Name		: FontAndTextExamAppView.cpp
 Author	  : Hou maoqing
 Copyright   : Copyright (c) Hou maoqing 2008
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <eikenv.h>
#include "FontAndTextExamAppView.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFontAndTextExamAppView* CFontAndTextExamAppView::NewL(const TRect& aRect)
	{
	CFontAndTextExamAppView* self = CFontAndTextExamAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFontAndTextExamAppView* CFontAndTextExamAppView::NewLC(const TRect& aRect)
	{
	CFontAndTextExamAppView* self = new ( ELeave ) CFontAndTextExamAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFontAndTextExamAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

	// Set the windows size
	SetRect(aRect);

	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::CFontAndTextExamAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CFontAndTextExamAppView::CFontAndTextExamAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::~CFontAndTextExamAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CFontAndTextExamAppView::~CFontAndTextExamAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CFontAndTextExamAppView::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	// Gets the control's extent
	TRect drawRect(Rect());

	// Clears the screen
	gc.Clear(drawRect);

	//获取和使用字体
	//const CFont* pFont=iEikonEnv->DenseFont();
	const CFont* pFont = CEikonEnv::Static()->DenseFont();
	gc.UseFont(pFont);
	
	//正常绘制
	TBuf<80> bufHello;
	bufHello.Append(_L("The Text is Drawn in Draw()"));
	gc.DrawText(bufHello,TPoint(10,30));
	
	//设置效果
	//gc.SetPenColor(KRgbBlue);
	gc.SetPenColor(TRgb(0,0,255));
	gc.SetUnderlineStyle(EUnderlineOn);
	gc.SetStrikethroughStyle(EStrikethroughOn);
	gc.SetUnderlineStyle(EUnderlineOff);
	gc.SetStrikethroughStyle(EStrikethroughOff);
	bufHello.Copy(_L("The Text is Drawn in Draw(), this is a long text"));
	gc.DrawText(bufHello,TPoint(10,60));
	
	//竖向绘制
	gc.DrawText(bufHello,TPoint(50,150));
	gc.DrawTextVertical(bufHello,TPoint(50,150),ETrue);
	gc.DrawTextVertical(bufHello,TPoint(50,150),EFalse);
	
	//换行绘制文本
	TPoint ptPos(10,90);
	TInt nLineHeight=pFont->FontMaxHeight();
	TInt nClientWidth=Rect().Width();
	
	TInt nLineCount=0;
	TInt nLineWidth=0;
	TInt nLineStartPos=0;
	TInt nLineCharCount=0;
	
	for(TInt i=0;i<bufHello.Length();i++)
		{
		TChar ch=bufHello[i];
		TInt nCharWidth=pFont->CharWidthInPixels(ch);
		if(nLineWidth+nCharWidth>nClientWidth)
			{
			TDes& des=bufHello;
			TPtrC ptrLine=des.Mid(nLineStartPos,nLineCharCount);
			
			ptPos.iY+=nLineHeight*nLineCount;
			gc.DrawText(ptrLine,ptPos);
			
			nLineCount++;
			nLineStartPos=i;
			nLineCharCount=1;
			nLineWidth=nCharWidth;			
			}
		else
			{
			nLineWidth+=nCharWidth;
			nLineCharCount+=1;
			}
		}
	if(nLineCharCount>0)
		{
		TDes& des=bufHello;
		TPtrC ptrLine=des.Mid(nLineStartPos,nLineCharCount);
		
		ptPos.iY+=nLineHeight*nLineCount;
		gc.DrawText(ptrLine,ptPos);
		}
	
	}

// -----------------------------------------------------------------------------
// CFontAndTextExamAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CFontAndTextExamAppView::SizeChanged()
	{
	DrawNow();
	}
// End of File

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?