Using TZoomFactor and MGraphicsDeviceMap

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.

Setting up a 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
// Set zoom factor at 200%

Twips to pixels conversions using a zoom factor

  1. Define a rectangle in twips

  2. Use the device map’s conversion function TwipsToPixels() to convert the rectangles co-ordinates into pixels

  3. 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);

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.

Drawing text using a zoom factor

This fragment of code shows how to draw text using a zoom factor.

  1. Create a font specification (in this case for a 10 point Times New Roman font)

  2. 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.

  3. Use this device font to draw the text using CGraphicsContext::DrawText()

  4. 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;
// use it for this graphics context
gc.DrawText(_L("some example text"),TPoint(20,20));
// discard and release font

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.

Zooming bitmaps

You can draw a bitmap to the graphics device, scaled according to the current zoom factor.

  1. Define a rectangle in twips

  2. Use the device map’s conversion function TwipsToPixels() to convert the rectangle's co-ordinates into pixels according to the current zoom factor.

  3. 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);
// draw the bitmap, stretched into the rectangle
SystemGc.DrawBitmap(bitmapRectInPixels, iBitmap);