效果如我们在窗体中自由拖放一样
看看大家列出的方法中谁的最简单

解决方案 »

  1.   

    TEdit的没有做过,那东西有点不是太好做估计.以前只做过Panel的.
    unit Unit_DragingPanel;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;type
      TMoveType=(mtNone,mtPosition,mtSize);
      TResizeType=(rtNone,rtLT,rtRT,rtLB,rtRB,rtLeft,rtTop,rtBottom,rtRight);
      TCustomDriagingPanel = class(TPanel)
      private
        Old_Position:TPoint;
        MoveType:TMoveType;
        ResizeType:TResizeType;
        minWidth:Integer;
        minHeight:Integer;
        OldRect:TRect;
        procedure Mouse_Cursor(var Msg:TMessage);
        procedure Mouse_Position(var Msg:TMessage);
        procedure Mouse_Resize(var Msg:TMessage);
        function CheckPosition(const Position :TPoint):TResizeType;
      protected
        procedure WndProc(var Message: TMessage); override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy;override;
      end;
    implementationconstructor TCustomDriagingPanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      minWidth:=9;
      minHeight:=9;
    end;destructor TCustomDriagingPanel.Destroy;
    begin
      inherited Destroy;
    end;function TCustomDriagingPanel.CheckPosition(const Position:TPoint):TResizeType;
    var
      tmpPos:TPoint;
    begin
      tmpPos:=self.ClientToParent(Position,Parent);
      Result:=rtNone;
      case MoveType of
        mtNone:
          begin
            if  tmpPos.X<=Left+3 then //Left
            begin
              if tmpPos.Y<=Top+3 then //LeftTop
                Result:=rtLT
              else
                 if tmpPos.Y>=Top+Height-3 then //LeftBottom
                    Result:=rtLB
                 else {Left}
                    Result:=rtLeft;
            end
            else
              if tmpPos.X>=Left+Width-3 then //Right
              begin
                if tmpPos.Y<=Top+3 then //RightTop
                   Result:=rtRT
                else
                  if tmpPos.Y>=Top+Height-3 then
                     Result:=rtRB
                  else
                     Result:=rtRight;
                
              end
              else {Top or Bottom?}
                if tmpPos.Y<=Top+3 then //Top
                   Result:=rtTop
                else
                  if tmpPos.Y>=Top+Height-3 then //Bottom
                     Result:=rtBottom;
                  //else {None}
          end;
        mtPosition:
          begin      end;
        mtSize:
          begin      end;
      end;end;procedure TCustomDriagingPanel.Mouse_Cursor(var Msg:TMessage);
    var
      newPos,tmpPos:TPoint;
    begin
      GetCursorPos(newPos);
      tmpPos:=ScreenToClient(newPos);
      case CheckPosition(tmpPos) of
        rtNone:
          Screen.Cursor:=crDefault;
        rtLT:
          Screen.Cursor:=crSizeNWSE;
        rtRT:
          Screen.Cursor:=crSizeNESW;
        rtLB:
          Screen.Cursor:=crSizeNESW;
        rtRB:
          Screen.Cursor:=crSizeNWSE;
        rtLeft:
          Screen.Cursor:=crSizeWE;
        rtTop:
          Screen.Cursor:=crSizeNS;
        rtBottom:
          Screen.Cursor:=crSizeNS;
        rtRight:
          Screen.Cursor:=crSizeWE;
      end;
    end;procedure TCustomDriagingPanel.Mouse_Position(var Msg:TMessage);
    var
      newPos,tmpPos:TPoint;
    begin
      GetCursorPos(newPos);
      Screen.Cursor := crSize;
      tmpPos.X {Left} := Left - old_Position.X + newPos.X;
      tmpPos.Y {Top} := Top - old_Position.Y + newPos.Y;  if tmpPos.X<0 then
        Left:=0
      else
        begin
          Left:=tmpPos.X;
          tmpPos.X:=0;
        end;  if tmpPos.Y<0 then
        Top:=0
      else
        begin
          Top:=tmpPos.Y;
          tmpPos.Y:=0;
        end;  newPos.X:=newPos.X-tmpPos.X;
      newPos.Y:=newPos.Y-tmpPos.Y;
      old_Position := newPos;
    end;
      

  2.   

    procedure TCustomDriagingPanel.Mouse_Resize(var Msg:TMessage);
    var
      newPos:TPoint;
      tmpRect:TRect;
      AdjPos:TPoint;
    begin
      GetCursorPos(newPos);
      tmpRect:=OldRect;
      AdjPos.X:=0;
      AdjPos.Y:=0;
      case ResizeType of
        rtNone: ;
        rtLT:
          begin
            tmpRect.Left:=tmpRect.Left-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X+AdjPos.X;
              tmpRect.Left:=tmpRect.Left+AdjPos.X;
            end;        tmpRect.Top:=tmpRect.Top - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y+AdjPos.Y;
              tmpRect.Top:=tmpRect.Top+AdjPos.Y;
            end;
          end;
        rtRT:
          begin
            tmpRect.right:=tmpRect.right-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X-AdjPos.X;
              tmpRect.right:=tmpRect.Right-AdjPos.X;
            end;        tmpRect.Top:=tmpRect.Top - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y+AdjPos.Y;
              tmpRect.Top:=tmpRect.Top+AdjPos.Y;
            end;
          end;
        rtLB:
          begin
            tmpRect.Left:=tmpRect.Left-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X+AdjPos.X;
              tmpRect.Left:=tmpRect.Left+AdjPos.X;
            end;        tmpRect.Bottom:=tmpRect.Bottom - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y-AdjPos.Y;
              tmpRect.Bottom:=tmpRect.Bottom-AdjPos.Y;
            end;
          end;
        rtRB:
          begin
            tmpRect.right:=tmpRect.right-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X-AdjPos.X;
              tmpRect.right:=tmpRect.Right-AdjPos.X;
            end;        tmpRect.Bottom:=tmpRect.Bottom - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y-AdjPos.Y;
              tmpRect.Bottom:=tmpRect.Bottom-AdjPos.Y;
            end;
          end;
        rtLeft:
          begin
            tmpRect.Left:=tmpRect.Left-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X+AdjPos.X;
              tmpRect.Left:=tmpRect.Left+AdjPos.X;
            end;
          end;
        rtTop:
          begin
            tmpRect.Top:=tmpRect.Top - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y+AdjPos.Y;
              tmpRect.Top:=tmpRect.Top+AdjPos.Y;
            end;
          end;
        rtBottom:
          begin
            tmpRect.Bottom:=tmpRect.Bottom - old_Position.Y + newPos.Y;
            AdjPos.Y:=tmpRect.Bottom-tmpRect.Top-minHeight;
            if AdjPos.Y<0 then
            begin
              newPos.Y:=newPos.Y-AdjPos.Y;
              tmpRect.Bottom:=tmpRect.Bottom-AdjPos.Y;
            end;
          end;
        rtRight:
          begin
            tmpRect.right:=tmpRect.right-old_Position.X + newPos.X;
            AdjPos.X:=tmpRect.Right-tmpRect.Left-minWidth;
            if AdjPos.X<0 then
            begin
              newPos.X:=newPos.X-AdjPos.X;
              tmpRect.right:=tmpRect.Right-AdjPos.X;
            end;
          end;
      end;  OldRect:=tmpRect;
      Old_Position:=NewPos;
      self.BoundsRect:=OldRect;
    end;procedure TCustomDriagingPanel.WndProc(var Message: TMessage);
    var
      tmpPos:TPoint;
    begin
      try
        case Message.msg of      WM_MOUSEMOVE: //鼠标移动
            begin
              case self.MoveType of
                mtNone:
                  begin
                    Mouse_Cursor(Message);//检测是否需要更换鼠标指针,当鼠标到达边缘时为支持大小调整而改变鼠标形状
                  end;
                mtPosition:
                  begin
                    Mouse_Position(Message);//调整位置
                  end;
                mtSize:
                  begin
                    Mouse_Resize(Message);//调整大小
                  end;
              end;
            end;      WM_MOUSELEAVE://鼠标移开
            begin
              if self.MoveType=mtNone then
                 screen.Cursor:=crDefault;
            end;      WM_CAPTURECHANGED:
            begin        end;      WM_LBUTTONUP:
            begin
              Screen.Cursor := crDefault;
              ReleaseCapture;
              MoveType := mtNone;
            end;
          WM_LBUTTONDOWN:
            begin
              if self.MoveType=mtNone then
              begin
                GetCursorPos(Old_Position);
                tmpPos:=ScreenToClient(Old_Position);
                self.ResizeType:=CheckPosition(tmpPos);
                if self.ResizeType=rtNone then
                   self.MoveType:=mtPosition
                else
                begin
                   self.MoveType:=mtSize;
                   OldRect:=self.BoundsRect;
                end;
                SetCapture(self.Handle);          end;
            end;
            CM_MOUSEENTER:
              begin
                self.BringToFront;
              end;
        end;
      except  end;
      inherited WndProc(Message);
    end;end.
      

  3.   

    edit1.perform(WM_SYSCOMMAND,$F008,0);   
      

  4.   

    用edit1.perform(WM_SYSCOMMAND,$F008,0);
    然后在控件的OnMouseUp事件中获取它的Left和Top并保存下来,窗体Create的时候设置这个控件的Left和Top为保存下来的值即可(可以在这里判断Left、Top是否超过了0或Form的ClientWidth和ClientHeight)。用不着那么麻烦。
      

  5.   

    var
      strleft:string;
      strtop:string;  
    ...
    ...
    begin
    然后记录下来放到一个文件里或注册表里
     
    注意:
    启动的时候读取最后的这个值...'''这个在你载入的时候读取这样是最简单的了
      

  6.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        procedure Edit1MouseMove(Sender: TObject; Shift: TShiftState; X,
         Y: Integer);
        procedure Edit1MouseDown(sender: tobject; button: tmousebutton;
          Shift: tshiftstate; x, y: integer);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      if (x>=0)and(x<=5) then
      begin
        if (y>=0)and(y<=5) then Edit1.cursor:=crsizenwse;
        if (y>5)and(y<Edit1.height-5) then Edit1.cursor:=crsizewe;
        if (y>=Edit1.height-5)and(y<=Edit1.height) then Edit1.cursor:=crsizenesw;
      end
      else if (x>5)and(x<Edit1.width-5) then
      begin
        if (y>=0)and(y<=5) then Edit1.cursor:=crsizens;
        if (y>5)and(y<Edit1.height-5) then Edit1.cursor:=crarrow;
        if (y>=Edit1.height-5)and(y<=Edit1.width) then Edit1.cursor:=crsizens;
      end
      else if (x>=Edit1.width-5)and(x<=Edit1.width) then
      begin
        if (y>=0)and(y<=5) then Edit1.cursor:=crsizenesw;
        if (y>5)and(y<Edit1.height-5) then Edit1.cursor:=crsizewe;
        if (y>=Edit1.height-5)and(y<=Edit1.width) then Edit1.cursor:=crsizenwse;
      end;
    end;
    procedure tform1.Edit1MouseDown(sender: tobject; button: tmousebutton;
      shift: tshiftstate; x, y: integer);
    begin
      releasecapture;
      if (x>=0)and(x<=5) then
      begin
        //左上角方向改变大小
        if (y>=0)and(y<=5) then Edit1.perform(wm_syscommand,$f004,0);
        //左侧
        if (y>5)and(y<Edit1.height-5) then Edit1.perform(wm_syscommand,$f001,0);
        //左下角
        if (y>=Edit1.height-5)and(y<=Edit1.height) then Edit1.perform(wm_syscommand,$f007,0);
      end
      else if (x>5)and(x<Edit1.width-5) then
      begin
        //上侧
        if (y>=0)and(y<=5) then Edit1.perform(wm_syscommand,$f003,0);
        //移动控件
        if (y>5)and(y<Edit1.height-5) then Edit1.perform(wm_syscommand,$f012,0);
        //下侧
        if (y>=Edit1.height-5)and(y<=Edit1.width) then Edit1.perform(wm_syscommand,$f006,0);
      end
      else if (x>=Edit1.width-5)and(x<=Edit1.width) then
      begin
        //右上角
        if (y>=0)and(y<=5) then Edit1.perform(wm_syscommand,$f005,0);
        //右侧
        if (y>5)and(y<Edit1.height-5) then Edit1.perform(wm_syscommand,$f002,0);
        //右下角
        if (y>=Edit1.height-5)and(y<=Edit1.width) then Edit1.perform(wm_syscommand,$f008,0);
      end;
    end;end.
      

  7.   

    要最简单吗?去盒子下一个CnPack包,里面有一个现成的控件。