This topic provides examples that demonstrate the effects of different zoom factors on text and graphics. Example code displays some text, a rectangle, and a bitmap at various different zoom factors, with commentary text set at a fixed zoom factor.
The following fragment of code demonstrates how to set up a zoom factor object. The zoom factor is first set to 1-to-1 (no zoom) using the value TZoomFactor::EZoomOneToOne . Then it is shown how to set a zoom factor by multiplying or dividing EZoomOneToOne .
In actual applications, for example, one zoom factor could be used for the client area of an application which can be zoomed, and another for drawing menus and button-bars which should not be zoomed.
Define a rectangle in twips
Use the device map’s conversion function TwipsToPixels() to convert the rectangles co-ordinates into pixels
Draw the rectangle using DrawRect() in the graphics context gc
// set up example box in twips TRect boxInTwips(TPoint(0,0),TPoint(500,300)); // convert rectangle co-ordinates into pixels TRect boxInPixels = deviceMap->TwipsToPixels(boxInTwips); gc.DrawRect(boxInPixels);
Note : The actual conversion that TwipsToPixels() applies is dependent on the device map’s zoom factor setting, and the twips to pixels mapping of the graphics device which the device map is associated with.
This fragment of code shows how to draw text using a zoom factor.
Create a font specification (in this case for a 10 point Times New Roman font)
Find the nearest device font, using MGraphicsDeviceMap::GetNearestFontInTwips() , to that specified. This takes into account the map’s zoom factor and twips to pixel mapping.
Use this device font to draw the text using CGraphicsContext::DrawText()
Discard the font using CGraphicsContext::DiscardFont() and MGraphicsDeviceMap::ReleaseFont()
// set up absolute font-spec and text box for 200 twip Roman font TFontSpec fontSpec(_L("Times New Roman"),200); // find the nearest font to the specified one CFont* screenFont; deviceMap->GetNearestFontInTwips(screenFont,fontSpec); // use it for this graphics context gc.UseFont(screenFont); gc.DrawText(_L("some example text"),TPoint(20,20)); // discard and release font gc.DiscardFont(); deviceMap->ReleaseFont(screenFont);
Note : Due to the overhead of running GetNearestFontInTwips() , the caller would normally cache any fonts provided by it at the start of an application and after each change to the zoom factor, and only release them when the application is exited or the zoom factor changed.
You can draw a bitmap to the graphics device, scaled according to the current zoom factor.
Define a rectangle in twips
Use the device map’s conversion function TwipsToPixels() to convert the rectangle's co-ordinates into pixels according to the current zoom factor.
Draw the bitmap using DrawBitmap() stretched/compressed into the rectangle.
// set up rectangle for bitmap to be stretched into TRect bitmapRectInTwips(TPoint(0,0),TPoint(500,500)); TRect bitmapRectInPixels = iLeftMap->TwipsToPixels(bitmapRectInTwips); bitmapRectInPixels.iTl.iY+=125; bitmapRectInPixels.iBr.iY+=125; bitmapRectInPixels.iTl.iX+=100; bitmapRectInPixels.iBr.iX+=100; // draw the bitmap, stretched into the rectangle SystemGc.DrawBitmap(bitmapRectInPixels, iBitmap);
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License v1.0.