效果如图,支持普通、移上去、按下、弹起、禁用5种状态。

unit BmpBtn;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls;

type

TButtonLayout = (blGlyphLeft, blGlyphRight, blGlyphTop, blGlyphBottom);

TDesignType = (dtMenu, dtButton);

TBmpButton = class(TGraphicControl)

private

MOver: TBitmap;

MDown: TBitmap;

MUp: TBitmap;

Bmp: TBitmap;

ActualBmp: TBitmap;

BmpDAble: TBitmap;   // 禁用状态图像

FGlyph: TIcon;

//FTransparentGlyph: Boolean;

FTransparentBmp: Boolean;

FLayout: TButtonLayout;

FSpacing: integer;

FDesignType: TDesignType; //用于菜单还是按钮

//FColorText: TColor;

BtnClick: TNotifyEvent;

OnMDown: TMouseEvent;

OnMUp: TMouseEvent;

OnMEnter: TNotifyEvent;

OnMLeave: TNotifyEvent;

procedure SetMOver(Value: TBitmap);

procedure SetMDown(Value: TBitmap);

procedure SetMUp(Value: TBitmap);

procedure SetBmp(Value: TBitmap);

procedure SetBmpDAble(Value: TBitmap);

procedure SetGlyph(Value: TIcon);      //

procedure SetLayout(Value: TButtonLayout);

//procedure SetTransparentGlyph(Value: Boolean);

procedure SetTransparentBmp(Value: Boolean);

procedure SetSpacing(Value: Integer);

// procedure SetColors(Value: TColor);

procedure SetDesignType(Value: TDesignType);

protected

procedure Paint; override;

procedure MouseDown(Button: TMouseButton; Shift: TShiftState;

X, Y: Integer); override;

procedure MouseUp(Button: TMouseButton; Shift: TShiftState;

X, Y: Integer); override;

procedure MouseEnter(var Message: TMessage); message CM_MOUSEENTER;

procedure MouseLeave(var Message: TMessage); message CM_MOUSELEAVE;

procedure TextChanged (var msg: TMessage); message CM_TEXTCHANGED;

procedure Click; override;

public

constructor Create(AOwner: TComponent); override;

published

property BitmapOver: TBitmap read MOver write SetMOver;

property BitmapDown: TBitmap read MDown write SetMDown;

property BitmapUp: TBitmap read MUp write SetMUp;

property BitmapDisabled: TBitmap read BmpDAble write SetBmpDAble;

property Bitmap: TBitmap read Bmp write SetBmp;

property Glyph: TIcon read FGlyph write SetGlyph; //加入图标属性

property Layout: TButtonLayout read FLayout write SetLayout;  //加入布局属性

//property TransparentGlyph: Boolean read FTransparentGlyph write SetTransparentGlyph; //加入透明度属性(去否去掩码,针对小图标)

property TransparentBmp: Boolean read FTransparentBmp write SetTransparentBmp; //加入透明度属性(去否去掩码,针对背景图像)

property Spacing: integer read FSpacing write SetSpacing; //加入图标和文字的间距属性

property Font;  //加入文字属性

property Caption; //加入文字

// property ColorText: TColor read FColorText write SetColors; //文字颜色

property DesignType: TDesignType read FDesignType write SetDesignType; //指定设计类型

property OnClick: TNotifyEvent read BtnClick write BtnClick;

property OnMouseDown: TMouseEvent read OnMDown write OnMDown;

property OnMouseUp: TMouseEvent read OnMUp write OnMUp;

property OnMouseEnter: TNotifyEvent read OnMEnter write OnMEnter;

property OnMouseLeave: TNotifyEvent read OnMLeave write OnMLeave;

property Enabled;

property ShowHint;

property ParentShowHint;

property ParentFont;

property Visible;

end;

procedure Register;

implementation

procedure Register;

begin

RegisterComponents('SkinsDesign', [TBmpButton]);

end;

{ TImageButton }

constructor TBmpButton.Create(AOwner: TComponent);

begin

inherited Create(AOwner);

MOver := TBitmap.Create;

MDown := TBitmap.Create;

MUp := TBitmap.Create;

Bmp := TBitmap.Create;

BmpDAble := TBitmap.Create;

ActualBmp := TBitmap.Create;

FGlyph := TIcon.Create;

//TransparentGlyph := True;

FSpacing := 4;

//FColorText := clBlack;

Width := 75;

Height := 25;

