TZoomFactor
and
MGraphicsDeviceMap
The example code shown here demonstrates 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.
TZoomFactor
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.
// Create zoom factor object
TZoomFactor devicemap(iCoeEnv->ScreenDevice());
// Set zoom factor at 1 to 1
devicemap.SetZoomFactor(TZoomFactor::EZoomOneToOne);
// Set zoom factor at 200%
devicemap.SetZoomFactor(TZoomFactor::EZoomOneToOne*2);
Define a rectangle in twips
Use the device maps 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);
The actual conversion that TwipsToPixels()
applies
is dependent on the device maps 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 maps 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);
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 maps 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);