1>需求: 图片按钮
2>功能: 鼠标移入切换图片1
鼠标移出切换图片2
鼠标按下切换图片3 并锁定当前图片状态。不在支持鼠标移入移出状态
开发
1> 运行Delphi7->New->Component
2> New Component 对话框上 的Anscetor Type 选择 Timage [ExtCtrls]
Class Name 输入 THSImageExButton
详细开发方式看代码,已经有详细注释:
unit HSImageExButton; interface uses Windows, Messages, SysUtils, Classes, Graphics, ExtCtrls, Controls ; type THSImageExButton = class(TImage) private { Private declarations } // 鼠标移出切换的图片 mpr_tpic_MouseLeave : TPicture ; // 鼠标移入切换的图片 mpr_tpic_MouseEnter : TPicture ; // 锁定状态图片 mpr_tpic_ClickLock : TPicture ; // 锁定状态 True表示锁定 False表示解除锁定 mpr_bool_ClickLock : Boolean ; // 处理CM_MOUSELEAVE 和 CM_MOUSEENTER 消息 procedure mpr_MSG_MouseLeave(var message:TMessage);message CM_MOUSELEAVE ; procedure mpr_MSG_MouseEnter(var message:TMessage);message CM_MOUSEENTER ; // 鼠标移出时图片设置 procedure mpr_pro_SetPicMouseLeave(const Value:TPicture) ; // 鼠标移入时图片设置 procedure mpr_pro_SetPicMouseEnter(const Value:TPicture) ; // 鼠标按下时图片设置 procedure mpr_pro_SetPicClickLock(const Value:TPicture) ; // 设置锁定状态 procedure mpr_pro_SetLockState(const Value:Boolean) ; protected { Protected declarations } // 重载TImage的Click,当按下时激活锁定状态 procedure Click() ;override; public { Public declarations } constructor Create(AOwner:TComponent);override; destructor Destroy; override; published { Published declarations } // THSImageExButton 的设置图片移入属性 property PictureMouseLeave:TPicture read mpr_tpic_MouseLeave write mpr_pro_SetPicMouseLeave; // THSImageExButton 的设置图片移出属性 property PictureMouseEnter:TPicture read mpr_tpic_MouseEnter write mpr_pro_SetPicMouseEnter; // THSImageExButton 的设置锁定状态的图片 property PictureClickLock:TPicture read mpr_tpic_ClickLock write mpr_pro_SetPicClickLock ; // THSImageExButton 的锁定状态属性 property LockState:Boolean read mpr_bool_ClickLock write mpr_pro_SetLockState; end; procedure Register; implementation procedure Register; begin RegisterComponents('HS', [THSImageExButton]); end; constructor THSImageExButton.Create(AOwner:TComponent); begin inherited Create(AOwner) ; mpr_tpic_MouseLeave := TPicture.Create() ; mpr_tpic_MouseEnter := TPicture.Create() ; mpr_tpic_ClickLock := TPicture.Create() ; LockState := False ; end;// End Create() destructor THSImageExButton.Destroy; begin mpr_tpic_MouseLeave.Destroy() ; mpr_tpic_MouseEnter.Destroy() ; mpr_tpic_ClickLock.Destroy() ; inherited Destroy(); end;// End Destroy() procedure THSImageExButton.mpr_MSG_MouseLeave(var message:TMessage); begin if csDesigning in ComponentState then Exit; if mpr_bool_ClickLock = False then begin // 设置父控件的双缓冲属性,防止图片闪烁 if Self.Parent.DoubleBuffered <> True then begin Self.Parent.DoubleBuffered := True ; end; // 切换图片 inherited Picture.Assign(mpr_tpic_MouseLeave) ; end; end;// End MSG_MouseLeave() procedure THSImageExButton.mpr_MSG_MouseEnter(var message:TMessage); begin if csDesigning in ComponentState then Exit; if mpr_bool_ClickLock = False then begin if Self.Parent.DoubleBuffered <> True then begin Self.Parent.DoubleBuffered := True ; end; inherited Picture.Assign(mpr_tpic_MouseEnter) ; end; end;// End MSG_MouseEnter() procedure THSImageExButton.mpr_pro_SetPicMouseLeave(const Value:TPicture); begin mpr_tpic_MouseLeave.Assign(Value) ; end;// End mpr_pro_SetPicMouseLeave() procedure THSImageExButton.mpr_pro_SetPicMouseEnter(const Value:TPicture); begin mpr_tpic_MouseEnter.Assign(Value) ; end;// End mpr_pro_SetPicMouseEnter() procedure THSImageExButton.mpr_pro_SetPicClickLock(const Value:TPicture); begin mpr_tpic_ClickLock.Assign(Value) ; end;// End mpr_pro_SetPicClickLock() procedure THSImageExButton.Click() ; begin if csDesigning in ComponentState then Exit; if mpr_bool_ClickLock = False then begin mpr_bool_ClickLock := True ; inherited Picture.Assign(mpr_tpic_ClickLock) ; inherited Click() ; end; end;// End Click() procedure THSImageExButton.mpr_pro_SetLockState(const Value:Boolean); begin // 判断锁定状态 if mpr_bool_ClickLock = True then begin mpr_bool_ClickLock := False ; inherited Picture.Assign(mpr_tpic_MouseLeave) ; end else begin mpr_bool_ClickLock := True ; inherited Picture.Assign(mpr_tpic_ClickLock) end; end;// End mpr_pro_SetLockState() end.