Canvas.Brush.Color := clBtnFace;

ShowHint := true;

end;

procedure TBmpButton.Paint;

var

TempBmp: TBitMap;

CaptionRect: TRect;

GlyphLeft, GlyphTop, TextTop, TextLeft, TextWidth, TextHeight: integer;

//TextColor: TColor;

begin

inherited Paint;

TempBmp := TBitMap.Create;

TempBmp.Width := Width;

TempBmp.Height := Height;

TempBmp.TransparentColor:= clFuchsia;

TempBmp.Transparent := FTransparentBmp;

if ActualBmp.Width = 0 then ActualBmp.Assign(Bmp);

TempBmp.Canvas.FillRect(Rect(0,0,Width,Height));

if Enabled or (BmpDAble.Width = 0) then TempBmp.Canvas.Draw(0,0,ActualBmp)

else begin

Width := BmpDAble.Width;

Height := BmpDAble.Height;

TempBmp.Canvas.Draw(0,0,BmpDAble);

end;

TempBmp.Canvas.Font := Font;

TextWidth := TempBmp.Canvas.TextWidth(Caption);

TextHeight := TempBmp.Canvas.TextHeight(Caption);

TextTop := (Height - TextHeight) div 2;

TextLeft := (Width - TextWidth) div 2;

if not Glyph.Empty then

begin

GlyphLeft:= 0;

case FLayout of

blGlyphLeft:   begin

GlyphTop:= (Height - FGlyph.Height) div 2;

GlyphLeft:= TextLeft - FGlyph.Width div 2;

inc(TextLeft, FGlyph.Width div 2);

if not (Caption = '') then begin

GlyphLeft:= GlyphLeft - FSpacing div 2 - FSpacing mod 2;

inc(TextLeft, FSpacing div 2);

end;

end;

blGlyphRight:  begin

GlyphTop:= (Height - FGlyph.Height) div 2;

GlyphLeft:= TextLeft + TextWidth - FGlyph.Width div 2;

inc(TextLeft, - FGlyph.Width div 2);

if not (Caption = '') then begin

GlyphLeft:= GlyphLeft + FSpacing div 2 + FSpacing mod 2;

inc(TextLeft, - FSpacing div 2);

end;

end;

blGlyphTop:    begin

GlyphLeft:= (Width - FGlyph.Width) div 2;

GlyphTop:= TextTop - FGlyph.Height div 2 - FGlyph.Height mod 2;

inc(TextTop, FGlyph.Height div 2);

if not (Caption = '') then begin

GlyphTop:= GlyphTop - FSpacing div 2 - FSpacing mod 2;

inc(TextTop, + FSpacing div 2);

end;

end;

blGlyphBottom: begin

GlyphLeft:= (Width - FGlyph.Width) div 2;

GlyphTop:= TextTop + TextHeight - Glyph.Height div 2;

inc(TextTop, - FGlyph.Height div 2);

if not (Caption = '') then begin

GlyphTop:= GlyphTop + FSpacing div 2 + FSpacing mod 2;

inc(TextTop, - FSpacing div 2);

end;

end;

end;

end;

{if FBtnState = bsDown then

begin

inc(GlyphTop, 1);

inc(GlyphLeft, 1);

end; }

//FGlyph.TransparentColor:= FGlyph.Canvas.Pixels[0, 0];

//FGlyph.Transparent:= FTransparentGlyph;

TempBmp.Canvas.Draw(GlyphLeft, GlyphTop, FGlyph);

with CaptionRect do begin

Top:= TextTop;

Left:=TextLeft;

Right:= Left + TextWidth;

Bottom:= Top + TextHeight;

end;

if Caption <> '' then begin

TempBmp.Canvas.Brush.Style:= bsClear;

DrawText(TempBmp.Canvas.Handle,

PChar(Caption),

length(Caption),

CaptionRect,

DT_CENTER or DT_VCENTER or DT_SINGLELINE or DT_NOCLIP);

end;

Canvas.Draw(0, 0, TempBmp);

TempBmp.Free;

end;

procedure TBmpButton.Click;

begin

inherited Click;

Paint;

if Enabled then if Assigned(BtnClick) then BtnClick(Self);

end;

procedure TBmpButton.SetMOver(Value: TBitmap);

begin

MOver.Assign(Value);

Paint;

end;

procedure TBmpButton.SetMDown(Value: TBitmap);

begin

MDown.Assign(Value);

Paint;

end;

