在窗体中放如一个Tpanel类控件。panel1,再将Tshape类的控件shap1放入不panel1中。
想实现,鼠标进入shape1中,点住,即可拖动.求教高手,如何做到。

解决方案 »

  1.   

    http://www.cnread.net/cnread1/dnwl/cxsj/delphi/jcjc/023.htm
      

  2.   

    //==============================================================================
    //任意摆布一个控件(拖动、放大、缩小)******************************************
    //==============================================================================
    procedure ManipulateControl(Control: TControl; Shift: TShiftState; X, Y, Precision: integer);
    var SC_MANIPULATE: Word;
    begin
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的最左侧**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           if (X<=Precision) and (Y>Precision) and (Y<Control.Height-Precision)
      then begin
             SC_MANIPULATE  := $F001;
             Control.Cursor := crSizeWE;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的最右侧**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>=Control.Width-Precision) and (Y>Precision) and (Y<Control.Height-Precision)
      then begin
             SC_MANIPULATE  := $F002;
             Control.Cursor := crSizeWE;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的最上侧**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>Precision) and (X<Control.Width-Precision) and (Y<=Precision)
      then begin
             SC_MANIPULATE  := $F003;
             Control.Cursor := crSizeNS;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的左上角**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X<=Precision) and (Y<=Precision)
      then begin
             SC_MANIPULATE  := $F004;
             Control.Cursor := crSizeNWSE;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的右上角**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>=Control.Width-Precision) and (Y<=Precision)
      then begin
             SC_MANIPULATE  := $F005;
             Control.Cursor := crSizeNESW    ;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的最下侧**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>Precision) and (X<Control.Width-Precision) and (Y>=Control.Height-Precision)
      then begin
             SC_MANIPULATE  := $F006;
             Control.Cursor := crSizeNS;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的左下角**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X<=Precision) and (Y>=Control.Height-Precision)
      then begin
             SC_MANIPULATE  := $F007;
             Control.Cursor := crSizeNESW;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的右下角**********************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>=Control.Width-Precision) and (Y>=Control.Height-Precision)
      then begin
             SC_MANIPULATE  := $F008;
             Control.Cursor := crSizeNWSE;
           end
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //光标在控件的客户区(移动整个控件)******************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      else if (X>5) and (Y>5) and (X<Control.Width-5) and (Y<Control.Height-5)
      then begin
             SC_MANIPULATE  := $F009;
             Control.Cursor := crSizeAll;
           end
      else begin
             SC_MANIPULATE := $F000;
             Control.Cursor := crDefault;
           end;
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      if Shift=[ssLeft] then
      begin
        ReleaseCapture;
        Control.Perform(WM_SYSCOMMAND, SC_MANIPULATE, 0);
      end;  
    end;example://~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    procedure TForm_Main.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    begin
      Caption := IntToStr(X) + '/' + IntToStr(Y);
      ManipulateControl((Sender as TControl), Shift, X, Y, 10);
    end;
    10为精度
      

  3.   

    baiyu123(Easy come Easy go) :你给的代码。我试了一下。用在Tshape控件上不行。动都动不了。
      

  4.   

    var
      MoveEn: Boolean;
      XY: TPoint;implementation{$R *.dfm}procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      with Shape1 do
        if MoveEn then
        begin
          Left := Left + (x - XY.x);
          Top := Top + (y - XY.y);
        end;end;procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        MoveEn := True;
        XY.x := x;
        XY.y := y;
      end;end;procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      MoveEn := False;end;