⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawing_hellocontrol.cpp

📁 《SymbianOSC手机应用开发》源码
💻 CPP
字号:
// Drawing_HelloControl.cpp
// ----------------------------
//
// Copyright (c) 2002 Symbian Ltd.  All rights reserved.
//

////////////////////////////////////////////////////////////////////////
//
// Source file for the implementation of the 
// application CExampleHelloControl class
//
////////////////////////////////////////////////////////////////////////

#include "Drawing.h"
#include <GulUtil.h>


CExampleHelloControl* CExampleHelloControl::NewL(const CCoeControl& aContainer, const TRect& aRect)
	{
	CExampleHelloControl* self=new(ELeave) CExampleHelloControl;
	CleanupStack::PushL(self);
	self->ConstructL(aContainer, aRect);
	CleanupStack::Pop();
	return self;
	}

void CExampleHelloControl::ConstructL(const CCoeControl& aContainer, const TRect& aRect)
	{
	SetContainerWindowL(aContainer);
	SetRect(aRect);
	iView=CExampleHelloView::NewL();
	iText=iEikonEnv->AllocReadResourceL(R_EXAMPLE_TEXT_HELLO_WORLD);
	iView->SetTextL(*iText);
	iZoomFactor.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
	SetZoomAndDeviceDependentFontL(1000);
	ActivateL();
	}
	


TInt CExampleHelloControl::GetZoom() const
	{
	return iZoomFactor.ZoomFactor();
	}

//
//	Called on class construction and as a result of zooming in or out. 
//	As well as setting the zoom factor, the function allocates a device-dependent
//  font for the "Hello World" text.
//	This is an appropriate location for the font allocation as it is expected to
//	change when the user zooms in or out.
//	The font allocation could also be performed in Draw() or CExampleHelloControl::DrawL(), which
//	is called by Draw(). However, Draw() is not a leave function and it implements a
//  virtual function in the parent class and cannot be altered. Therefore, in order to
//  be able to allocate a font a trap harness would have to be used, resulting in a call
//  from Draw() something like the following, with the font allocation taking place in
//  DrawL(): TRAPD(err, iView -> DrawL(iZoomFactor, gc, rect));
//  Using a trap here is undesirable practice (although it would allow all the
//  device-independent code to be contained in the CExampleHelloView class).
//
void CExampleHelloControl::SetZoomAndDeviceDependentFontL(TInt aZoomFactor)
	{
	iZoomFactor.SetZoomFactor(aZoomFactor);

	//Allocate a device dependent font for drawing.
	iZoomFactor.ReleaseFont(iFont);
	iFont=NULL; // This is in case the function leaves with the LeaveIfError() below.
				// In this case iFont would still have the old value when the class
				// destructor is called, and in the destructor the zoom factor will
				// attempt to release the same font again, causing the application
				// to crash. 
    _LIT(fontName, "SwissA");
    TFontSpec fontSpec(fontName, 240); // font size is 12 point 
	fontSpec.iFontStyle=TFontStyle(EPostureUpright, EStrokeWeightBold, EPrintPosNormal);
	User::LeaveIfError(iZoomFactor.GetNearestFontInTwips(iFont, fontSpec));
	}

void CExampleHelloControl::SetZoomInL()
	{
	TInt zoom=GetZoom();
	zoom=
		zoom < 500 ?  500 :
		zoom < 1000 ? 1000 : 
		zoom < 1500 ? 1500 :
		zoom < 2000 ? 2000 :
		500;//if you keep zooming in the font and rectangle size becomes small again.
	SetZoomAndDeviceDependentFontL(zoom);
	}

void CExampleHelloControl::SetZoomOutL()
	{
	TInt zoom=GetZoom();
	zoom=
		zoom > 2000 ? 2000 :
		zoom > 1500 ? 1500 :
		zoom > 1000 ? 1000 :
		zoom > 500 ? 500 :
		2000;//if you keep zooming out the font and rectangle size becomes large again.
	SetZoomAndDeviceDependentFontL(zoom);
	}


void CExampleHelloControl::SetFullRedraw(TBool aFullRedraw)
	{
	iView->SetFullRedraw(aFullRedraw);
	}

CExampleHelloControl::~CExampleHelloControl()
	{
	delete iText;
	delete iView;
	iZoomFactor.ReleaseFont(iFont);	//releases the last font to be used
	}
//
// Draws a rectangle in a corner of the screen with a white border, then calls
// DrawInRect() to fill the rectangle
//
void CExampleHelloControl::Draw(const TRect& /*aRect*/) const
	{
	CWindowGc& gc=SystemGc();
	TRect rect=Rect();
	rect.Shrink(10,10);
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.SetBrushColor(KRgbWhite);
	DrawUtils::DrawBetweenRects(gc, Rect(), rect); 
	gc.SetPenStyle(CGraphicsContext::ESolidPen);
	gc.SetPenColor(KRgbBlack);
	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
	gc.DrawRect(rect);
	rect.Shrink(1,1);
	iView->DrawInRect(iZoomFactor, gc, rect, iFont);
	}


⌨️ 快捷键说明

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