procedure TBmpButton.SetMUp(Value: TBitmap);

begin

MUp.Assign(Value);

Paint;

end;

procedure TBmpButton.SetBmp(Value: TBitmap);

begin

Bmp.Assign(Value);

ActualBmp.Assign(Value);

Width := Bmp.Width;

Height := Bmp.Height;

Paint;

end;

procedure TBmpButton.SetBmpDAble(Value: TBitmap);

begin

BmpDAble.Assign(Value);

paint;

end;

procedure TBmpButton.MouseDown(Button: TMouseButton; Shift: TShiftState;

X, Y: Integer);

begin

inherited MouseDown(Button, Shift, X, Y);

if (Button = mbLeft) and Enabled then begin

if Assigned (OnMDown) then OnMDown(Self, Button, Shift, X, Y);

if MDown.Width > 0 then begin

ActualBmp.Assign(MDown);

Width := MDown.Width;

Height := MDown.Height;

Paint;

end;

end;

end;

procedure TBmpButton.MouseUp(Button: TMouseButton; Shift: TShiftState;

X, Y: Integer);

//var MouseOverButton: Boolean;

//    P: TPoint;

begin

Case FDesignType of

dtMenu:

begin

ActualBmp.Assign(MDown);

Paint;

end;

dtButton:

begin

inherited MouseUp(Button, Shift, X, Y);

end;

end;

//if (x>0) and (y>0) and (x<width) and (y<height) then

{if (Button = mbLeft) and Enabled then begin

if Assigned (OnMUp) then OnMUp(Self, Button, Shift, X, Y);

if MUp.Width > 0 then begin

GetCursorPos(P);

MouseOverButton := (FindDragTarget(P, True) = Self);

if MouseOverButton then begin

Width := MUp.Width;

Height := MUp.Height;

Canvas.FillRect(Rect(0,0,Width,Height));

Canvas.Draw(0,0,MUp);

end else begin

Width := bmp.Width;

Height := Bmp.Height;

Canvas.FillRect(Rect(0,0,Width,Height));

Canvas.Draw(0,0,Bmp);

end;

end else begin

if MouseOverButton = false then begin

Width := MOver.Width;

Height := MOver.Height;

Canvas.FillRect(Rect(0,0,Width,Height));

Canvas.Draw(0,0,MOver);

end else begin

Width := bmp.Width;

Height := Bmp.Height;

Canvas.FillRect(Rect(0,0,Width,Height));

Canvas.Draw(0,0,Bmp);

end;

end;

end;  }

end;

procedure TBmpButton.MouseEnter(var Message: TMessage);

begin

if Enabled then begin

if MOver.Width > 0 then begin

ActualBmp.Assign(MOver);

Width := MOver.Width;

Height := MOver.Height;

Paint;

end;

end;

end;

procedure TBmpButton.MouseLeave(var Message: TMessage);

begin

Case FDesignType of

dtMenu:

begin

Exit;

end;

dtButton:

begin

if Enabled then begin

if Bmp.Width > 0 then begin

ActualBmp.Assign(Bmp);

Width := Bmp.Width;

Height := Bmp.Height;

Paint;

end;

end;

end;

end;

end;

procedure TBmpButton.SetGlyph(Value: TIcon);

begin

FGlyph.Assign(Value);

Invalidate;

end;

procedure TBmpButton.SetLayout(Value: TButtonLayout);

begin

FLayout:= Value;

Invalidate;

end;

{procedure TBmpButton.SetTransparentGlyph(Value: Boolean);

begin

FTransparentGlyph:= Value;

Invalidate;

end; }

procedure TBmpButton.SetSpacing(Value: Integer);

begin

FSpacing:= Value;

Invalidate;

end;

{procedure TBmpButton.SetColors(Value: TColor);

begin

FColorText := Value;

Paint;

end;  }

procedure TBmpButton.TextChanged(var msg: TMessage);

begin

Invalidate;

end;

procedure TBmpButton.SetDesignType(Value: TDesignType);

begin

FDesignType := Value;

Invalidate;

end;

procedure TBmpButton.SetTransparentBmp(Value: Boolean);

begin

FTransparentBmp:= Value;

Invalidate;

end;

end.

http://blog.csdn.net/zang141588761/article/details/52287872

