动态调整 TBitBtn 上的图片 - 回复 \"游戏而已\" 的问题

删除回忆录丶 提交于 2020-03-25 07:25:35

问题来源: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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!