[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Drawing text transparently on a canvas

Posted on 2003-12-05
8
Medium Priority
?
908 Views
Last Modified: 2012-05-04
Hallo there,

I'm doing a lot of drawing and writing to a canvas, using the methods of the canvas. In some cases however I want to write text on top of other text. I've tried using drawtext API setting the brush.style := bsClear for the font of the canvas, but with no success. IS there something special I have to do before it will work ?

Best regards
NetGeek

0
Comment
Question by:NetGeek
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9880911
invisible text?
0
 

Author Comment

by:NetGeek
ID: 9880947
not invisible, just transparent. As it is now the area taken up by the second text i write in the same spot completely covers the first text. I want to be able to se the letters from the first text "shining" through the spaces between the letters in the second text, like when you place a tlabel on a form and then a scond tlabel on the same spot with transparent set to true. When you move the second label aorund over the first one, you can see the first labe "behind" it.

NetGeek
0
 
LVL 1

Expert Comment

by:renkotk
ID: 9880948
How about:
  Canvas.Brush.Style := bsClear;
  Rct := Rect(0,0, w, h);
  DrawText(Canvas.Handle,     PChar(MyString), Length(MyString),     Rct,     DT_WORDBREAK);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:odissey1
ID: 9880949
Hi NetGeek,

I also wonder what do you want to achieve?

odissey1
0
 

Author Comment

by:NetGeek
ID: 9880968
I'm already setting font name,size,style and color, and brush style to bsClear and brush.color as well. the falgs i'm using are
 DT_SINGLELINE or DT_NOPREFIX or DT_LEFT. I'm controlling the output just liuke i want to, except forthe transparency

NetGeek
0
 
LVL 1

Expert Comment

by:renkotk
ID: 9880974
Btw heres an example, where I do write text on top of other text:

--------------------
uFrmStrCmp.pas
--------------------

unit uFrmStrCmp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls;

type
  TfrmStrCmp = class(TForm)
    Image: TImage;
    TrackBarHor: TTrackBar;
    TrackBarVer: TTrackBar;
    procedure FormCreate(Sender: TObject);
    procedure TrackBarVerChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FTmpBmp: TBitmap;
    FStringGreen: string;
    FStringRed: string;
    procedure SetStringGreen(const Value: string);
    procedure SetStringRed(const Value: string);
    procedure ImageUpdate;
    { Private declarations }
  public
    property StringRed: string read FStringRed write SetStringRed;
    property StringGreen: string read FStringGreen write SetStringGreen;
  end;

var
  frmStrCmp: TfrmStrCmp;

implementation

{$R *.dfm}

procedure TfrmStrCmp.ImageUpdate;
var
  Rct: TRect;
begin
  FTmpBmp.Height := Image.Height div 2;
  FTmpBmp.Width := Image.Width div 2;
  FTmpBmp.Canvas.Brush.Style := bsSolid;
  FTmpBmp.Canvas.FillRect(Rect(0,0, FTmpBmp.width, FTmpBmp.height));
  FTmpBmp.Canvas.Brush.Style := bsClear;
  FTmpBmp.Canvas.Font.Color := clGreen;
  Rct := Rect(0,0, FTmpBmp.Width-TrackBarHor.Position, FTmpBmp.Height-TrackBarVer.Position);
  DrawText(FTmpBmp.Canvas.Handle,
     PChar(FStringGreen), Length(FStringGreen),
     Rct,
     DT_WORDBREAK);

  FTmpBmp.Canvas.Font.Color := clRed;
  Rct := Rect(TrackBarHor.Position, TrackBarVer.Position, FTmpBmp.Width, FTmpBmp.Height);
  DrawText(FTmpBmp.Canvas.Handle,
     PChar(FStringRed), Length(FStringRed),
     Rct,
     DT_WORDBREAK);


  Image.Canvas.StretchDraw(Rect(0,0,Image.Width, Image.Height), FTmpBmp);
end;

procedure TfrmStrCmp.SetStringGreen(const Value: string);
begin
  FStringGreen := Value;
  ImageUpdate;
end;


procedure TfrmStrCmp.SetStringRed(const Value: string);
begin
  FStringRed := Value;
  ImageUpdate;
end;

procedure TfrmStrCmp.FormCreate(Sender: TObject);
begin
  FStringGreen := 'Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.';
  FStringRed := 'Bla bla bla bla bli bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bli.';

  FTmpBmp := TBitmap.Create;
end;

procedure TfrmStrCmp.TrackBarVerChange(Sender: TObject);
begin
  ImageUpdate;
end;

procedure TfrmStrCmp.FormDestroy(Sender: TObject);
begin
  FTmpBmp.Free;
end;


end.


--------------------
uFrmStrCmp.dfm
--------------------

object frmStrCmp: TfrmStrCmp
  Left = 519
  Top = 248
  BorderStyle = bsDialog
  Caption = 'frmStrCmp'
  ClientHeight = 375
  ClientWidth = 567
  Color = clWhite
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Image: TImage
    Left = 40
    Top = 33
    Width = 526
    Height = 342
  end
  object TrackBarHor: TTrackBar
    Left = 40
    Top = 0
    Width = 121
    Height = 33
    Orientation = trHorizontal
    Frequency = 1
    Position = 0
    SelEnd = 0
    SelStart = 0
    TabOrder = 0
    TickMarks = tmBottomRight
    TickStyle = tsNone
    OnChange = TrackBarVerChange
  end
  object TrackBarVer: TTrackBar
    Left = 0
    Top = 33
    Width = 25
    Height = 112
    Orientation = trVertical
    Frequency = 1
    Position = 0
    SelEnd = 0
    SelStart = 0
    TabOrder = 1
    TickMarks = tmBottomRight
    TickStyle = tsNone
    OnChange = TrackBarVerChange
  end
end
0
 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 9881244
Hi,

Brush.Color should be set before Brush.Style := bsClear. Otherwise bsClear doesn't work:

...
  with YourCanvas do begin
    Brush.Color := clWhite; // if you swap these two lines the text won't be transparent
    Brush.Style := bsClear;
    Font.Color := clGreen;
    DrawText(...
...

Regards, Geo
0
 

Author Comment

by:NetGeek
ID: 9881306
sorry renkotk, nice effort but geobul hit it right on the mark

Thank you all
Netgeek
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question