Delphi皮肤之 - 图片按钮的更多相关文章

  1. [示例] Firemonkey 图片按钮(3态)

    说明:Firemonkey 图片按钮(支持三种状态:MouseOver, MouseDown, MouseUp,可各别指定图片) 原码下载:[示例]TestImageButton_圖片按鈕(3态).z ...

  2. [CSS]Input标签与图片按钮对齐

    页面直接摆放一个input文本框与ImageButton图片按钮,但是发现没有对齐: <input type="text" id="txtQty" /&g ...

  3. Expression Blend4经验分享:制作一个简单的图片按钮样式

    这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...

  4. 漂亮的自适应宽度的多色彩CSS图片按钮

    一.素材               二.效果 三.CSS *{padding:0;margin:0} /*----------------------------------- 自适应宽度图片按钮 ...

  5. WPF利用Image实现图片按钮

    之前有一篇文章也是采用了Image实现的图片按钮,不过时间太久远了,忘记了地址.好吧,这里我进行了进一步的改进,原来的文章中需要设置4张图片,分别为可用时,鼠标悬浮时,按钮按下时,按钮不可用时的图片, ...

  6. 在VC中,为图片按钮添加一些功能提示(转)

    在VC中,也常常为一些图片按钮添加一些功能提示.下面讲解实现过程:该功能的实现主要是用CToolTipCtrl类.该类在VC  msdn中有详细说明.首先在对话框的头文件中加入初始化语句:public ...

  7. Android ImageButton Example 图片按钮

    Android ImageButton Example 图片按钮 使用“android.widget.ImageButton” 展现一个具有背景图片的按钮 本教程将展现一个具有名字为 c.png背景图 ...

  8. 使用KindEditor富文本编辑器,点击批量上传按钮没有选择图片按钮

    问题:批量上传没有选择图片按钮

  9. WPF控件库:图片按钮的封装

    需求:很多时候界面上的按钮都需要被贴上图片,一般来说: 1.按钮处于正常状态,按钮具有背景图A 2.鼠标移至按钮上方状态,按钮具有背景图B 3.鼠标点击按钮状态,按钮具有背景图C 4.按钮处于不可用状 ...

随机推荐

  1. Opencv中使用Surf特征实现图像配准及对透视变换矩阵H的平移修正

    图像配准需要将一张测试图片按照第二张基准图片的尺寸.角度等形态信息进行透视(仿射)变换匹配,本例通过Surf特征的定位和匹配实现图像配准. 配准流程: 1. 提取两幅图像的Surf特征 2. 对Sur ...

  2. 【codeforces 765C】Table Tennis Game 2

    [题目链接]:http://codeforces.com/contest/765/problem/C [题意] 枚举游戏先拿到k分的人胜; 然后两个人一个人得了a分,一个人得了b分; 问你最多可能进行 ...

  3. matplotlib plot 绘图函数发生阻塞(block)时的解决方法

    Is there a way to detach matplotlib plots so that the computation can continue? 在一般编辑器中: from matplo ...

  4. CentOS 挂载iso文件配置yum源

    1.挂载iso 准备好centos的光盘镜像 挂载前的准备; mkdir -p /dev/centos mkdir -p  /mnt/local_yum 挂载 mount -o loop /opt/s ...

  5. 欢迎来到Swift天地(Welcome to Swift)

    期待已久的WWDC真的是不管是什么硬件更新,没有太多的开发者,本次会议是还是很有亮点.水果给我们带来了一种新的语言Swift.种无比简洁高效的语言,并且新的 Swift 语言依然会和 C 与 Obje ...

  6. Android之消息推送实现

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  7. wpf控件设计时支持(3)

    原文:wpf控件设计时支持(3) wpf设计时调试 编辑模型 装饰器 1.wpf设计时调试 为了更好的了解wpf设计时框架,那么调试则非常重要,通过以下配置可以调试控件的设计时代码 (1)将启动项目配 ...

  8. 简明Python3教程 8.控制流

    简介 迄今为止我们见到的所有程序总是含有一连串语句并且python忠实的顺序执行它们. 那么如何改变它们的执行顺序呢?例如你希望程序根据不同情况作出不同反应,按照当前时间分别 打印出’Good Mor ...

  9. 使用WPF将图片转变为灰度并加上水印并保存为文件

    原文:使用WPF将图片转变为灰度并加上水印并保存为文件 运行效果: (上图中左下角为原图的缩小显示,By:Johnson为TextBlock)保存的结果图片:上图的"Test Words.& ...

  10. react项目实践——(3)babel

    1. babel Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行. (1)安装 npm install --save-dev babel-core babel-e ...