令
令狐小侠
Unregistered / Unconfirmed
GUEST, unregistred user!
大侠们 帮忙 看看
unit HlBitbtn;
interface
uses
SysUtils, Classes, StdCtrls,controls,windows,messages,graphics;
type
THlBitbtn = class(TButton)
private
FBmp: TbitMap;
FCanvas : TCanvas;
procedure SetBmp(const Value: TbitMap);
{ Private declarations }
protected
procedure DrawItem(DrawItemStruct : TDrawItemStruct);
{ Protected declarations }
procedure CNMeasureItem(var Message : TWMMeasureItem);
message CN_MeasureItem;
procedure CNDrawItem(var Message: TWMDrawItem);
message CN_DRAWITEM;
procedure CMEnabledChanged(var Message: TMessage);
message CM_ENABLEDCHANGED;
procedure CreateParams(var Params: TCreateParams);
override;
public
Constructor Create(Owner : Tcomponent);override;
Destructor Destroy;override;
{ Public declarations }
published
property Bmp:TbitMap read FBmp write SetBmp;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('HL', [THlBitbtn]);
end;
{ THlBitbtn }
procedure THlBitbtn.CMEnabledChanged(var Message: TMessage);
begin
inherited;
Invalidate;
end;
procedure THlBitbtn.CNDrawItem(var Message: TWMDrawItem);
begin
DrawItem(message.DrawItemStruct^);
end;
procedure THlBitbtn.CNMeasureItem(var Message: TWMMeasureItem);
begin
message.MeasureItemStruct.itemWidth := width;
message.MeasureItemStruct.itemHeight := height;
end;
Constructor THlBitbtn.Create(Owner: Tcomponent);
begin
inherited Create(Owner);
Fbmp := TBitMap.Create;
fbmp.LoadFromFile('c:/a.bmp');
FCanvas := TCanvas.Create;
end;
procedure THlBitbtn.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(params);
Params.Style := Params.Style + BS_OWNERDRAW;
end;
destructor THlBitbtn.Destroy;
begin
Fbmp.Free;
FCanvas.Free;
inherited;
end;
procedure THlBitbtn.DrawItem(DrawItemStruct: TDrawItemStruct);
var
Rect,R : Trect;
begin
Rect := Fbmp.Canvas.ClipRect;
R := self.ClientRect;
Fcanvas.Handle := DrawItemStruct.hDC;
FCanvas.Brush.Color := clgray;
FCanvas.RoundRect (R.Left ,R.Top ,R.Right ,R.Bottom ,5,5);
Fcanvas.CopyRect(Rect,fbmp.Canvas,rect);
Fcanvas.Handle := 0;
end;
procedure THlBitbtn.SetBmp(const Value: TbitMap);
begin
FBmp := Value;
end;
end.
unit HlBitbtn;
interface
uses
SysUtils, Classes, StdCtrls,controls,windows,messages,graphics;
type
THlBitbtn = class(TButton)
private
FBmp: TbitMap;
FCanvas : TCanvas;
procedure SetBmp(const Value: TbitMap);
{ Private declarations }
protected
procedure DrawItem(DrawItemStruct : TDrawItemStruct);
{ Protected declarations }
procedure CNMeasureItem(var Message : TWMMeasureItem);
message CN_MeasureItem;
procedure CNDrawItem(var Message: TWMDrawItem);
message CN_DRAWITEM;
procedure CMEnabledChanged(var Message: TMessage);
message CM_ENABLEDCHANGED;
procedure CreateParams(var Params: TCreateParams);
override;
public
Constructor Create(Owner : Tcomponent);override;
Destructor Destroy;override;
{ Public declarations }
published
property Bmp:TbitMap read FBmp write SetBmp;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('HL', [THlBitbtn]);
end;
{ THlBitbtn }
procedure THlBitbtn.CMEnabledChanged(var Message: TMessage);
begin
inherited;
Invalidate;
end;
procedure THlBitbtn.CNDrawItem(var Message: TWMDrawItem);
begin
DrawItem(message.DrawItemStruct^);
end;
procedure THlBitbtn.CNMeasureItem(var Message: TWMMeasureItem);
begin
message.MeasureItemStruct.itemWidth := width;
message.MeasureItemStruct.itemHeight := height;
end;
Constructor THlBitbtn.Create(Owner: Tcomponent);
begin
inherited Create(Owner);
Fbmp := TBitMap.Create;
fbmp.LoadFromFile('c:/a.bmp');
FCanvas := TCanvas.Create;
end;
procedure THlBitbtn.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(params);
Params.Style := Params.Style + BS_OWNERDRAW;
end;
destructor THlBitbtn.Destroy;
begin
Fbmp.Free;
FCanvas.Free;
inherited;
end;
procedure THlBitbtn.DrawItem(DrawItemStruct: TDrawItemStruct);
var
Rect,R : Trect;
begin
Rect := Fbmp.Canvas.ClipRect;
R := self.ClientRect;
Fcanvas.Handle := DrawItemStruct.hDC;
FCanvas.Brush.Color := clgray;
FCanvas.RoundRect (R.Left ,R.Top ,R.Right ,R.Bottom ,5,5);
Fcanvas.CopyRect(Rect,fbmp.Canvas,rect);
Fcanvas.Handle := 0;
end;
procedure THlBitbtn.SetBmp(const Value: TbitMap);
begin
FBmp := Value;
end;
end.