Solved

Drawing text transparently on a canvas

Posted on 2003-12-05
8
902 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 58
control image tags in a string ? 12 131
Delphi and Access based Enumeration 9 64
Firemonkey allowing RTL on android 6 32
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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