作用:在image上写上背景为透明字符。
程序中:
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
procedure TForm1.Button1Click(Sender: TObject);
var
OldBkMode : integer;
begin
with image1.Canvas do begin
OldBkMode := SetBkMode(Handle, TRANSPARENT);
TextOut(10, 50, 'Transparent!');
SetBkMode(Handle, OldBkMode);
end;
end;
控件(省去的应该是无关大局的东东,也就一两句):
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ExtDlgs;
type
TMyImage = class(TImage)
private
{ Private declarations }
FString:string;
FTextX,FTextY:integer;
FTextOut:boolean;
FTextTransparent:boolean;
FTextFont:TFont;
FTextBackColor:TColor;
protected
{ Protected declarations }
procedure Setstring(value: string);
procedure SetTextX(value: Integer);
procedure SetTextY(value: Integer);
procedure SetTextOut(value: boolean);
procedure SetTextTransparent(value: boolean);
procedure SetTextFont(value: TFont);
procedure SetTextBackColor(value: TColor);
public
{ Public declarations }
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
procedure WriteString(a:boolean);
published
{ Published declarations }
property Text:String read FString Write Setstring ; //图片上显示的文字
property TextX:integer read FTextX Write SetTextX ; //文字输出的X轴位置
property TextY:integer read FTextY Write SetTextY ; //文字输出的Y轴位置
property TextOut:boolean read FTextOut Write setTextOut; //是否输出文字
property TextTransparent:boolean read FTextTransparent Write SetTextTransparent; //文字背景是否透明
……
procedure TMyImage.WriteString(a:boolean);
var
OldBkMode:integer;
begin
with self.Canvas do begin
if a then begin
Brush.Color:=FTextBackColor;
Font:=FTextFont;
OldBkMode:= SetBkMode(Handle, Transparent);
TextOut(FTextX,FTextY,FString);
SetBkMode(Handle, OldBkMode);
end
else begin
Brush.Color:=FTextBackColor;
Font:=FTextFont;
TextOut(FTextX,FTextY,FString);
end;
end;
end;
procedure TMyImage.Setstring(value: string);
begin
if FString<>value then FString:=value;
if FTextOut then begin
WriteString(FTextTransparent);
end;
end;
end.