D
dadabox
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大俠﹕
我想自己寫一個控件來練習一下。便決定寫一個透明標筌﹐就相當於Lable﹐但它是
透明的。我繼承Shape﹐然后加上一個Canvas類在里面。現在能夠實現透明了﹐且在表單
上我對其作修改﹐也能馬上表現出來。但我一按F9編譯﹐系統便提示讀錯誤。(property
does not exist)->Error read LimpidLabel1.brush.style﹐我若將其屬性改為bsSolid
就沒事﹐改為bsClear﹐就出錯。而且﹐若我改為bsSolid﹐運行正常﹐且出的效果也是透
明的﹐這是為什么﹐小弟實在不明白。哪位大俠能幫幫小弟呢﹖能跟我講一下這些原理嗎﹖
每次控件刷新除了通過paint外﹐還會有其他的方法重繪控件嗎﹖
源代碼如下﹕謝謝各位大俠幫忙﹐小弟能學到東西全靠你們啦﹗若你們有相關的源代碼最
好。
unit LimpidLabel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,extctrls;
type
TLimpidLabel = class(TShape)
private
FShadowWidth: integer;
FShadowHeight: integer;
FShadowColor: TColor;
FCaption: string;
StartX,StartY:integer;
FontColor:TColor;
procedure SetCaption(const Value: string);
procedure SetShadowColor(const Value: TColor);
procedure SetShadowHeight(const Value: integer);
procedure SetShadowWidth(const Value: integer);
{ Private declarations }
protected
procedure Paint;Override;
procedure Setting;
{ Protected declarations }
public
constructor Create(AOwner:TComponent);Override;
destructor Destroy;Override;
{ Public declarations }
published
property ShadowHeight:integer read FShadowHeight write SetShadowHeight default 4;
property ShadowWidth:integer read FShadowWidth write SetShadowWidth default 4;
property ShadowColor:TColor read FShadowColor write SetShadowColor default clBlack;
property Caption:string read FCaption write SetCaption;
property Font;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('new', [TLimpidLabel]);
end;
{ TLimpidLabel }
constructor TLimpidLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
StartX:=1;
StartY:=1;
ShadowHeight:=4;
ShadowWidth:=4;
ShadowColor:=clBlack;
Caption:=Name;
end;
destructor TLimpidLabel.Destroy;
begin
inherited Destroy;
end;
procedure TLimpidLabel.Paint;
begin
inherited;
Setting;
end;
procedure TLimpidLabel.SetCaption(const Value: string);
begin
FCaption := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowColor(const Value: TColor);
begin
FShadowColor := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowHeight(const Value: integer);
begin
FShadowHeight := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowWidth(const Value: integer);
begin
FShadowWidth := Value;
Refresh;
end;
procedure TLimpidLabel.Setting;
var lf: TLogFont;
tf:TFont;
begin
if Length(Caption)=0 then Caption:=Name;
FillChar(lf, SizeOf(lf), 0);
FontColor:=Font.Color;
Canvas.Font:=Font;
with Canvas do
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.handle,sizeof(lf),@lf);
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
Brush.Style:=bsClear;
Font.Color:=ShadowColor;
TextOut(StartX+ShadowWidth,StartY+ShadowHeight,Caption);
Font.Color:=FontColor;
TextOut(StartX,StartY,Caption);
Font.Color:=FontColor;
end;
end;
end.
小弟寫的很菜﹐還有許多功能要加強﹐但寫到這儿就卡殼啦。
我想自己寫一個控件來練習一下。便決定寫一個透明標筌﹐就相當於Lable﹐但它是
透明的。我繼承Shape﹐然后加上一個Canvas類在里面。現在能夠實現透明了﹐且在表單
上我對其作修改﹐也能馬上表現出來。但我一按F9編譯﹐系統便提示讀錯誤。(property
does not exist)->Error read LimpidLabel1.brush.style﹐我若將其屬性改為bsSolid
就沒事﹐改為bsClear﹐就出錯。而且﹐若我改為bsSolid﹐運行正常﹐且出的效果也是透
明的﹐這是為什么﹐小弟實在不明白。哪位大俠能幫幫小弟呢﹖能跟我講一下這些原理嗎﹖
每次控件刷新除了通過paint外﹐還會有其他的方法重繪控件嗎﹖
源代碼如下﹕謝謝各位大俠幫忙﹐小弟能學到東西全靠你們啦﹗若你們有相關的源代碼最
好。
unit LimpidLabel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,extctrls;
type
TLimpidLabel = class(TShape)
private
FShadowWidth: integer;
FShadowHeight: integer;
FShadowColor: TColor;
FCaption: string;
StartX,StartY:integer;
FontColor:TColor;
procedure SetCaption(const Value: string);
procedure SetShadowColor(const Value: TColor);
procedure SetShadowHeight(const Value: integer);
procedure SetShadowWidth(const Value: integer);
{ Private declarations }
protected
procedure Paint;Override;
procedure Setting;
{ Protected declarations }
public
constructor Create(AOwner:TComponent);Override;
destructor Destroy;Override;
{ Public declarations }
published
property ShadowHeight:integer read FShadowHeight write SetShadowHeight default 4;
property ShadowWidth:integer read FShadowWidth write SetShadowWidth default 4;
property ShadowColor:TColor read FShadowColor write SetShadowColor default clBlack;
property Caption:string read FCaption write SetCaption;
property Font;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('new', [TLimpidLabel]);
end;
{ TLimpidLabel }
constructor TLimpidLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
StartX:=1;
StartY:=1;
ShadowHeight:=4;
ShadowWidth:=4;
ShadowColor:=clBlack;
Caption:=Name;
end;
destructor TLimpidLabel.Destroy;
begin
inherited Destroy;
end;
procedure TLimpidLabel.Paint;
begin
inherited;
Setting;
end;
procedure TLimpidLabel.SetCaption(const Value: string);
begin
FCaption := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowColor(const Value: TColor);
begin
FShadowColor := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowHeight(const Value: integer);
begin
FShadowHeight := Value;
Refresh;
end;
procedure TLimpidLabel.SetShadowWidth(const Value: integer);
begin
FShadowWidth := Value;
Refresh;
end;
procedure TLimpidLabel.Setting;
var lf: TLogFont;
tf:TFont;
begin
if Length(Caption)=0 then Caption:=Name;
FillChar(lf, SizeOf(lf), 0);
FontColor:=Font.Color;
Canvas.Font:=Font;
with Canvas do
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.handle,sizeof(lf),@lf);
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
Brush.Style:=bsClear;
Font.Color:=ShadowColor;
TextOut(StartX+ShadowWidth,StartY+ShadowHeight,Caption);
Font.Color:=FontColor;
TextOut(StartX,StartY,Caption);
Font.Color:=FontColor;
end;
end;
end.
小弟寫的很菜﹐還有許多功能要加強﹐但寫到這儿就卡殼啦。