Lee Jenkins wrote 26 lines
Hi all,
I'm having a problem with drawing text on an image using the DrawText
function where the area around the text masked out. Please see the
attached image.
This same routine works perfectly in Delphi. Maybe the
Canvas.Brush.Style := bsClear is not working?
procedure TMyImage.DrawCaption;
var
lRect: TRect;
begin
// setup rect
lRect.Left := 10;
lRect.Top := 10;
lRect.Right := Width -10;
lRect.Bottom := Height -10;
//Canvas.Pen.Style := psClear; // <-- tried this as well
Canvas.Font.Assign(FButtonFont);
Canvas.Brush.Style := bsClear;
DrawText(Canvas.Handle, PChar(FCaption), -1, lRect,
DT_WORDBREAK or DT_VCENTER or DT_CENTER or DT_END_ELLIPSIS);
end;
Here is the problem that I was having with:
procedure TDTImageButton.HandleMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
lBmp: TBitmap;
begin
if Button = mbLeft then
begin
lBmp := FImageManager.ImageByName(FUpImageName);
Picture.Bitmap.Assign(lBmp);
DrawCaption;
end;
end;
Notice the line:
Picture.Bitmap.Assign(lBmp);
Changing it to
Canvas.Draw(0,0,lBmp);
Fixed the problem. Odd because it works in Delphi, but the .Canvas.Draw methods doesn't seem to work well in delphi so I ended with this:
procedure TDTImageButton.HandleMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
lBmp: TBitmap;
begin
if Button = mbLeft then
begin
lBmp := FImageManager.ImageByName(FUpImageName);
{$IFDEF fpc}
Canvas.Draw(0,0,lBmp);
{$ELSE}
picture.Bitmap.Assign(lBmp);
{$endif}
DrawCaption;
end;
end;
procedure TDTImageButton.DrawCaption;
var
lRect: TRect;
lBmp: TBitmap;
begin
// setup rect
lRect.Left := 10;
lRect.Top := 10;
lRect.Right := Width -10;
lRect.Bottom := Height -10;
Canvas.Brush.Style := bsClear;
Canvas.Font.Assign(FButtonFont);
DrawText(Picture.Bitmap.Canvas.Handle, PChar(FCaption), -1, lRect,
DT_WORDBREAK or DT_CENTER);
end;
This works well and both methods are fast!
Warm Regards,
Lee
"When my company started out, we were really, really, really, really small.
Now...we're just really small."