如何在Delphi中从合成图像创建平移

前端之家收集整理的这篇文章主要介绍了如何在Delphi中从合成图像创建平移前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是delphi图形方法的新手,我一直在创建一个…视口,这就是我在为一个项目做的时候如何调用它.对不起,我不能提供任何代码,但我被困在逻辑部分,搜索谷歌指向我一些OnPaint,Draw方法.但那些不是我想要完成的,因为我有,例如:

> 1600×1000背景图像锚定到客户端的顶部/底部/右侧和左侧.
>多个TImage元素放置在x / y坐标上.
>像HTML中的地图元素一样的“热点”,我可以设置可点击区域(对于我在步骤2中放置的图像)
>不需要缩放.
>最重要的是,在拖动背景的同时,放置在背景上的TImages也需要拖动.

我的逻辑(在HTML / jQuery中)是创建一个#viewportBinder(这是我正在拖动的div,透明的bg),然后是另一个名为#viewtown(1600×1000,背景)的div,其中包含div(那些TImages)放在CSS的设定坐标.

所以当我拖动viewportBinder时,jQuery会在#viewport上设置新的x / y.隐含地,#viewport内的div(TImages)正在移动,因为父级是相对的.

有没有人有这种项目的经验?任何代码片段?

更具体一点,我会给你我的html例子,说明我所做的和我想要移植到Delphi代码中的内容http://www.youtube.com/watch?v=9iYqzvZFnGA

对不起,如果我不够清楚,我没有起点,因为我根本没有在delphi中的经验. (使用RAD Studio 2010)

解决方法

一个非常简短的例子,它是如何以一种简单的方式实现的.

您可以使用PaintBox进行绘画,1 Backimage,带有信息和透明pngimages的记录数组.

可以在偏移/缩放/旋转中操纵画布.
移动和命中检测会发生在mousedown和mousemove中.

它不完整,但可能会让你知道如何做到这一点.

[delphi]
unit Unit1;

interface

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

type
  TBuilding=Record   // record for building informations
    Pos:TPoint;
    PNGImage:TPngImage;
    // what ever needed
  End;

  TBuildingArray=Array of TBuilding; // array of buildings

  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X,Y: Integer);
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    FXoffs,FYOffs,FZoom:Double; // offset and zoom for painting
    FMouseDownPoint:TPoint;
    FBackGroundpnG:TPNGImage;
    FBuildingArray:TBuildingArray;
    procedure Check4Hit(X,Y: Integer);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation
uses Math;
{$R *.dfm}


Procedure SetCanvasZoomAndRotation(ACanvas:TCanvas;Zoom:Double;Angle:Double;CenterpointX,CenterpointY:Double);
var
    form : tagXFORM;
    Winkel:Double;
begin
      Winkel := DegToRad(Angle);
      SetGraphicsMode(ACanvas.Handle,GM_ADVANCED);
      SetMapMode(ACanvas.Handle,MM_ANISOTROPIC);
      form.eM11 := Zoom * cos( Winkel);
      form.eM12 := Zoom *Sin( Winkel)  ;
      form.eM21 := Zoom * (-sin( Winkel));
      form.eM22 := Zoom * cos( Winkel) ;
      form.eDx := CenterpointX;
      form.eDy := CenterpointY;
      SetWorldTransform(ACanvas.Handle,form);
end;

Procedure ResetCanvas(ACanvas:TCanvas);
begin
   SetCanvasZoomAndRotation(ACanvas,1,0);
end;


procedure TForm1.FormCreate(Sender: TObject);
var
 Path:String;
 i:Integer;
begin
   FZoom := 1;
   DoubleBuffered := true;
   Path := ExtractFilePath(Paramstr(0));
   FBackGroundpnG:=TPNGImage.Create;
   FBackGroundpnG.LoadFromFile(Path + 'infect.png');
   SetLength(FBuildingArray,3);
   for I := 0 to High(FBuildingArray)  do
      begin
         FBuildingArray[i].PNGImage := TPngImage.Create;
         FBuildingArray[i].PNGImage.LoadFromFile(Path + Format('B%d.png',[i]));
         FBuildingArray[i].Pos.X := I * 300;
         FBuildingArray[i].Pos.Y := Random(1000);
      end;

end;
procedure TForm1.FormDestroy(Sender: TObject);
var
 i:Integer;
begin
   for I := 0 to High(FBuildingArray)  do
      begin
         FBuildingArray[i].PNGImage.Free;
      end;
   FBackGroundpnG.Free;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  if FZoom=0.5 then FZoom := 1 else FZoom := 0.5;
  PaintBox1.Invalidate;
end;

procedure TForm1.Check4Hit(X,Y:Integer);
var
 i,Index:Integer;
 R:TRect;
 P:TPoint;
begin
   index := -1;
   for I := 0 to High(FBuildingArray)  do
      begin
         R := Rect(FBuildingArray[i].Pos.X,FBuildingArray[i].Pos.Y,FBuildingArray[i].Pos.X + FBuildingArray[i].PNGImage.Width,FBuildingArray[i].Pos.Y + FBuildingArray[i].PNGImage.Height);
         P := Point(Round((x - FXOffs)/FZoom),Round((y - FYOffs)/FZoom));
         if PtInRect(R,P) then Index := i;
      end;
   if index > -1 then
      begin
        Caption := Format('Last hit %d',[index]);
      end
   else Caption := 'No Hit';
end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X,Y: Integer);
begin
   Check4Hit(X,Y);
   FMouseDownPoint.X := X;
   FMouseDownPoint.Y := Y;
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
begin
   if ssLeft in Shift then
      begin
         FXoffs := -( FMouseDownPoint.X - X) ;
         FYoffs := -( FMouseDownPoint.Y - Y) ;
         if FXoffs>0 then FXoffs := 0;
         if FYoffs>0 then FYoffs := 0;
         PaintBox1.Invalidate;
      end;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
 i:Integer;
begin
   SetCanvasZoomAndRotation(PaintBox1.Canvas,FZoom,FXoffs,FYOffs);
   PaintBox1.Canvas.Draw(0,FBackGroundpnG);
   for I := 0 to High(FBuildingArray)  do
      begin
         PaintBox1.Canvas.Draw(FBuildingArray[i].Pos.X,FBuildingArray[i].PNGImage);
      end;
end;

end.

[/delphi]
原文链接:https://www.f2er.com/delphi/239392.html

猜你在找的Delphi相关文章