0

DataGridView で列のドラッグ アンド ドロップ (自動スクロール付き) 機能を実装する方法を提案してください。コントロールの AllowUserToDragDrop オプションを使用できることはわかっています。ただし、私の datagridview コントロールには比較的多数の列があるため、ユーザーがドロップする前に宛先列を表示できるように、現在のドラッグ アンド ドロップ位置に追従する自動スクロール機能が必要です。カスタム ドラッグ アンド ドロップ機能を実装しましたが、自動スクロール オプションを有効にするにはまだ問題があります。

4

2 に答える 2

1

次のクラスを使用して、TTreeView を自動スクロールしています。TScroller は、それが置かれている Frame の Create で作成され、TreeView に渡されます。フレームのデストロイで破壊される。TreeView の OnDragOver では、MyDragScroller.Scroll(State); を呼び出すだけです。

type
  TScroller = class(TObject)
  private
    MyTimer: TTimer;
    FControl: TWinControl;
    FSensitiveSize: Integer;
  protected
    procedure HandleTimer(Sender: TObject);
  public
    constructor Create(aControl: TWinControl);
    destructor Destroy; override;

    procedure Scroll(const aState: TDragState);
  end;

implementation

{ TScroller }

constructor TScroller.Create(aControl: TWinControl);
begin
  inherited Create;
  MyTimer := TTimer.Create(nil);
  MyTimer.Enabled := False;
  MyTimer.Interval := 20; // Not too short, otherwise scrolling flashes by.
  MyTimer.OnTimer := HandleTimer;

  FControl := aControl;
  // Width/Height from edge of FControl within which the mouse has to be for
  // automatic scrolling to occur. By default it is the width of a vertical scrollbar.
  FSensitiveSize := GetSystemMetrics(SM_CXVSCROLL);
end;

destructor TScroller.Destroy;
begin
  FreeAndNil(MyTimer);
  FControl := nil;
  inherited;
end;

procedure TScroller.HandleTimer(Sender: TObject);
var
  MousePos: TPoint;
  MouseX: Integer;
  MouseY: Integer;

  function _MouseInSensitiveSize: Boolean;
  begin

    MousePos := FControl.ScreenToClient(Mouse.CursorPos);
    MouseY := MousePos.Y;
    MouseX := MousePos.X;

    Result :=
         ((MouseY >= 0) and (MouseY < FSensitiveSize))
      or ((MouseY > FControl.ClientHeight - FSensitiveSize) and (MouseY <= FControl.ClientHeight))
      or ((MouseX >= 0) and (MouseX < FSensitiveSize))
      or ((MouseX > FControl.ClientWidth - FSensitiveSize) and (MouseX <= FControl.ClientWidth))
    ;

  end;
begin
  if Mouse.IsDragging and _MouseInSensitiveSize then begin
    if MouseY < FSensitiveSize then begin
      FControl.Perform(WM_VSCROLL, SB_LINEUP, 0);
    end else if MouseY > FControl.ClientHeight - FSensitiveSize then begin
      FControl.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
    end;

    if MouseX < FSensitiveSize then begin
      FControl.Perform(WM_HSCROLL, SB_LINELEFT, 0);
    end else if MouseX > FControl.ClientWidth - FSensitiveSize then begin
      FControl.Perform(WM_HSCROLL, SB_LINERIGHT, 0);
    end;
  end else begin
    MyTimer.Enabled := False;
  end;
end;

procedure TScroller.Scroll(const aState: TDragState);
begin
  if not Mouse.IsDragging then Exit;  // Only scroll while dragging.
  if not (aState in [dsDragMove]) then Exit; // No use scrolling on a dsDragLeave and not nice to do so on a dsDragEnter.

  MyTimer.Enabled := True;
end;

注: 自動スクロールが必要なコントロールがさらにある場合は、コントロールごとに TScroller を作成する必要があります。その場合、ある種のオブザーバー/監視メカニズムを使用して、すべてのスクロール コントロール間でタイマーを共有すると、アプリのパフォーマンスが大幅に向上する可能性があります。

于 2010-05-29T15:13:28.827 に答える
0

OnMouseMoveを処理し、それに応じてプログラムでスクロールすることができます。

于 2010-05-20T21:08:12.803 に答える