Einzelnen Beitrag anzeigen

Benutzerbild von sniper_w
sniper_w

Registriert seit: 12. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#4

Re: Bild in Image mit der Maus verschieben

  Alt 3. Mär 2005, 22:29
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormResize(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ImagePos:TPoint;
  end;

var
  Form1: TForm1;
  MouseStartPos : TPoint;
  Down : Boolean = False;
  OffScreen, Picture : TBitmap;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
//Image1.Picture.Bitmap.LoadFromFile('c:\Pic.bmp');
ImagePos := Point(0,0);

Picture := TBitmap.Create;
Picture.LoadFromFile('c:\Pic.bmp');


OffScreen := TBitmap.Create;
OffScreen.PixelFormat := pf32bit;
OffScreen.Width := Width;
OffScreen.Height := Height;
OffScreen.Canvas.Brush.Color := clBlue;
OffScreen.Canvas.FillRect(Rect(0,0,OffScreen.Width,OffScreen.Height));
OffScreen.Canvas.Draw(0,0,Picture);
Canvas.Draw(0,0,OffScreen);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 MouseStartPos := Point(x,y);
 Down := True;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
  var temp : tpoint;
begin
 if Down then
 begin
   temp := Point( MouseStartPos.X - ImagePos.X ,MouseStartPos.Y - ImagePos.Y);
   OffScreen.Canvas.FillRect(Rect(0,0,OffScreen.Width,OffScreen.Height));
   OffScreen.Canvas.Draw(temp.x,temp.y,Picture);
   Canvas.Draw(0,0,OffScreen);
   MouseStartPos := Point(x,y)
 end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 Down := False;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
OffScreen.Width := Width;
OffScreen.Height := Height;
//Image1.Picture.Bitmap.Width := Image1.Width;
//Image1.Picture.Bitmap.Height :=Image1.Height;
FormPaint(self);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
OffScreen.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
   Canvas.Draw(0,0,OffScreen);
end;
Allerdings ohne TImage Komponente zu benutzen...
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat