關於制作透明控件遇到的問題。(小弟問的問題太多﹐沒分啦﹐實在是對不住)(65分)

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.
小弟寫的很菜﹐還有許多功能要加強﹐但寫到這儿就卡殼啦。
 
我在Delphi 3下试你的程序。
Brush.Style := bsClear没问题!
 
我在D5下测试过,无论是bsClear还是bsSolid都运行通过!!!!
 
哦﹐有一點小錯誤﹐我在Setting方法中加上了
Brush.Style:=bsClear;
Pen.Style:=psClear;
而因為我測試的時候將它們改成註釋了﹐貼上來的時候就刪掉了。因為加上這兩個畫上去時
才沒有框線和填充色﹐這個在表單上布置的時候沒有任何問題﹐但一運行程序就出問題(是
已經生成控件﹐另開一個新專案﹐加入此控件運行時就會出現上述問題)。也就是這兩上地
方出錯。而像ShadowHeight等屬性﹐若我不在
property ShadowHeight:integer read FShadowHeight write SetShadowHeight default 4
中加上default 4也會出錯﹐請問這是什么原因造成的﹐應該怎么解決﹖謝謝﹗
 
Brush.Style := bsClear是绝对没有问题的。
 
大蝦們﹕能不能告訴一下你們的Email﹐我將這個源代碼發過來給你們幫忙修改一下好不好。
我現在加了一些功能在上面﹐但還是在編譯的時候出問題﹐你們看看有什么地方要做修改﹐
或者是再加上一點功能更好。我實在是沒法子﹐今天忙了一天了﹐還是沒有解決。
 
實際上在Label中有個Transparent屬性﹐你將它改為True﹐就可以實現透明了。我有一個
類似的例子﹐就是繼承Label寫的透明控件(自己寫著玩的哈)﹐你要是感興趣﹐我可以發給
你。不過﹐寫得很菜哈。
 
zhangkan﹐我試過啦。用Transparent的確是最好的辦法﹐我又用Lable繼承重寫了一個﹐沒
出現問題啦。首先謝謝你﹐另外﹐你若能寄給我例子的話﹐更加感謝﹗zhkfy@21cn.com。
接受答案啦。
 
顶部