Author: Tomas Rutkauskas
How to mirror text horizontally or vertically on a TPaintBox
Answer:
This is actually not quite straightforward. The best way to do that is to first
paint the text onto an off-screen bitmap and then paint that bitmap on screen using
some weird coordinate manipulations. Drop a TPaintbox on the screen and connect a
method to its OnPaint handler. Change the handler to the code below to see how this
works:
1
2 procedure TForm1.PaintBox1Paint(Sender: TObject);
3 const
4 test = 'Hello world';
5 var
6 bmp: TBitmap;
7 cv: TCanvas;
8 ext: TSize;
9 r: TRect;
10 begin
11 cv := (Sender as TPaintbox).canvas;
12 ext := cv.TextExtent(test);
13 bmp := TBitmap.Create;
14 try
15 bmp.Width := ext.cx;
16 bmp.Height := ext.cy;
17 bmp.Canvas.Brush := cv.Brush;
18 bmp.Canvas.Font := cv.Font;
19 bmp.Canvas.FillRect(bmp.canvas.cliprect);
20 bmp.Canvas.TextOut(0, 0, test);
21 {draw text in normal orientation}
22 cv.Draw(0, 0, bmp);
23 r := Rect(ext.cx, 0, 0, ext.cy);
24 OffsetRect(r, 0, ext.cy);
25 {draw text horizontally mirrored}
26 cv.CopyRect(r, bmp.canvas, bmp.canvas.ClipRect);
27 r := Rect(0, ext.cy, ext.cx, 0);
28 OffsetRect(r, 0, 2 * ext.cy);
29 {draw text vertically mirrored}
30 cv.CopyRect(r, bmp.canvas, bmp.canvas.ClipRect);
31 finally
32 bmp.Free
33 end;
34 end;
The key here is to set up the target rectangle for CopyRect with left and right or top and bottom switched. Be warned, there is a slight potential that this code will cause acute indigestion for some video drivers!
|