自編組件出現﹕Class TImage Not Found的錯誤﹐急﹗怎么辦﹖(50分)

  • 主题发起人 主题发起人 fancy105
  • 开始时间 开始时间
F

fancy105

Unregistered / Unconfirmed
GUEST, unregistred user!
自編的一個按鈕﹐功能是根據不同的鼠標狀態顯示不同的圖像﹐達到動態的效果。
有兩個錯誤﹕
1.在Destroy中Free對象出錯﹐去掉則通過編譯﹐見代碼中的注解﹔
2.用代碼建立的方式使用該組件﹐則一切正常﹐而在設計時加入則出錯﹐
去掉后錯誤也存在。錯誤的提示是﹕Class TImage no found.

unit BmpBtn;

interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;

type
TBmpBtn = class(TGraphicControl)
private
FForeBmp: TGraphic;
FOverBmp: TGraphic;
FDownBmp: TGraphic;
ImgFore,ImgOver,ImgDown: TImage;

FMouseIn: Boolean;
FIsMouseDown: Boolean;
FFrameRect: TRect;
FOffset: Integer;
FBorder: Boolean;
FTransparent: Boolean;
procedure SetForeBmp(const Value: TGraphic);
procedure SetOverBmp(const Value: TGraphic);

procedure MouseEnter(var Message:TMessage);
message CM_MOUSEENTER;
procedure MouseExit(var Message:TMessage);
message CM_MOUSELEAVE;
procedure LeftMouseDown(var Message:TMessage);
message WM_LBUTTONDOWN;
procedure LeftMouseUp(var Message:TMessage);
message WM_LBUTTONUP;
procedure DReSize(var Message:TMessage);
message WM_SIZE;
procedure SetDownBmp(const Value: TGraphic);
procedure DrawOver;
Procedure DrawDown;
procedure SetFrameRect(const Value: TRect);
procedure SetOffset(const Value: Integer);
procedure SetBorder(const Value: Boolean);
procedure SetTransparent(const Value: Boolean);
{ Private declarations }
protected
procedure Paint;override;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
property ForeBmp:TGraphic read FForeBmp write SetForeBmp;
property OverBmp:TGraphic read FOverBmp write SetOverBmp;
property DownBmp:TGraphic read FDownBmp write SetDownBmp;
property MouseIn:Boolean read FMouseIn;
property IsMouseDown:Boolean read FIsMouseDown;
property FrameRect:TRect read FFrameRect write SetFrameRect;
property Offset:Integer read FOffset write SetOffset;
property Border:Boolean read FBorder write SetBorder;
property Transparent:Boolean read FTransparent write SetTransparent;
property OnClick;
end;

procedure Register;

implementation

{ TBmpBtn }

constructor TBmpBtn.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
Parent:=AOwner as TWinControl;
self.Width:=100;
Self.Height:=60;
FOffset:=2;
FFrameRect.Left:=FOffset;
FFrameRect.Top:=FOffset;
FFrameRect.Right:=Width-FOffset;
FFrameRect.Bottom:=Height-FOffset;
FMouseIn:=false;
FIsMouseDown:=False;
FBorder:=False;
FTransparent:=False;
FForeBmp:=TBitmap.Create;
FOverBmp:=TBitmap.Create;
FDownBmp:=TBitmap.Create;
ImgFore:=TImage.Create(AOwner);
ImgOver:=TImage.Create(AOwner);
ImgDown:=TImage.Create(AOwner);
end;

destructor TBmpBtn.Destroy;
begin
inherited;
end;

procedure TBmpBtn.DrawOver;
begin
canvas.MoveTo(0,0);
canvas.pen.Color:=clWhite;
Canvas.LineTo(Width-1,0);
canvas.pen.Color:=clBtnShadow;
canvas.LineTo(Width-1,Height-1);
canvas.LineTo(0,Height-1);
canvas.pen.Color:=clWhite;
Canvas.LineTo(0,0);
end;

procedure TBmpBtn.DrawDown;
begin
canvas.MoveTo(0,0);
canvas.pen.Color:=clBtnShadow;
Canvas.LineTo(Width-1,0);
canvas.pen.Color:=clWhite;
canvas.LineTo(Width-1,Height-1);
canvas.LineTo(0,Height-1);
canvas.pen.Color:=clBtnShadow;
Canvas.LineTo(0,0);
end;

procedure TBmpBtn.LeftMouseDown(var Message: TMessage);
begin
Inherited;
if Assigned(FDownBmp) then
begin
FIsMouseDown:=True;
Canvas.StretchDraw(FFrameRect,FDownBmp);
self.Invalidate;
end;
if FBorder then
DrawDown;
end;

