我在form上有一scrollbox
scrollbox里有一image  我想让鼠标靠近form边缘时scrollbox的滚动条自动移动(form的formstyle为stayontop)

解决方案 »

  1.   

    在form的MouseMove中
    if x<20 then
    begin
     while (Image1.Width-SCrollBox1.HorzScrollBar.Position)>205 do
     begin
       for i:=0 to 10000000 do;
       SCrollBox1.HorzScrollBar.Position:=SCrollBox1.HorzScrollBar.Position+2;
     end;
    end;
      

  2.   

    //参考如下代码~~
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Tabs, jpeg, ExtCtrls, Buttons;type
      TForm1 = class(TForm)
        Scrollbox1: TScrollBox;
        Image1: TImage;
        Timer1: TTimer;
        procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure Timer1Timer(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        FAnchors: TAnchors;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses Math;procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    const
      cSpace = 10;
    var
      I, J, K: Integer;
    begin
      FAnchors := [];
      J := 0;
      K := 0;
      with TControl(Sender) do
      begin
        I := Abs(X + Left);
        if I <= cSpace then
        begin
          J := J + I;
          Inc(K);
          Include(FAnchors, akLeft);
        end;
        I := Abs(Y + Top);
        if I <= cSpace then
        begin
          J := J + I;
          Inc(K);
          Include(FAnchors, akTop);
        end;
        I := Abs(Parent.ClientWidth - X - Left);
        if I <= cSpace then
        begin
          J := J + I;
          Inc(K);
          Include(FAnchors, akRight);
        end;
        I := Abs(Parent.ClientHeight - Y - Top);
        if I <= cSpace then
        begin
          J := J + I;
          Inc(K);
          Include(FAnchors, akBottom);
        end;
      end;
      if K > 0 then J := J div K;
      Timer1.Enabled := FAnchors <> [];
      Timer1.Interval := Trunc(50 * (J / cSpace));
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    var
      vOffset: TPoint;
    begin
      vOffset := Point(0, 0);
      if akTop in FAnchors then vOffset.Y := -1;
      if akLeft in FAnchors then vOffset.X := -1;
      if akRight in FAnchors then vOffset.X := +1;
      if akBottom in FAnchors then vOffset.Y := +1;
      Scrollbox1.HorzScrollBar.Position :=
        Scrollbox1.HorzScrollBar.Position + vOffset.X;
      Scrollbox1.VertScrollBar.Position :=
        Scrollbox1.VertScrollBar.Position + vOffset.Y;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Timer1.Interval := 50;
      Timer1.Enabled := False;
    end;end.