我动态的创建了一个label控件,定义了它的onmousemove过程,想在onmousemove事件中
实现label的移动,但不知代码如何写,请各位帮一下忙。谢谢。

解决方案 »

  1.   

    通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL:
    var xpanel,ypanel,xlabel,ylabel:integer;
    PANEL的MouseMove事件:xpanel:=x;ypanel:=y;
    PANEL的DragOver事件:xpanel:=x;ypanel:=y;
    LABEL的MouseMove事件:xlabel:=x;ylabel:=y;
    LABEL的EndDrag事件:label.left:=xpanel-xlabel;label.top:=ypanel-ylabel;
      

  2.   

    不知道新建的label的name要如何写,
    ???.left
    ???.top
    ???不知道要写什么。
      

  3.   

    外加panel增加EXE文件大小,
    而且大小巨增,
    用StaticText1控件,,况且LABEL不支持句柄procedure TForm1.StaticText1MouseMove(Sender: TObject; Shift: TShiftState;
    X, Y: Integer);
    begin
    ReleaseCapture;
    form1.StaticText1.Perform(WM_SYSCOMMAND, $F012, 0);
    end;祝:身体健康,提前完工:)
      

  4.   

    unit MovePanel;interfaceuses
      Windows, Classes, Controls, ExtCtrls, Forms;type
      TMovePanel = class(TPanel)
      private
        { Private declarations }
    PrePoint: TPoint;
    Down: Boolean;
      protected
        { Protected declarations }
      public
        { Public declarations }
    constructor Create(AOwner:TComponent); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
       published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TMovePanel]);
    end;{ TMovePanel }constructor TMovePanel.Create(AOwner: TComponent);
    begin
    inherited Create(AOwner);
    end;procedure TMovePanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    begin
    if (Button = MBLeft) then
        begin
    Down := true;
            Screen.Cursor := crSizeAll;
    GetCursorPos(PrePoint);
    end; if assigned(OnMouseDown) then
         OnMouseDown(Self, Button, Shift, X, Y);
    end;procedure TMovePanel.MouseMove(Shift: TShiftState; X, Y: Integer);
    Var
    NowPoint: TPoint;
    begin
    if Down then
    begin
    GetCursorPos(NowPoint);
    Self.Parent.Left := Self.Parent.Left + NowPoint.X - PrePoint.X;
    Self.Parent.Top := Self.Parent.Top + NowPoint.Y - PrePoint.Y;
    PrePoint := NowPoint;
        end; if Assigned(OnMouseMove) then
    OnMouseMove(Self, Shift, X, Y);
    end;procedure TMovePanel.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    begin
    if (Button = MBLeft) and Down then
        begin
       Down := False;
         Screen.Cursor := crDefault;
        end; if assigned(OnMouseUp) then
    OnMouseUp(Self, Button, Shift, X, Y);
    end;end.
      

  5.   

    上面是完整的代码,
    用个TMovePanel组件
    以后就随时可以用了