Solved

Drawing text transparently on a canvas

Posted on 2003-12-05
8
904 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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