BlueJade

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Image 32 自带的Demo,添加一些注解。

unit uFrmAnimation2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, System.Math, Img32;

type
  TfrmAnimation2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    img: TImage32;
    imgCnt: Integer;
    timer: TTimer;
    imgIndex: Integer;
    drawRec: TRect;
    procedure Timer1Timer(Sender: TObject);
  protected
    procedure WMERASEBKGND(var message: TMessage); message WM_ERASEBKGND;
  end;

var
  frmAnimation2: TfrmAnimation2;

implementation

{$R *.dfm}
{$R images.res}   // 这里有图片资源

uses
  Img32.Extra, Img32.Vector, Img32.Fmt.PNG;

function GetMinRotationRect(const rec: TRect): TRect;
var
  mp: TPointD;
  radius: double;
begin // 得出图片以中心点旋转时,所有旋转画布可正常展示的矩形区域.
  mp.X := rec.Width / 2;
  mp.Y := rec.Height / 2;
  radius := Distance(PointD(rec.Left, rec.Top), mp); // Img32.Vector    2点之间的距离
  Result.Left := Ceil(mp.X - radius);
  Result.Right := Ceil(mp.X + radius);
  Result.Top := Ceil(mp.Y - radius);
  Result.Bottom := Ceil(mp.Y + radius);
end;
// ------------------------------------------------------------------------------

function EnumResNameProc(hModule: hModule; lpszType, lpszName: PChar; lParam: LONG_PTR): BOOL; stdcall;
begin
  inc(PInteger(lParam)^);
  Result := true;
end;

procedure TfrmAnimation2.FormCreate(Sender: TObject);
var
  i: Integer;
  angle, angleDelta: double;
begin
  img := TImage32.Create;
  // 此窗口做为独立程序时,可以这样修改窗口大小,对此综合实例,暂无用途
  // img.LoadFromResource('RAZZ_00', 'PNG');
  // with GetMinRotationRect(img.Bounds) do
  // begin
  // ClientWidth := Width;
  // ClientHeight := Height;
  // end;

  // 统计在资源中为 PNG类型资源的数量
  EnumResourceNames(0, 'PNG', @EnumResNameProc, LONG_PTR(@imgCnt));
  // 设置窗体透明.
  self.Color := clRed;
  self.TransparentColorValue := self.Color;
  self.TransparentColor := true;
  // make the form **background** transparent
  // Self.TransparentColor := true; // :))
  // and make sure BorderStyle = bsNone and BorderIcons = []
  // 创建定时器,定时要求界面刷新
  timer := TTimer.Create(self);
  timer.OnTimer := Timer1Timer;
  timer.Interval := 33;
end;

procedure TfrmAnimation2.FormDestroy(Sender: TObject);
begin
  img.Free;
  timer.Free;
end;

procedure TfrmAnimation2.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // if CharInSet(Key, [#13, #27, #32]) then
  // Close;
end;

procedure TfrmAnimation2.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  timer.Enabled := not timer.Enabled;
end;

procedure TfrmAnimation2.FormPaint(Sender: TObject);
var
  X, Y: Integer;
  mpForm: TPointD;
begin
  with ClientRect do
    mpForm := PointD(Width / 2, Height / 2);
  img.LoadFromResource(format('RAZZ_%2.2d', [imgIndex]), 'PNG');
  X := Round(mpForm.X - img.MidPoint.X);
  Y := Round(mpForm.Y - img.MidPoint.Y);
  //居中中显示
  Canvas.FillRect(ClientRect);
  img.CopyToDc(img.Bounds, Canvas.Handle, X, Y, false);
end;

procedure TfrmAnimation2.Timer1Timer(Sender: TObject);
begin
  imgIndex := (imgIndex + 1) mod imgCnt;     //下1张图.
  Invalidate;
end;

procedure TfrmAnimation2.WMERASEBKGND(var message: TMessage);
begin
  message.Result := 1;
end;

end.

 

 

posted on 2024-06-11 16:39  bluejade2024  阅读(7)  评论(0编辑  收藏  举报