2017-06-13 64 views
-2

我有一個水平滾動條的窗體,但我想通過使用鼠標移動(水平)在窗體上滾動條來移除滾動條。 我希望隨着鼠標的移動我的表單將滾動,但沒有滯後和平穩,當我到達最後它停止滾動。因此,如果任何人都可以幫助我,那麼這將是一個巨大的幫助。 在此先感謝。德爾福 - 平滑滾動鼠標移動

+0

顯式使用滾動條還是另一個控件的一部分?你能舉一個小例子來展示你目前的工作嗎? – Dsm

+0

滾動條是表單本身的一部分,我的表單有一些額外的元素,在沒有滾動的情況下無法看到 – bluedragon

+0

除了隱藏滾動條以外*很容易。你死定了嗎? – Dsm

回答

0

只要你不隱藏滾動條,它就可以很好地在10.1柏林上移動屏幕。文檔建議,如果你隱藏滾動條,它應該可以工作,所以也許在Delphi的早期版本中它會。

使用OnMouseDown,OnMouseMove和OnMouseUp,以及3個局部變量。

unit Unit10; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.StrUtils, Vcl.Mask; 

type 
    TForm10 = class(TForm) 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Button1: TButton; 
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; 
     Shift: TShiftState; X, Y: Integer); 
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; 
     Shift: TShiftState; X, Y: Integer); 
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); 
    private 
    { Private declarations } 
    fIsDown : boolean; 
    fX, fY : integer; 
    public 
    { Public declarations } 
    end; 

var 
    Form10: TForm10; 

implementation 

{$R *.dfm} 

procedure TForm10.FormMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    if Shift = [ssLeft] then // if ONLY left down 
    begin 
    // Save co-ordinates 
    fIsDown := TRUE; 
    fX := X; 
    fY := Y; 
    end; 
end; 

procedure TForm10.FormMouseMove(Sender: TObject; Shift: TShiftState; X, 
    Y: Integer); 
begin 
    if Shift = [ssLeft] then // if ONLY left down 
    begin 
    if fIsDown then 
    begin 
     HorzScrollBar.Position := HorzScrollBar.Position + fX - X; 
     VertScrollBar.Position := VertScrollBar.Position + fY - Y; 
    end 
    else 
    begin 
     fIsDown := TRUE; 
    end; 
    fX := X; 
    fY := Y; 
    end 
    else 
    begin 
    fIsDown := FALSE; 
    end; 
end; 

procedure TForm10.FormMouseUp(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    fIsDown := FALSE; // regardless of shift state! 
end; 

end. 

請讓我們知道是否隱藏滾動條在XE8上工作,因爲這對於未來的讀者會有用。

+0

如果您將Tracking屬性設置爲True而沒有任何額外的代碼,則滾動條可以在拖動滾動條的同時移動內容。拖動不可聚焦的客戶區時,此代碼將滾動表單內容。如果這是OP想要的,或許現在是時候澄清這個問題了。 – Victoria

+0

對於你可以調用的東西_「視覺平滑滾動」_我會遵循[本文](https://stackoverflow.com/a/9503002/8041231)。 – Victoria

+0

@維多利亞。這給了真正的*「視覺上好的滾動」*,並且與鏈接中的第一個解決方案沒有什麼不同(TForm和TScrollingWindow「有相同的祖先負責滾動。第二和第三種形式是很好的選擇,但需要額外的代碼請確保您不會走得太遠..使用計時器並不能提高響應速度 – Dsm