How to implement the dragging of graph in client area of window?

Hi!

I’m using Delphi 5.0

I want to implement a form such that:
When I press the mouse down on the client area and move the cursor, the position of graph in client area will move following the mouse. When mouse button up, graph will stay there. (Just image how we drag the pages in adobe pdf viewer).

The cap problem I have now is don’t know how to trace the movement of mouse cursor on client area.

I know I can use OnMouseMove event to detect the movement of cursor. However, this event only detect the coordinate of cursor after movement(when event is triggered), but not before movement. So I don’t know how to implement the offset of graph (the only idea I have now is to detect how much have been drag on mouse, then apply the offset to graph).

I can use a state variable to record the last coordinate of cursor, but this may course error if the cursor leave client area from left(stop triggering), then enter client arent from right(start triggering).

Can you give me some idea?

Thank you!

OnMouseDown (or OnLeftClick, whatever) - copy coordinates of point into a variable then SetCapture() (makes sure you get mouse messages even out of client window).

OnMouseMove - use current coordiantes to work out relative movement from click point and move graph origin by same offset.

OnMouseUp - ReleaseCapture().

Try to use functions like: GetCursporPos and SetCursorPos combined with mouse events. You only have to experiment a little.

Thank you!

I tested these routines successfully as following:

First add these global variables:

var
headX,headY: Integer;
tailX, tailY: Integer;
oldX,oldY;
Drag: boolean = False;

OnMouseDown:

tailX := X;
tailY := Y;
oldX := X;
oldY := Y;
Drag := True;

OnMouseMove:

if Drag then
begin
Graph.Left := oldX + (headX - tailX);
Graph.Top := oldX + (headY - tailY);
end;

OnMouseUp:

Drag := False;