2009. augusztus 1., szombat
How to mirror text horizontally or vertically on a TPaintBox
Problem/Question/Abstract:
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:
procedure TForm1.PaintBox1Paint(Sender: TObject);
const
test = 'Hello world';
var
bmp: TBitmap;
cv: TCanvas;
ext: TSize;
r: TRect;
begin
cv := (Sender as TPaintbox).canvas;
ext := cv.TextExtent(test);
bmp := TBitmap.Create;
try
bmp.Width := ext.cx;
bmp.Height := ext.cy;
bmp.Canvas.Brush := cv.Brush;
bmp.Canvas.Font := cv.Font;
bmp.Canvas.FillRect(bmp.canvas.cliprect);
bmp.Canvas.TextOut(0, 0, test);
{draw text in normal orientation}
cv.Draw(0, 0, bmp);
r := Rect(ext.cx, 0, 0, ext.cy);
OffsetRect(r, 0, ext.cy);
{draw text horizontally mirrored}
cv.CopyRect(r, bmp.canvas, bmp.canvas.ClipRect);
r := Rect(0, ext.cy, ext.cx, 0);
OffsetRect(r, 0, 2 * ext.cy);
{draw text vertically mirrored}
cv.CopyRect(r, bmp.canvas, bmp.canvas.ClipRect);
finally
bmp.Free
end;
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!
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése