Private //form movement; oldpos,newpos:tpoint;bmove:boolean;procedure Tform1.formmousedown (Sender:tobject; Button:tmousebutton; Shift:tshiftstate; X, Y:integer); Beginbmove:=true;oldpos:=clienttoscreen (Point (x, y)); End;procedure Tform1.formmousemove (Sender: TObject; Shift:tshiftstate; X,y:integer); beginif not bmove then Exit; Newpos:=clienttoscreen (Point (x, y)); left:=left+newpos.x-oldpos.x; Top:=top+newpos.y-oldpos.y;oldpos:=newpos;end;procedure Tform1.formmouseup (sender:tobject; Button:tmousebutton; Shift:tshiftstate; X, Y:integer); beginbmove:=false;end;
Expansion: This allows a control to be moved;
1 //variables2Ismousedown:boolean;//whether to drag or drop3Posx,posy:integer;//Drag the coordinates at the beginning4 5 procedureTform1.edt2mousedown (sender:tobject; Button:tmousebutton;6 shift:tshiftstate; X, Y:integer);7 begin8Ismousedown: = True;//tell the form I'm going to drag9PosX: = Mouse.cursorpos.x-edt2. Left;//where the record beginsTenPosY: = Mouse.cursorpos.y-edt2. Top;// One End; A - procedureTform1.edt2mousemove (sender:tobject; Shift:tshiftstate; X, - Y:integer); the begin - ifIsmousedown Then //Mouse is pressed state (to drag) - begin -Edt2. Left: = Mouse.cursorpos.x-posx;//the new location +Edt2. Top: = Mouse.cursorpos.y-PosY; - End; + End; A at procedureTform1.edt2mouseup (sender:tobject; Button:tmousebutton; - shift:tshiftstate; X, Y:integer); - begin -Ismousedown: = False;//tell the form I'm done dragging . - End;
Control Move
Implementing a form with mouse movement (control)-"developing shareware with Delphi"-15.1 Task Manager