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.
有兩個錯誤﹕
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.