谁有好办法实现用鼠标在form上拖动字符(label)?(75分)

  • 主题发起人 主题发起人 vic
  • 开始时间 开始时间
V

vic

Unregistered / Unconfirmed
GUEST, unregistred user!
如题。<br>希望越简单越好。^-^
 
在mousemove中<br>判断鼠标是否按下,<br>记下每次mouse的位置,用Textout写字符,<br>下一次同样不过先抹去前一次的字符串。<br>
 
把Label的DragMode设为dmAutomatic,DragKind设为dkDrag,Form的DockSite设为True,<br>Form的OnDragOver事件可这么写:<br><br>procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;<br> State: TDragState; var Accept: Boolean);<br>begin<br>&nbsp; ......<br>&nbsp; with Label1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Parent:=Form1;<br>&nbsp; &nbsp; Left:=x;<br>&nbsp; &nbsp; Top:=y;<br>&nbsp; end;<br>&nbsp; Accept:=True;<br>&nbsp; ......<br>end;(鼠标形状不好看,自己解决吧)<br><br><br>另一个办法:<br>procedure TForm1.Label1MouseDown(Sender: TObject; Button: &nbsp; &nbsp; TMouseButton;Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; Label1.Tag:=1;<br>end;<br><br>procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; Label1.Tag:=0;<br>end;<br><br>procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);<br>Var<br>&nbsp; P:TPoint;<br>begin<br>&nbsp; with Label1 do<br>&nbsp; &nbsp; if Tag=1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; P.x:=X;<br>&nbsp; &nbsp; &nbsp; P.y:=Y;<br>&nbsp; &nbsp; &nbsp; Left:=Form1.ScreenToClienT(ClientToScreen(P)).X;<br>&nbsp; &nbsp; &nbsp; Top:=Form1.ScreenToClienT(ClientToScreen(P)).Y;<br>&nbsp; &nbsp; end;<br>end;<br><br>
 
var SrcX,Srcy:integer<br>//先定义2个全局变量,用于纪录x,y<br><br>procedure Tform1.LabelMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; label1.tag:=1;<br>&nbsp; SrcX:=x;<br>&nbsp; SrcY:=y;;<br>end;<br><br>procedure Tform1.LabelMouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);<br>begin<br>&nbsp; If label1.tag=1 then //MouseUp的时候,不允许移动<br>&nbsp; &nbsp; label1.tag:=0;<br>end;<br><br>procedure Tform1.labelMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);<br>begin<br>&nbsp; If label1.tag=1 Then //如果允许移动<br>&nbsp; begin<br>&nbsp; &nbsp; label1.Left := X-Srcx+label1.Left;<br>&nbsp; &nbsp; label1.Top := Y-SrcY+label1.Top;<br>&nbsp; End;<br>end;<br><br><br><br>
 
多人接受答案了。
 
后退
顶部