JohnLM 14 Posted September 20 (edited) I am having trouble figuring this one out. I have a form with a tscrollbox and an timage. In the image1MouseMove event of the timage I have the following code; procedure TForm2.im1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin scrollbox1.vertScrollBar.Position := x; scrollbox1.horzScrollBar.Position := y; end; But it is not what I'd expect. What I am trying to do: 1. Lets say I have screenshot of my desktop and inserted into the timage. 2. And I resize the app's form smaller 3. Using the mouse, I want to click on the image and drag (or pan) around inside the form's scrollbox area so that I can see other parts of the image. What am I doing wrong and how can I fix this? TIA Edited September 21 by JohnLM Share this post Link to post
Lajos Juhász 293 Posted September 21 You would like to move the scrollbar by the ammount the user have move the mouse to do so you will need two variables (private fields) that stores the previous mouse coordinates: procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin fPRevX:=X; fPrevY:=Y; end; Now when the user moves the mouse while the left button is pressed we can calculate the new position for the scrollbars: procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if ssLeft in Shift then begin scrollbox1.vertScrollBar.Position := scrollbox1.vertScrollBar.Position+fPrevY-Y; scrollbox1.horzScrollBar.Position := scrollbox1.horzScrollBar.Position+fPRevX-X; fPrevX:=X; fPrevY:=y; end; end; 1 Share this post Link to post
Anders Melander 1784 Posted September 21 10 hours ago, Lajos Juhász said: You would like to move the scrollbar by the ammount the user have move the mouse No, don't do that; It would accumulate the various errors there is in coordinate system conversion, mouse imprecision, etc. Also, the example just doesn't work. Instead remember the starting mouse position and adjust the scrollbar position with the difference between the starting mouse position and the current position: type TForm1 = class(TForm) [...] private FStartPos: TPoint; end; procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin FStartPos.X := X; FStartPos.Y := Y; end; procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var DeltaMouse: TPoint; begin if (ssLeft in Shift) then begin // How much has the mouse moved since we started the drag? DeltaMouse := Point(FStartPos.X - X, FStartPos.Y - Y); // Reposition scrollbars (i.e. pan the image) ScrollBox1.HorzScrollBar.Position := ScrollBox1.HorzScrollBar.Position + DeltaMouse.X; ScrollBox1.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position + DeltaMouse.Y; end; end; 1 Share this post Link to post
JohnLM 14 Posted September 21 Thank you @Lajos Juhász for your suggestion. I had tried a few mods late last night but I could not get it to work any better. And thank you @Anders Melander for your suggestion. It is working the way I was hoping. And, I learned a little bit about Delta. Thank you both for your help, it is appreciated! Share this post Link to post