Timage控件不能完全显示图片,怎么样才能让他出现左右、上下滚动条?怎样才能实现鼠标对图象的拖动?

解决方案 »

  1.   

    将timage放在其他容器控件中设置控件的滚动调,在设置timage的属性应该可以的吧
      

  2.   

    yzty(雨中太阳),li_zhifu(东北人) :两位大虾能不能说得详细点?我把Timage放到了一个TScrollBox里面,他们的Align属性都设为alclient了,可是运行的时候依然没有滚动条出现。
      

  3.   

    实现鼠标对图象的拖动
    var d:boolean;
        ox,oy:integer;procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      d:=true;
      ox:=x;
      oy:=y;
    end;procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      if d then
        ScrollBox1.ScrollBy(x-ox,y-oy);
    end;procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      d:=False;
    end;
      

  4.   

    li_zhifu(东北人):按照你的代码做了,结果只是图象抖动几下,图象并没有拖动,是不是ScrollBox1的属性还要有特别的设置?
      

  5.   

    给你一个控件:
    //////////////////////////////////////////////////////////////////
    unit PanImage;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls,Forms;type
      //TPanImage = class(TCustomControl)
      TPanImage = class(TGraphicControl)
      private
        { Private declarations }
        FBitmap : TBitmap;
        FBitmap2: TBitmap;
        FBmpName: String;    MDown: Boolean;
        OldX,OldY,NowX,NowY:Integer;
        ShowX,ShowY:Integer;    Procedure SetBMP(Value:String);
        Procedure WMLButtonDown(var msg:TWMLButtonDown); message WM_LBUTTONDOWN;
        Procedure WMMouseMove(var msg:TWMMouseMove); message WM_MOUSEMOVE;
        Procedure WMLButtonUp(var msg:TWMLButtonUp); message WM_LBUTTONUP;
      protected
        { Protected declarations }
        Procedure Paint; Override;
        Procedure DrawBMP;
        Procedure CheckPos;
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;    Property Canvas;
      published
        { Published declarations }
        Property Align;
        Property Visible;
        Property PopupMenu;
        Property OnClick;
        Property OnDblClick;
        Property OnMouseDown;
        Property OnMouseUp;
        Property OnMouseMove;    Property ShowBMPName: String Read FBmpName Write SetBMP;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('FLY', [TPanImage]);
    end;//创建
    constructor TPanImage.Create(AOwner: TComponent);
    Begin
         inherited Create(AOwner);     Width:=100;
         Height:=100;     FBitmap := TBitmap.Create;
         FBitmap2 := TBitmap.Create;
         FBitmap2.Canvas.Pen.Style:=psClear;
         MDown:=False;     FBmpName:='NO BMP';
    End;//注销对象
    destructor TPanImage.Destroy;
    Begin
         FBitmap2.Destroy;
         FBitmap.Destroy;
         inherited Destroy;
    End;//设置要显示的图片
    Procedure TPanImage.SetBMP(Value:String);
    Begin
         FBitmap.Assign(Nil);
         If Not FileExists(Value) Then
         Begin
              Refresh;
              Exit;
         End;     Try
            FBitmap.LoadFromFile(Value);
            FBmpName:=Value;
         Except
               //bmp file error
               FBmpName:='NO BMP';
         End;     ShowX:=0;
         ShowY:=0;
         MDown:=False;     Paint;
    End;//鼠标左键按下
    Procedure TPanImage.WMLButtonDown(var msg:TWMLButtonDown);
    Begin
         inherited;     NowX:=msg.XPos;
         NowY:=msg.YPos;
         msg.Result:=0;
         MDown:=True;     OldX:=NowX;
         OldY:=NowY;
    End;//检测显示位置的合理性
    Procedure TPanImage.CheckPos;
    Begin
         If (FBitMap.Width<Width) Then
            ShowX:=(Width-FBitMap.Width) Div 2
         Else
         Begin
              If ShowX>0 Then
                 ShowX:=0
              Else If (ShowX<(Width-FBitMap.Width)) Then
                 ShowX:=Width-FBitMap.Width;
         End;     If (FBitMap.Height<Height) Then
            ShowY:=(Height-FBitMap.Height) Div 2
         Else
         Begin
              If ShowY>0 Then
                 ShowY:=0
              Else If (ShowY<(Height-FBitMap.Height)) Then
                 ShowY:=Height-FBitMap.Height;
         End;
    End;//鼠标移动
    Procedure TPanImage.WMMouseMove(var msg:TWMMouseMove);
    Var MoveByX,MoveByY:Integer;
    Begin
         inherited;     //如果 BMP 为空 则退出
         If FBmpName='NO BMP' Then Exit;
         If Not MDown Then Exit;     NowX:=msg.XPos;
         NowY:=msg.YPos;
         msg.Result:=0;     //显示新的图
         MoveByX:=NowX-OldX;
         MoveByY:=NowY-OldY;     //修改显示位置 ShowX,ShowY
         ShowX:=ShowX+MoveByX;
         ShowY:=ShowY+MoveByY;
         CheckPos;     DrawBMP;     OldX:=NowX;
         OldY:=NowY;
    End;//鼠标左键松开
    Procedure TPanImage.WMLButtonUp(var msg:TWMLButtonUp);
    Begin
         inherited;
         MDown:=False;
    End;//重新显示
    Procedure TPanImage.Paint;
    Begin
         //inherited;     //如果 BMP 为空 则退出
         If FBmpName='NO BMP' Then Exit;     //Canvas.Rectangle(ClientRect);
         CheckPos;
         DrawBMP;
    End;Procedure TPanImage.DrawBMP;
    Begin
         //如果 BMP 为空 则退出
         If FBmpName='NO BMP' Then Exit;     FBitmap2.Width:=Width;
         FBitMap2.Height:=Height;
         FBitmap2.Canvas.Rectangle(ClientRect);
         FBitmap2.Canvas.Draw(ShowX,ShowY,FBitMap);     Canvas.CopyRect(ClientRect,FBitmap2.Canvas,ClientRect);     //Canvas.Draw(ShowX,ShowY,FBitMap);
    End;end.
    //////////////////////////////////////////////////////////////////