Delphi GDIPLUS改变图像位置

时间:2013-03-24 01:52:47

标签: image delphi gdi+

我正在尝试实现一个简单的任务,但通过使用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和他的灵感,我发现了this GDIPlus实现delphi 2007

1 个答案:

答案 0 :(得分:1)

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

首先,将TImage组件替换为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.