请问:如何做出几个移上去可以换图片的按钮? (100分)

  • 主题发起人 主题发起人 balloy
  • 开始时间 开始时间
B

balloy

Unregistered / Unconfirmed
GUEST, unregistred user!
我试过用ToolButton,配合HotImages属性,但使用ToolButton时,图片会在原来的大小外加上边框,
破坏了我的背景。而我用SpeedButton,4个图里又没有hot图片。
请大家帮帮忙啊!还有什么好的控件吗?最好是Delphi自带的。
 
From中的 SpeenButton 可以用以下方法实现:

implementation

{$R *.DFM}
var
InButton:Boolean=false;
bIn,bOut:TBitmap;
procedure TForm1.FormCreate(Sender: TObject);
begin
bIn:=TBitmap.Create ;
bOut:=TBitmap.Create ;
bIn.LoadFromFile('c:/windows/circles.bmp');
bOut.LoadFromFile('c:/windows/bubbles.bmp');
speedbutton1.glyph:=bOut;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
bIn.free;
bOut.free;
end;

procedure TForm1.SpeedButton1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if Inbutton then exit;
InButton:=not(InButton);
speedbutton1.Glyph:=bIn;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not(Inbutton) then exit;
InButton:=not(InButton);
speedbutton1.Glyph:=bOut;

end;

end.
 
多谢,这个是可以的,但使用起来太繁琐了,我不希望给主界面添加太多这种辅助界面的代码(因为逻辑代码已经很复杂了)。
我的想法是,能不能我继承TSpeedButton自己写一个类?然后可以和SpeedButton一样的使用?
但如果需要容器来判断鼠标移出按钮,就不太好封装了,可以给SpeedButton添加OnMouseOut事件吗?
 
sunisoft UI控件不是都有这样的功能吗
源代码在源码空间可以找到
你可以参看他们的源代码
 
我的未完成控件代码,但可以参考
//作用双态Image
unit ImageHot;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Graphics;

type
TImageHot = class(TImage)
private
FGlyphOut: TPicture;
FGlyphIn: TPicture;
FIn: Boolean;
{ Private declarations }
procedure CMMouseEnter(var AMsg: TMessage);
message CM_MOUSEENTER;
procedure CMMouseLeave(var AMsg: TMessage);
message CM_MOUSELEAVE;
procedure SetGlyph(const Index: Integer; const Value: TPicture);
procedure DoPaint;
protected
{ Protected declarations }
procedure Loaded; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property GlyphIn: TPicture index 1 read FGlyphIn write SetGlyph;
property GlyphOut: TPicture index 2 read FGlyphOut write SetGlyph;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Custom', [TImageHot]);
end;

{ TImageHot }

procedure TImageHot.CMMouseEnter(var AMsg: TMessage);
begin
FIn := true;
DoPaint;
end;

procedure TImageHot.CMMouseLeave(var AMsg: TMessage);
begin
Fin := False;
DoPaint;
end;

constructor TImageHot.Create(AOwner: TComponent);
begin
inherited Create(AOwner); ;
FGlyphOut := TPicture.Create;
FGlyphIn := TPicture.Create;
Self.Width := 75;
Self.Height := 25;
Self.Transparent := true;
Self.Center := true;
end;

destructor TImageHot.Destroy;
begin
FGlyphOut.Free;
FGlyphIn.Free;
inherited Destroy;
end;

procedure TImageHot.DoPaint;
var
x, y, i: integer;
begin
x := 0; y := 0;
canvas.pen.width := 1;
for i := 0 to 8 do
begin
canvas.pen.color := ClBlack;
canvas.moveto(left + Clientwidth + x,
top + y);
canvas.lineto(left + Clientwidth + x,
top + height + y);
canvas.pen.color := $00606060;
canvas.moveto(left + x,
top + Clientheight + y);
canvas.lineto(left + Clientwidth + x,
top + height + y);
x := x + 1;
y := y + 1;
end;

if FIn then
self.Picture := FGlyphIn
else
self.Picture := FGlyphOut;
end;

procedure TImageHot.Loaded;
begin
inherited;
Self.Picture.Assign(FGlyphOut);
Self.Canvas.Ellipse(0, 0, clientwidth, clientheight);
end;

procedure TImageHot.SetGlyph(const Index: Integer; const Value: TPicture);
begin
case Index of
1: FGlyphIn.Assign(Value);
2: FGlyphOut.Assign(Value);
end;
end;

end.
 
to heipi2003,
你说的源码空间网址是什么?不好意思,我刚开始学Delphi,还不知道这些网站在哪儿。
多谢!
 

呵呵,只不过是拦两条消息
参考一下label,用不了几分钟就能做到。
 
源码空间网址:
www.playicq.com
 

送你一个吧。

unit SpeedButtonEx;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Buttons, Graphics;

type
TSpeedButtonEx = class(TSpeedButton)
private
FEnterGlyph: TBitmap;
FLeaveGlyph: TBitmap;
FOnMouseLeave: TNotifyEvent;
FOnMouseEnter: TNotifyEvent;

procedure SetEnterGlyph(Value: TBitmap);
procedure SetLeaveGlyph(Value: TBitmap);

procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy;override;
published
property EnterGlyph: TBitmap read FEnterGlyph write SetEnterGlyph; //移入图片
property LeaveGlyph: TBitmap read FLeaveGlyph write SetLeaveGlyph; //移出图片
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter; //移入事件
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave; //移出事件

end;

procedure Register;

implementation

constructor TSpeedButtonEx.Create(AOwner: TComponent);
begin
inherited;
FEnterGlyph := TBitmap.Create;
FLeaveGlyph := TBitmap.Create;
end;

destructor TSpeedButtonEx.Destroy;
begin
FEnterGlyph.Free;
FLeaveGlyph.Free;
inherited;
end;

procedure TSpeedButtonEx.SetEnterGlyph(Value: TBitmap);
begin
FEnterGlyph.Assign(Value);
end;

procedure TSpeedButtonEx.SetLeaveGlyph(Value: TBitmap);
begin
FLeaveGlyph.Assign(Value);
end;

procedure TSpeedButtonEx.CMMouseEnter(var Message: TMessage); //message CM_MOUSEENTER;
begin
Glyph.Assign(FEnterGlyph);
inherited;
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self);
end;

procedure TSpeedButtonEx.CMMouseLeave(var Message: TMessage); //message CM_MOUSELEAVE;
begin
Glyph.Assign(FLeaveGlyph);
inherited;
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self);
end;

procedure Register;
begin
RegisterComponents('dwh', [TSpeedButtonEx]);
end;

end.
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部