问题来源:http://www.cnblogs.com/del/archive/2011/02/11/1951641.html#2025348

测试图片:

测试代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BitBtn1MouseLeave(Sender: TObject);
    procedure BitBtn1MouseEnter(Sender: TObject);
    procedure BitBtn1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BitBtn1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    procedure ChangeBitmap(Sender: TObject);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses jpeg;

const picPath = 'C:\Temp\btn_sys_close.jpg';
var
  tag2: Integer = -1;
  jpg: TJPEGImage;
  bit: TBitmap;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if not FileExists(picPath) then begin beep; Application.Terminate; end;
  jpg := TJPEGImage.Create;
  jpg.LoadFromFile(picPath);
  bit := TBitmap.Create;
  bit.SetSize(jpg.Width div 3, jpg.Height);
  BitBtn1.OnMouseLeave(BitBtn1);
end;

{被重复调用的过程}
procedure TForm1.ChangeBitmap(Sender: TObject);
begin
  if tag2 = TControl(Sender).Tag then Exit;
  tag2 := TControl(Sender).Tag;
  bit.Canvas.Draw(-bit.Width * tag2, 0, jpg);
  TBitBtn(Sender).Glyph := bit;
end;

procedure TForm1.BitBtn1MouseLeave(Sender: TObject);
begin
  TControl(Sender).Tag := 0;
  ChangeBitmap(Sender);
end;

procedure TForm1.BitBtn1MouseEnter(Sender: TObject);
begin
  TControl(Sender).Tag := 1;
  ChangeBitmap(Sender);
end;

procedure TForm1.BitBtn1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TControl(Sender).Tag := 2;
  ChangeBitmap(Sender);
end;

procedure TForm1.BitBtn1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TControl(Sender).Tag := 1;
  ChangeBitmap(Sender);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  jpg.Free;
  bit.Free;
end;

end.

posted on 2011-02-13 11:01  万一  阅读(2816)  评论(19编辑  收藏  举报