如何实现将图片周围可以调整大小的控件?
就是在图象周围可以调整大小,需要动态的创建。

解决方案 »

  1.   

    PHOTOSHOP不是有个功能吗?当你选取一个图片的某一部分的时候在选取的图片周围出现可以调节大小,表现为选择图片周围出现小箭头调整。
      

  2.   

    试试这个,程序中可能还有一些问题,欢迎交流:[email protected]
    /////代码////////////
    unit Graphic;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls,Graphics;type
      TGraphic = class(TGraphicControl)
      private
        { Private declarations }
        FPicFile:string;
        X1,Y1,X2,Y2:integer;
        FDragKind:TDragKind;
        FDragMode:TDragMode;
        Selected:Boolean;
        DragState:Integer;
        DragX,DragY:integer;
        H,W:integer;
    //    S1,s2:String;
        procedure SetPicFile(Value:String);
        procedure SetDragKind(Value:TDragKind);
    //    procedure SetDragMode(Value:TDragMode);
      protected
        { Protected declarations }
        procedure Paint; 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;
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        { Published declarations }
        property PicFile:string read FPicFile write SetPicFile;
        property DragKind:TDragKind read FDragKind write SetDragKind;
        property DragMode:TDragMode read FDragMode write SetDragMode;    property OnMouseDown;
        property OnMouseUp;
        property OnMouseMove;  end;      procedure Register;implementationprocedure TGraphic.SetDragKind(Value:TDragKind);
    begin
     FDragKind:=value;
     Invalidate;
    end;
      

  3.   

    function Between(x,x1,x2:integer):boolean;
    begin
     if (x>=x1) and (x<=x2)  then
        Between:=true
      else
        Between:=false;
    end;procedure TGraphic.MouseMove(Shift: TShiftState; X, Y: Integer);
     var p:TPoint;
    begin
        GetCurSorPos(p);
        p:=ScreenToClient(p);
     //   S2:='X:' +inttostr(P.x)+'   Y:'+Inttostr(p.y);
        if selected then
        begin
         cursor:= crArrow;
      //   DragState:=0;
      if between(x,0,6) then
       begin
        if between(y,0,6) then  //左上角
         begin
          cursor:= crSizeNWSE;
          DragState:=1;
         end;
        if between(y,round(height/2)-3,round(height/2)+3) then //中左
         begin
          cursor:= crSizeWE;
          DragState:=2;
         end;
        if between(y,height-6,height) then   //左下角
         begin
          cursor:= crSizeNESW;
          DragState:=3;
         end;
       end;
       if between(x,round(width/2)-3,round(width/2)+3) then
        begin
         if between(y,0,6) then //中上
          begin
           cursor:= crSizeNS;
           DragState:=4;
          end;
         if between(y,height-6,height) then   //中下
          begin
           cursor:= crSizeNS;
           DragState:=5;
          end;
        end;
       if between(x,width-6,width) then
        begin
         if between(y,0,2) then    //右上角
          begin
           cursor:= crSizeNESW;
           DragState:=6;
          end;
         if between(y,round(height/2)-3,round(height/2)+3) then //中右
          begin
           cursor:= crSizeWE;
           DragState:=7;
          end;
         if between (y,height-6,height) then  //右下角
          begin
           cursor:= crSizeNWSE;
           DragState:=8;
          end;
       end;
        end;
    //    Invalidate;
    end;procedure TGraphic.SetPicFile(Value:string) ;
    begin
       FPicFile:=value;
       Invalidate;
    end;procedure TGraphic.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     var P:TPoint;
    begin
     GetCurSorPos(p);
     p:=ScreenToClient(p);
     case  DragState of
       0:    //拖动
        begin
         top:=Y1+(p.Y-DragY);
         Left:=X1+(p.X-DragX);
        end;
       1:    //左上角
        begin
         if p.y>h then
          top:=y1+p.y+H-p.y
         else
          top:=y1+p.y;
         if p.x>W then
          left:=x1+p.x+W-p.X
         else
          left:=x1+p.x;
         height:=abs(H-p.y);
         width:=abs(w-p.X);
        end;
       2:   //中左
        begin
         top:=y1;
         if p.x<w then
            left:=x1+p.X
          else
            left:=x2;  
         height:=h;
         width:=abs(p.X-w);
        end;
       3:   //左下角
        begin
         if p.x>W then
          left:=x2//-w//x1-p.x-W     //here have problem=
         else
          left:=x1+p.x;
         if p.y<0 then
          top:=y1+p.Y
         else
          top:=y1;//x1+p.x;
         if p.y<0 then
          height:=abs(p.Y)
         else
          height:=abs(p.Y);
         width:=abs(w-p.X);
        end;
       4:  //中上
        begin
         left:=x1;
         if p.y<H then
            top:=y1+p.y
          else
            top:=y2;
         height:=abs(p.Y-h);
         width:=w;
        end;
       5:  //中下
        begin
         left:=x1;
         if p.y<0 then
            top:=y1+p.y
          else
            top:=y1;
         if p.y<0 then
            height:= abs(p.y)
          else
            height:=p.Y ; 
         width:=w;
        end;
       6: //右上角
        begin
         if p.x>0 then
          left:=x1
         else
          left:=(x1+p.X);
         if p.y>H then
          top:=y2
         else
          top:=y1+p.Y;
         height:=abs(h-p.Y);
         width:=abs(p.X);
         
        end;
       7: //中右
        begin
         if p.x>0 then
          left:=x1
         else
          left:=abs(x1+p.X);
         top:=y1;
         height:=h;
         if p.x>0 then
          width:=p.X
         else
          width:= abs(p.X);
        end;
       8: //右下角
        begin
         top:=y1;
         if p.x>0 then
          left:=x1
         else
          left:=abs(x1+p.x);
          
         width:=abs(p.X);
         height:=abs(p.y);
        end;
     end;
    end;procedure TGraphic.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     var P:TPoint;
    begin
      Selected:=true;
      DragState:=0;
      if between(x,0,6) then
       begin
        if between(y,0,6) then  //左上角
         begin
          cursor:= crSizeNWSE;
          DragState:=1;
         end;
        if between(y,round(height/2)-3,round(height/2)+3) then //中左
         begin
          cursor:= crSizeWE;
          DragState:=2;
         end;
        if between(y,height-6,height) then   //左下角
         begin
          cursor:= crSizeNESW;
          DragState:=3;
         end;
       end;
       if between(x,round(width/2)-3,round(width/2)+3) then
        begin
         if between(y,0,6) then //中上
          begin
           cursor:= crSizeNS;
           DragState:=4;
          end;
         if between(y,height-6,height) then   //中下
          begin
           cursor:= crSizeNS;
           DragState:=5;
          end;
        end;
       if between(x,width-6,width) then
        begin
         if between(y,0,2) then    //右上角
          begin
           cursor:= crSizeNESW;
           DragState:=6;
          end;
         if between(y,round(height/2)-3,round(height/2)+3) then //中右
          begin
           cursor:= crSizeWE;
           DragState:=7;
          end;
         if between (y,height-6,height) then  //右下角
          begin
           cursor:= crSizeNWSE;
           DragState:=8;
          end;
       end;//   Top1:=top;
    //   left1:=left;
    //  height1:=height;
    //   width1:=width;
       GetCurSorPos(p);
       p:=ScreenToClient(p);
       DragX:=p.X;
       DragY:=p.Y;
       X1:=left;//p.x;
       Y1:=top;//p.y;
       X2:=X1+Width;
       Y2:=Y1+Height;
       H:=Height;
       W:=Width;
    end;procedure TGraphic.Paint ;
      var Bitmap : TBitMap;
      //    BitRect:TRect;
          ARect: TRect;
    begin
      with inherited Canvas do
        begin
         Brush.Style := bsSolid;
         brush.Color :=clWhite;
       //  brush.
         //Rectangle(0,0,width,height);
         Rectangle(2,2,width-2,height-2);
       //  textout(100,100,s1);
       //  textout(100,120,S2);
         if fileexists(PicFile) then
          begin
    //       Bitmap := TBitmap.Create;
    //       bitmap.LoadFromFile(PicFile);
           ARect := Rect(0,0,width,height);
           ARect.Left := 0;
           ARect.Top := 0;
           Bitmap := TBitmap.Create;
           bitmap.LoadFromFile(PicFile);
           Canvas.Brush.Bitmap := Bitmap;
           Canvas.FillRect(ARect);
           Canvas.StretchDraw(ARect,Bitmap);   //    bitrect.Left :=0;
       //    bitrect.Top :=0;
       //    bitrect.Right:=width;
       //    bitrect.Bottom :=height;
         //  bitmap.Canvas.StretchDraw(Rect(0,0,width-1,height-1),picfile);
          canvas.StretchDraw(rect(0,0,Width,Height),bitmap );
      //    CopyRect(rect(0,0,Width,Height),bitmap.Canvas,rect(0,0,bitmap.Width,bitmap.Height) );
    //         with Image.Canvas do
     // begin
     //   CopyMode := cmWhiteness;
     //   ARect := Rect(0, 0, Image.Width, Image.Height);
      //  CopyRect(ARect, Image.Canvas, ARect);
      //  CopyMode := cmSrcCopy; { restore the copy mode }
       //    bitmap.Transparent:= True;
        //   bitmap.TransParentColor := BitMap.Canvas.Brush.Color;
        //   bitmap.TransparentMode := tmAuto;
    //       bitmap.
         //  Draw(0,0,BitMap);
          end;   if selected then
        begin
         Brush.Style := bsSolid;
         brush.Color :=clBlack;
         canvas.FillRect(rect(0,0,6,6));     //左上角
         canvas.FillRect(rect(width-6,0,width,6));      //右上角
         canvas.FillRect(rect(0,height-6,6,height));       //左下角
         canvas.FillRect(rect(width-6,height-6,width,height));     //右下角
         canvas.FillRect(rect(Round(width/2)-3,0,Round(width/2)+3,6));  //中上
         canvas.FillRect(rect(Round(width/2)-3,height-6,Round(width/2)+3,height)); //中下
         canvas.FillRect(rect(0,Round(height/2)-3,6,Round(height/2)+3));     //中左
         canvas.FillRect(rect(width-6,Round(height/2)-3,width+3,Round(height/2)+3)) //中右
        end;
      end;
    end;constructor TGraphic.Create(AOwner: TComponent);
    begin
      inherited Create (AOwner);
     // ZoomMode:=0;
      DragState:=0;
      Selected:=false;
    end;destructor TGraphic.Destroy;
    begin
      inherited Destroy;
    end;procedure Register;
    begin
      RegisterComponents('Samples', [TGraphic]);
    end;end.
      

  4.   

    to: zytao
    这段代码工作得很好,但是为什么移动速度比较慢
    另外,如果希望移动和改变大小的时候有一个虚线框该如何实现呢?