[SOLVED] How to move (drag) a picture on a TScrollBox via mouse? lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2024-10-26 09:14 My program can show a picture file with various scaling factors on a TScrollBox which resides on a TForm. If the (scaled) picture is bigger than the TForm, scrollbars from the TScrollBox appear. So you can move the visible part of the picture with the scrollbars in all 4 directions. I want, that you can move (drag) the visible part of the picture also via left-mouse in all 4 directions. I'm a beginner to Graphics and have no idea to start. Here are the details:  - the picture file is loaded into a TPicture  - this picture is streched by a selectable scaling factor and the result is stored into a TBitmap  - there is a TPaintBox which displays the TBitmap via Event TPaintBox.OnPaint  - the TPaintBox resides on a TScrollBox which has the same size as the TForm I guess that I need some kind of Mouse-Event(s), but don't know which Event(s) and for which Control(s). I want a solution for Windows and Linux. Thanks in advance. ---------------------------------------------------------------------- jamie · 2024-10-26 13:53 You need to pan the picture I guess ? using the MouseDown event, log the current X, Y position of the mouse and then while in the MouseMove event, take the current X, Y mouse position and create an offset value with the current location of the image and repaint it at that position.  Basically, you are offsetting your image via the amount of offset from where you first initially logged the mouse in the mouse down and where you are now when the mouse move event occurs.  also, make sure you test for the mouse button being down.  The TPoint Class has members in it to help do the math. ---------------------------------------------------------------------- jamie · 2024-10-26 16:24 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X,      Y: Integer);  private   public  ButtonDownAT:TPoint;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  ButtonDownAt:= TPoint.Create(X,Y); //Create a starting point.end; procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);Var  P:TPoint;begin  If ssLeft in Shift then With  button1 do   Begin    P :=Point(ButtonDownAt.X-X,ButtonDownAt.Y-Y);//Get difference.    Left := Left-P.X;    Top := Top-P.Y;    ButtonDownAt := Point(X+P.X,Y+P.Y); //Update for next position;   end;end; end.   This drags a button around the screen. Should give you an idea. ---------------------------------------------------------------------- Hartmut · 2024-10-26 16:53 Thanks a lot jamie for your posts and the demo. I understand what you suggest. Now I have 2 questions left: 1) Are Events OnMouseDown and OnMouseMove easier / better than Events OnDragDrop / OnDragOver / OnEndDrag? Or are the last 3 Events for another purpose? (I don't understand their Help in the OI (too short for me) and did not have yet the time for further investigations). 2) Whichever Events I use: from which Control (TPicture / TBitmap / TPaintBox / TScrollBox / TForm) must I use them? (please see therefore clause "Here are the details:" in my 1st post). ---------------------------------------------------------------------- jamie · 2024-10-26 17:11 I assume you are sliding the image around a little bit once dropped? What I suggested is a simple way to create an offset value for you to reposition the image drawing Starting point. So assuming you are using some sort of X,Y TPoint as the starting point during the Onpaint event, this X,Y is what you need to offset with the example code I showed you. The next painting cycle will use the new offset, so you get panning effect. As for the Scrollbox, I am not sure what you are using that for? I assume maybe because the image is too large to show. P..S  Just a comment, you can adjust the position of the scrollbars to move the image around if that is what you mean? Look at the properties of the scrollboxes scrollbars ---------------------------------------------------------------------- jamie · 2024-10-26 19:41 Look at this code for panning a TPaintBox around in a scrollbox. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,lclIntf, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    PaintBox1: TPaintBox;    ScrollBox1: TScrollBox;    procedure FormCreate(Sender: TObject);    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,      Y: Integer);    procedure PaintBox1Paint(Sender: TObject);  private   public   B:TBitmap;   P:TPoint;   StartPT :TPoint;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);Var  A:TBitmap;begin  A := Tbitmap.Create;  A.LoadFromDevice(GetDC(0));  B:= TBitMap.Create;  B.SetSize(320,256);  B.Canvas.StretchDraw(Rect(0,0,B.Width,B.Height),A);  B.canvas.Pen.Color := clBlack;  B.Canvas.Brush.Style := bsClear;  B.Canvas.Rectangle(0,0,320,256);  A.Free;  PaintBox1.Width :=320;  PaintBox1.Height := 256;end; procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin   StartPT := Point(X,Y);end; procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);begin   if ssleft in Shift then With PaintBox1 do   Begin     Left := Left-(StartPT.X-X);     Top :=  Top-(startPt.Y-Y);   end;end; procedure TForm1.PaintBox1Paint(Sender: TObject);begin  PaintBox1.Canvas.Draw(P.X,P.Y,B);end; end. ---------------------------------------------------------------------- Hartmut · 2024-10-27 11:54 I think I've got it. This is my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var mausStart: TPoint; {Mouse-Coordinates from the start of panning the picture} procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;                                    Shift: TShiftState; X, Y: Integer);   var P: TPoint;   begin   P:=Mouse.CursorPos; {get absolute Mouse-Coordinates}   if Button=mbLeft then mausStart:=P;   end; procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;                                    X, Y: Integer);   var SB: TControlScrollBar;       P: TPoint;       d: integer;   begin   P:=Mouse.CursorPos; {get absolute Mouse-Coordinates}   if not (ssLeft in Shift) then exit;    SB:=ScrollBox1.HorzScrollBar; {abbreviation}   d:=mausStart.X-P.X;           {horizontal distance}   SB.Position:=SB.Position+d;    SB:=ScrollBox1.VertScrollBar; {abbreviation}   d:=mausStart.Y-P.Y;           {vertical distance}   SB.Position:=SB.Position+d;    mausStart:=P;   end; procedure TForm1.PaintBox1Paint(Sender: TObject);   begin   if Bitmap1 <> nil then      Paintbox1.Canvas.Draw(0,0,Bitmap1);   end; I wanted that Event PaintBox1MouseMove() changes also the visible positions of the scrollbars (which are displayed when the (scaled) picture is bigger than the Form) instead of only changing the starting point of the PaintBox. And I had to use absolute Mouse-Coordinates instead of PaintBox- relative coordinates, because PaintBox-relative coordinates *change*, when you pan the picture and that gave a strange effect. Thank you very much jamie for your valuable help. Only one point is left: While the picture is panned, I want to show a different type of mouse cursor, e.g. a hand-symbol or a quad-arrow (which has an arrow left and right and up and down). How can I achieve this? I assume that some predifined mouse cursor types for such purposes exist? ---------------------------------------------------------------------- jamie · 2024-10-27 12:55 You can change the cursor during the mouseDown event of the paintbox and reset it back to default on mouse up or mousemove if the keys are not down. TPaintBox.Cursor := ??? Look at the TScreen class in the help for the list of cursors. You can also make your own custom cursors. ---------------------------------------------------------------------- Hartmut · 2024-10-27 17:56 Thanks jamie again. This 2 commands did the job: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PaintBox1.Cursor:=crSize; {quad-arrow cursor = arrow left and right and up and down}...PaintBox1.Cursor:=crDefault; {default mouse-cursor again}