Solved

Drawing text transparently on a canvas

Posted on 2003-12-05
8
899 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
 
LVL 2

Expert Comment

by:odissey1
ID: 9880949
Hi NetGeek,

I also wonder what do you want to achieve?

odissey1
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 50 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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 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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now