我要实现用鼠标选择多个单元格,然后拖动,使单元格中的内容移动,怎么实现?
当用鼠标在StringGrid上拖动选择多个单元格,如何判断出哪几个单元格被选中?

解决方案 »

  1.   

    试试下面的代码
    for i:=StringGrid1.Selection.Left to StringGrid1.Selection.Right  do
         for j:=StringGrid1.Selection.Top to StringGrid1.Selection.Bottom  do
            StringGrid1.Cells[i,j] :='Select';
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids;type
      TForm1 = class(TForm)
        s1: TStringGrid;
        procedure s1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure s1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormCreate(Sender: TObject);
        procedure s1MouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        { Private declarations }
        iLeft, iTop: Integer;
        iRight, iButtom: Integer;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.s1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      s1.MouseToCell(X, Y, iLeft, iTop);
    end;procedure TForm1.s1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      SS: TGridRect;
    begin
      if iLeft<0 then Exit;
      s1.MouseToCell(X, Y, iRight, iButtom);
      SS.Left:=iLeft;
      SS.Top:=iTop;
      SS.Right:=iRight;
      SS.Bottom:=iButtom;
      s1.Selection:=SS;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      iLeft:=-1;
    end;procedure TForm1.s1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      iLeft:=-1;
    end;end.