Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#2

AW: Steuerung einer Bitmap auf einer Imagekomponente

  Alt 12. Feb 2014, 10:19
TImage ist eigentlich nicht die optimale Komponente für so etwas.
Wenn Du es dennoch versuchen möchtest, kannst Du das Canvas des Vorfahren (TGraphicControl) im überschreibenen Paint manipulieren vor inherited aufgerufen wird. Dies erfordert eine veröffenlichung des Canvas von TGraphicControl sowie ein überschreiben von Paint von TImage. Ein Beispiel mit einem Zoom von 2, einer Rotation um 45 Grad und einem Versatz von X/Y um 25, mit eine Interposerklasse:

Delphi-Quellcode:
unit Unit3;

interface

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

type
  TImage = Class(ExtCtrls.TImage)
  private
    FAngle: Double;
    FPoint: TPoint;
    FZoom: Double;
    procedure SetAngle(const Value: Double);
    procedure SetPoint(const Value: TPoint);
    procedure SetZoom(const Value: Double);
  public
    Constructor Create(AOwner:TComponent);override;
  published
    procedure Paint; override;
    property CenterPoint:TPoint read FPoint write SetPoint;
    property Angle:Double read FAngle write SetAngle;
    Property Zoom:Double read FZoom write SetZoom;
  End;

  TGraphicControl = Class(Controls.TGraphicControl)
  public
    Property Canvas;
  End;



  TForm3 = class(TForm)
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

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 TForm3.Button1Click(Sender: TObject);
begin

  Image1.Invalidate;
end;

{ TImage }

constructor TImage.Create(AOwner:TComponent);
begin
  inherited;
  FZoom := 1;
end;

procedure TImage.Paint;
begin

  SetCanvasZoomAndRotation(TGraphicControl(self).Canvas, FZoom, FAngle, FPoint.x, FPoint.y);
  inherited;
end;

procedure TImage.SetAngle(const Value: Double);
begin
  FAngle := Value;
  invalidate;
end;

procedure TImage.SetPoint(const Value: TPoint);
begin
  FPoint := Value;
  invalidate;
end;

procedure TImage.SetZoom(const Value: Double);
begin
  FZoom := Value;
  invalidate;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  KeyPreview := true;
end;

procedure TForm3.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
 p:TPoint;
begin
  if key in [37..40,187,189,76,82] then
  begin
      p := Image1.CenterPoint;
      case Key of
        38: p.Y := p.Y - 1;
        40: p.Y := p.Y + 1;
        37: p.X := p.X - 1;
        39: p.X := p.X + 1;
        187:Image1.zoom := Image1.Zoom + 0.1; // +
        189:Image1.zoom := Image1.Zoom - 0.1; // -
        82 :Image1.Angle := Image1.Angle + 1; // R
        76 :Image1.Angle := Image1.Angle - 1; // L
      end;
      Image1.CenterPoint := p;
  end;
  Caption := IntToStr(Ord(key))
end;

end.
Ich habe das Beispiel noch etwas erweitert um eine Tastatursteuerung
Miniaturansicht angehängter Grafiken
bild-2.png   bild-1.png  
Angehängte Dateien
Dateityp: zip temp.zip (92,9 KB, 9x aufgerufen)
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)

Geändert von Bummi (12. Feb 2014 um 10:46 Uhr)
  Mit Zitat antworten Zitat