1

我正在尝试完成一项简单的任务,但是通过使用 GDI+,我找不到任何示例。在我的代码中,我需要更改图像位置(如果更具体,则为顶部),但我不知道是否可以以更好的方式做到这一点。这就是我现在的做法:

procedure TForm2.Timer1Timer(Sender: TObject);
var
  I: Integer;
begin
  if image1.Top = -93 then
  Begin
    for I := -93 to -1 do
    Begin
    Sleep(10);
    image1.Top := Image1.Top + 1;
    Application.ProcessMessages;
  End;
End else if image1.Top = 0 then
Begin
  for I := 0 downto -92 do
  Begin
    Sleep(10);
    image1.Top := Image1.Top - 1;
    Application.ProcessMessages;
  End;
End;
end;

嗯,这很简单,但它并不顺利,而是在每一步都会跳跃和重绘。

我感谢您的帮助。

更新:感谢 TLama 和他的灵感,我找到了这个用于 delphi 2007 的 GDIPlus 实现

4

1 回答 1

1

移动控制是一种错误的动画方式,独立于 GDI+。相反,您应该记住要为动画更改的位置,在OnTimer事件中修改它并告诉系统您要使目标控件无效。然后在控件的OnPaint事件中,您应该在该位置呈现您想要的任何内容。

所以首先,用TImagea 替换你的组件,TPaintBox因为TImage它主要用于静态图像,而不是动态渲染。也使用两个计时器。一个用于向上动画,一个用于向下动画。

以下代码没有考虑计时器的近似值,并且它使用了不太为人所知Delphi 2009 GDI+ Library的 Delphi 包装器:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, GdiPlus;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Timer2: TTimer;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
  private
    GPImage: IGPImage;
    FImageTop: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FImageTop := 0;
  Timer1.Interval := 15;
  Timer2.Interval := 15;
  DoubleBuffered := True;
  Timer1.Enabled := True;
  Timer2.Enabled := False;
  GPImage := TGPImage.Create('d:\Image.jpg');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // no need for the following line since it's a reference of the interface
  // GPImage := nil;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (FImageTop > -93) then
  begin
    FImageTop := FImageTop - 1;
    PaintBox1.Invalidate;
  end
  else
  begin
    Timer1.Enabled := False;
    Timer2.Enabled := True;
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  if (FImageTop < 0) then
  begin
    FImageTop := FImageTop + 1;
    PaintBox1.Invalidate;
  end
  else
  begin
    Timer2.Enabled := False;
    Timer1.Enabled := True;
  end;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  GPGraphics: IGPGraphics;
begin
  GPGraphics := TGPGraphics.Create(PaintBox1.Canvas.Handle);
  GPGraphics.DrawImage(GPImage, 0, FImageTop);
end;

end.
于 2013-03-24T04:07:09.080 回答