怎么样可以在Image上拖动显示的图片??图片的分辨率是大于屏幕的!!

解决方案 »

  1.   

    定义了:
        Origin    : TPoint;
        Image_Left: Integer;
        Image_Top : Integer;
        Visa1     : TPoint;
        Visa2     : TPoint;
        CanMove   : Boolean;procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      if (Image1.Width <= Panel2.Width) and (Image1.Height <= Panel1.Height) then
      begin
        CanMove:=false;
        exit;
      end;
      if Button=mbLeft then
      begin
        Origin.X:= X;
        Origin.Y:= Y;
        Image_Left:= Image1.Left;
        Image_Top := Image1.Top;
        Visa1.X:= X-(Image1.Width - Panel1.Width + Image1.Left);
        Visa1.y:= Y-(Image1.Height - Panel1.Height + Image1.Top);
        Visa2.x:= X-Image1.left;
        Visa2.y:= Y-Image1.top;
        CanMove:= true;
      end;
    end;procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
    begin
      if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then
      if CanMove then
      begin
         if X < Visa1.X then X:= Visa1.X;
         if X > Visa2.X then X:= Visa2.X;
         if Y < Visa1.Y then Y:= Visa1.Y;
         if Y > Visa2.Y then Y:= Visa2.Y;
         if Image1.Left <= Panel1.Left then
           Image1.Left:= Image_Left+(X-Origin.X);
         if Image1.Top <= Panel1.Top then
           Image1.Top:= Image_top+(Y-Origin.y);
      end;
    end;procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      CanMove:=false;
    end;这样写不行,Image上的图片不能跟鼠标移动。
      

  2.   

    是这样的我在 ScrollBox 上加了 Panel 和 Image 
    Image 上的图片大于 Panel 的尺寸想让 Image 上图片可以跟随鼠标拖动。
    我在 Image 上写了:
    在  private  定义了:
        Origin    : TPoint;
        Image_Left: Integer;
        Image_Top : Integer;
        Visa1     : TPoint;
        Visa2     : TPoint;
        CanMove   : Boolean;
    procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      if (Image1.Width <= Panel1.Width) and (Image1.Height <= Panel1.Height) then
      begin
        CanMove:=false;
        exit;
      end;
      if Button=mbLeft then
      begin
        Origin.X:= X;
        Origin.Y:= Y;
        Image_Left:= Image1.Left;
        Image_Top := Image1.Top;
        Visa1.X:= X-(Image1.Width - Panel1.Width + Image1.Left);
        Visa1.y:= Y-(Image1.Height - Panel1.Height + Image1.Top);
        Visa2.x:= X-Image1.left;
        Visa2.y:= Y-Image1.top;
        CanMove:= true;
      end;
    end;procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
    begin
      if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then
      if CanMove then
      begin
         if X < Visa1.X then X:= Visa1.X;
         if X > Visa2.X then X:= Visa2.X;
         if Y < Visa1.Y then Y:= Visa1.Y;
         if Y > Visa2.Y then Y:= Visa2.Y;
         if Image1.Left <= Panel1.Left then
           Image1.Left:= Image_Left+(X-Origin.X);
         if Image1.Top <= Panel1.Top then
           Image1.Top:= Image_top+(Y-Origin.y);
      end;
    end;procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      CanMove:=false;
    end;这样写不行,Image上的图片不能跟鼠标移动,有谁能帮我一下。
      

  3.   

    声明全局变量
    down1:Boolean;//纪录鼠标按下
    ox,oy,nx,ny:integer;//ox,oy纪录鼠标按下时的坐标.nx,ny纪录鼠标移动时,当前的坐标procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
    down1:true;//鼠标按下
    ox:=x;
    oy:=y;
    end;procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      if down1 = True then
      begin
        nx:=x;
        ny:=y;
        if ox <> nx then
        Image1.Left:= Image1.Left + nx - ox
        else
        if oy <> ny then
        Image1.Top:= Image1.Left + ny - oy;
      end;
    end;procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      down1:=False;//只要这个条件不成立,Image1就不会随鼠标移动了
    end;
      

  4.   

    你最好把你的dfm文件一起贴出来,不然不明白你控件间的关系和属性是否与你的算法有什么冲突(感觉好像你的Panel和Image上有什么没有考虑清楚的,而且Panel好像是visible=false?),就先说说我觉得是问题的地方吧,希望对你有帮助:
    procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      // 必须是Image的宽和高都大于Panel?
      // 还是(Image1.Width <= Panel1.Width) or (Image1.Height <= Panel1.Height)?
      if (Image1.Width <= Panel1.Width) and (Image1.Height <= Panel1.Height) then
      begin
        CanMove:=false;
        exit; // 不是什么好的选择这里这样用让人头晕,用else if button == mbLeft试试。
              // 也或者先设置CanMove标致位,如果CanMove为真再用做你doMoveImage的变量准备工作。
      end;
      if Button=mbLeft then
      begin
        Origin.X:= X;
        Origin.Y:= Y;
        Image_Left:= Image1.Left;
        Image_Top := Image1.Top;
        Visa1.X:= X-(Image1.Left + Image1.Width - Panel1.Width);
        Visa1.y:= Y-(Image1.Top + Image1.Height - Panel1.Height);
        Visa2.x:= X-Image1.left;
        Visa2.y:= Y-Image1.top;
        CanMove:= true;
        // 看到这里就觉得你在算法没有想清楚的情况下就开始编码,哈哈哈
      end;
    end;procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
    begin
      // 这个if比较多余,因为在MouseDown里的时候CanMove为真的情况已经判断过下面这个布尔表达式了,so comment it
    //  if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then
      if CanMove then
      begin
         if X < Visa1.X then X:= Visa1.X;
         if X > Visa2.X then X:= Visa2.X;
         if Y < Visa1.Y then Y:= Visa1.Y;
         if Y > Visa2.Y then Y:= Visa2.Y;
         if Image1.Left <= Panel1.Left then
           Image1.Left:= Image_Left+(X-Origin.X);
         if Image1.Top <= Panel1.Top then
           Image1.Top:= Image_top+(Y-Origin.y);
      end;
    end;procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
    begin
      CanMove:=false;
    end;其实挺简单的一个问题,google找一下多如牛毛,可说难也难,比如你还要考虑操作风格的问题:1、IMAGE随着左键按下后随mouse而移动?那是否要考虑平滑?因为在Image移动的时候整个Image投影到Form上的Region都需要Refresh!那这也有好的方法暂时避开刷新的问题:2、当MouseDown的时候设置标致,在MouseMove的时候在ScrollBox里画虚框(用Frame3D),当MouseUp的时候真正的移动Image对象的坐标。
    你给示例代码没有问题,可更希望你能自己想通,因为这是算法的问题,提高会更快些,如果你要代码我可以把自己过去写的给你贴上来。