?
Solved

Drawing text transparently on a canvas

Posted on 2003-12-05
8
Medium Priority
?
907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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