안녕하세요! 캐리어입니다.
RYU님의 ImageBtn도 쓸만하더군요!
근데 소스는 없고 DCU File만 있어서 소스를 구했습니다.
http://www.wilsonc.demon.co.uk/delphi.htm 사이트에 가서 컴포넌트와 소스를 받았습니다.
근데 MouseOver와 MouseOut만 있어서 MouseDown를 추가했습니다.
저와 같이 이런 컴포넨트를 찾는 사람이 있을것 같아서 올려봅니다.
위 사이트에 가셔서 Down 받아서 아래 내용과 비교하시면 초보자님들에게 도움이 될서이라고
사료됩니다.
[문제점]
Mouse를 이리저리 움직이면 Image가 깜박이는데 이것은 어떻게 해결해야할지 모르겠더군요
--아래--
unit ImgChgBtn;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TImgChgBtn = class(TGraphicControl)
private
Dest1 : TRect;
Dest2 : TRect;
Dest3 : TRect;
EAutosize : Boolean;
ECenter : Boolean;
EPicture1 : TPicture;
EPicture2 : TPicture;
EPicture3 : TPicture;
ETransparent : Boolean;
Mousein : Boolean;
MousePush : Boolean;
Procedure PictureChanged(Sender:TObject);
procedure SetAutosize(Value: Boolean);
procedure SetCenter(Value: Boolean);
procedure SetPicture1(Value:TPicture);
procedure SetPicture2(Value:TPicture);
procedure SetPicture3(Value:TPicture);
procedure SetTransparent(Value: Boolean);
procedure CMMou
seEnter(var AMsg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
procedure CMMousePush(var AMsg: TMessage); message WM_LBUTTONDOWN;
procedure CMMousePull(var AMsg: TMessage); message WM_LBUTTONUP;
protected
procedure Paint; override;
public
constructor Create(COwner: TComponent); override;
destructor Destroy; override;
published
Property Align;
property Anchors;
property Autosize: Boolean read EAutosize write SetAutosize;
property Center: Boolean read ECenter write SetCenter;
property Cursor;
property Constraints;
property Enabled;
property Picture_Default: TPicture read EPicture1 write SetPicture1;
property Picture_MouseOver: TPicture read EPicture2 write
SetPicture2;
property Picture_MouseDown: TPicture read EPicture3 write SetPicture3;
property Popupmenu;
property ShowHint;
property Transparent: Boolean read ETransparent write SetTransparent;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('공개Btn', [TImgChgBtn]);
end;
constructor TImgChgBtn.Create(COwner: TComponent);
begin
inherited Create(COwner);
ControlStyle := ControlStyle + [csReplicatable];
ControlStyle := ControlStyle - [csOpaque];
EPicture1 := TPicture.Create;
EPicture2 := TPicture.Create;
EPicture3 := TPicture.Create;
EPicture1.OnChange := PictureChanged;
if Height<1 then Height := 60;
if Width<1 then Width := 60;
picturechanged(self);
end;
destructor TImgChgBtn.Destroy;
begin
EPicture1.Free;
EPicture2.Free;
EPicture3.Free;
inherited Destroy;
end;
procedure TImgChgBtn.SetAutosize(Value: Boolean);
begin
if EAutosize <> Value then
begin
EAutosize := Value;
PictureChanged(Self);
end;
end;
procedure TImgChgBtn.SetCenter(Value: Boolean);
begin
if ECenter <> Value then
begin
ECenter := Value;
PictureChanged(Self);
end;
end;
procedure TImgChgBtn.SetPicture1(Value: TPicture);
begin
EPicture1.Assign(Value);
PictureChanged(Self);
end;
procedure TImgChgBtn.SetPicture2(Value: TPicture);
begin
EPicture2.Assign(Value);
PictureChanged(Self);
end;
procedure TImgChgBtn.SetPicture3(Value: TPicture);
begin
EPicture3.Assign(Value);
PictureChanged(Self);
end;
procedure TImgChgBtn.SetTransparent(Value: Boolean);
begin
if Value <> ETransparent then
begin
ETransparent:=value;
PictureChanged(Self);
end;
end;
procedure TImgChgBtn.Paint;
begin
Dest1:= Bounds((Width - Picture_Default.Width) div 2, 0,
Picture_Default.Width, Picture_Default.Height);
Dest2 := Bounds((Width - Picture_MouseOver.Width) div 2, 0,
Picture_MouseOver.Width, Picture_MouseOver.Height);
Dest3 := Bounds((Width - Picture_MouseDown.Width) div 2, 0,
Picture_MouseDown.Width, Picture_MouseDown.Height);
if mousein=false then begin
with inherited Canvas do
begin
if Picture_Default.Graphic<>nil then
StretchDraw(dest1,Picture_Default.Graphic);
if mousepush = true then
begin
StretchDraw(dest3,Picture_MouseDown.Graphic);
end;
end;
end else begin
with inherited Canvas do
begin
if Picture_MouseOver.Graphic<>nil then
StretchDraw(dest2, Picture_MouseOver.Graphic)
else
if Picture_Default.Graphic<>nil then
StretchDraw(dest1, Picture_Default.Graphic);
if mousepush = true then
begin
StretchDraw(dest3,Picture_MouseDown.Graphic);
end;
end;
end;
if csDesigning in ComponentStat
e then
drawfocusrect(canvas.handle,rect(0,0,width,height));
end;
procedure TImgChgBtn.PictureChanged(Sender: TObject);
var w,h:word;
begin
if autosize then begin
if Picture_Default.Width>Picture_MouseOver.Width then
w:=Picture_Default.Width else w:=Picture_MouseOver.Width;
if Picture_Default.Height>Picture_MouseOver.Height then
h:=Picture_Default.Height else h:=Picture_MouseOver.Height;
if Picture_Default.Height>Picture_MouseDown.Height then
h:=Picture_Default.Height else h:=Picture_MouseDown.Height;
end else begin
h:=height;
w:=width;
end;
if h<5 then h:=5;
if w<5 then w:=5;
setbounds(left,top,w,h);
if Picture_Default.Graphic<>nil then
Picture_Default.Graphic.Transparent:=Etransparent;
if Picture_MouseOver.Graphic<>nil then
Pic
ture_MouseOver.Graphic.Transparent:=Etransparent;
if Picture_MouseDown.Graphic<>nil then
Picture_MouseDown.Graphic.Transparent:=Etransparent;
Invalidate;
end;
procedure TImgChgBtn.CMMouseEnter(var AMsg: TMessage);
begin
MouseIn:=True;
Refresh;
end;
procedure TImgChgBtn.CMMouseLeave(var AMsg: TMessage);
begin
MouseIn:=False;
Refresh;
end;
procedure TImgChgBtn.CMMousePush(var AMsg: TMessage);
begin
MousePush := True;
Refresh;
inherited;
end;
procedure TImgChgBtn.CMMousePull(var AMsg: TMessage);
begin
MousePush := False;
Refresh;
inherited;
end;
end.