procedure TBmpBtn.LeftMouseUp(var Message: TMessage);
begin
Inherited;
if Assigned(FForeBmp) then
begin
FIsMouseDown:=False;
Canvas.StretchDraw(FFrameRect,FForeBmp);
self.Invalidate;
end;
end;

procedure TBmpBtn.MouseEnter(var Message: TMessage);
begin
Inherited;
if Assigned(FOverBmp) then
begin
FMouseIn:=True;
Canvas.StretchDraw(FFrameRect,FOverBmp);
self.Invalidate;
end;
if FBorder then
DrawOver;
end;

procedure TBmpBtn.MouseExit(var Message: TMessage);
begin
Inherited;
if Assigned(FForeBmp) then
begin
FMouseIn:=False;
Canvas.StretchDraw(FFrameRect,FForeBmp);
self.Invalidate;
end;
end;

procedure TBmpBtn.Paint;
begin
inherited;
if FMouseIn then
begin
if FIsMouseDown then
begin
if Assigned(FDownBmp) then
Canvas.StretchDraw(FFrameRect,FDownBmp);
if FBorder then
DrawDown;
end
else
begin
if Assigned(FOverBmp) then
Canvas.StretchDraw(FFrameRect,FOverBmp);
if FBorder then
DrawOver;
end;
end
else
begin
Canvas.Brush.Style:=bsClear;
if Assigned(FForeBmp) then
Canvas.StretchDraw(FFrameRect,FForeBmp);
end;
end;

procedure TBmpBtn.SetDownBmp(const Value: TGraphic);
begin
FDownBmp.Assign(Value);
self.Invalidate;
end;

procedure TBmpBtn.SetForeBmp(const Value: TGraphic);
begin
FForeBmp.Assign(Value);
self.Invalidate;
end;

procedure Register;
begin
RegisterComponents('Standard', [TBmpBtn]);
end;

procedure TBmpBtn.SetOverBmp(const Value: TGraphic);
begin
FOverBmp.Assign(Value);
self.Invalidate;
end;

procedure TBmpBtn.SetFrameRect(const Value: TRect);
begin
FFrameRect := Value;
end;

procedure TBmpBtn.SetOffset(const Value: Integer);
begin
if (Value>0) and (Value<Width) and (Value<Height) then
begin
FOffset := Value;
FFrameRect.Left:=FOffset;
FFrameRect.Top:=FOffset;
FFrameRect.Right:=Width-FOffset;
FFrameRect.Bottom:=Height-FOffset;
end;
self.Invalidate;
end;

procedure TBmpBtn.SetBorder(const Value: Boolean);
begin
FBorder := Value;
end;

procedure TBmpBtn.DReSize(var Message: TMessage);
begin
Inherited;
self.Invalidate;
end;

procedure TBmpBtn.SetTransparent(const Value: Boolean);
begin
FTransparent := Value;

if Assigned(FForeBmp) then
begin
ImgFore.Picture.Graphic:=FForeBmp;
ImgFore.Transparent:=FTransparent;
FForeBmp:=ImgFore.Picture.Graphic;
end;

if Assigned(FOverBmp) then
begin
ImgOver.Picture.Graphic:=FOverBmp;
ImgOver.Transparent:=FTransparent;
FOverBmp:=ImgOver.Picture.Graphic;
end;

if Assigned(FDownBmp) then
begin
ImgDown.Picture.Graphic:=FDownBmp;
ImgDown.Transparent:=FTransparent;
FDownBmp:=ImgDown.Picture.Graphic;
end;

self.Invalidate;
end;

end.
 
你将Create方法中的这句去掉试试:Parent:=AOwner as TWinControl;
这一句其实是多余的,没有必要写这句。
 
SetTransparent(const Value: Boolean);
这个过程不要算了,好象没什么作用。
如果这个过程不要的话,那么ImgFore,ImgOver,ImgDown: TImage;这句也可以不要,
在Create的时候就不用建立IMAGE了,这样就不会出错。
 
1.Parent:=AOwner as TWinControl;這句不要的話,則出現
控件沒有定義父的錯誤,況且問題不在可能在這里.
2.我建立Image的目的是產生透明的位圖,除非有別的方法,不過
問題可能在這里,因為之前不增加Transparent屬性時沒有這個錯誤.
但沒有這個屬性不大有意義,因為按鈕不可能總是方的.
 
ImgFore:=TImage.Create(AOwner);
ImgOver:=TImage.Create(AOwner);
ImgDown:=TImage.Create(AOwner);
//////////////////////////////////
Img… := TImage.Create(Self);
 
哦,我明白了,搞錯對象了.我試試!
 
多人接受答案了。
 
后退
顶部