Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#8

Re: Stringgrid: Ganze Zeilen mit der Maus verschieben

  Alt 17. Sep 2008, 21:57
Hallo,

Müsste in etwa so funktionieren:

( Stringgrid1.DragMode := dmManual; )


Delphi-Quellcode:
type
  TForm1 = class(TForm)
    Stringgrid1: TStringGrid;
    procedure Stringgrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Stringgrid1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Stringgrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FSourceRow: Integer;
  public
    { Public declarations }
  end;

// Zugriff auf protected MoveRow
type
  TStringGridHack = class(TStringGrid)
  public
    procedure MoveRow(FromIndex, ToIndex: Longint);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TStringGridHack.MoveRow(FromIndex, ToIndex: Integer);
begin
  inherited;
end;

procedure TForm1.Stringgrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  SourceCol : Integer;
begin
  // Zeile ermitteln
  (Sender as TStringgrid).MouseToCell(X, Y, SourceCol, FSourceRow);
  if (FSourceRow > 0) then
    (Sender as TStringgrid).BeginDrag(False, 4); // Drag starten
end;

procedure TForm1.Stringgrid1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var
  Col, Row: Integer;
begin
  (Source as TStringgrid).MouseToCell(X, Y, Col, Row);
  Accept := (Sender = Source) and (Row > 0);
end;

procedure TForm1.Stringgrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  DestCol, DestRow: Integer;
begin
  // Ziel Zeile ermitteln
  Stringgrid1.MouseToCell(X, Y, DestCol, DestRow);
  if (FSourceRow <> DestRow) then
  begin
     // Zeile FSourceRow nach DestRow verschieben
     TStringGridHack(Source as TStringgrid).MoveRow(FSourceRow, DestRow);
  end;
end;
Thomas
  Mit Zitat antworten Zitat