如何实现按住Ctrl键,用鼠标点击记录实现的多选?

解决方案 »

  1.   

    只能用 shift, 設置  DrawGrid1.Options
    加上
    goRowSelect
      

  2.   

    type
      TCustomGrid = class(TCustomControl)
    //....
        property Selection: TGridRect read GetSelection write SetSelection;
    //....
      end;从这个定义来看,选择区域只能是一个矩形~~
    如果想实现就需要自己加上选中的标志,然后在绘制Cell的地方处理标志~~
      

  3.   

    //参考如下代码~~
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls, Buttons;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
      private
        { Private declarations }
        FFlags: TList;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      I: Integer;
    begin
      if Button <> mbLeft then Exit;
      TStringGrid(Sender).MouseToCell(X, Y, X, Y);
      if Y < TStringGrid(Sender).FixedRows then Exit;
      if ssCtrl in Shift then
        I := FFlags.IndexOf(Pointer(Y))
      else begin
        FFlags.Clear;
        I := -1;
      end;
      if I < 0 then
        FFlags.Add(Pointer(Y))
      else FFlags.Delete(I);
      TStringGrid(Sender).Repaint;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
    begin
      FFlags := TList.Create;
      FFlags.Add(Pointer(1));
      for I := 1 to StringGrid1.RowCount - 1 do
        StringGrid1.Cells[1, I] := IntToStr(I);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      FFlags.Free;
    end;type
      TStringGridEx = class(TStringGrid)
      end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if ACol < TStringGridEx(Sender).FixedCols then Exit;
      if FFlags.IndexOf(Pointer(ARow)) < 0 then Exit;
      TStringGridEx(Sender).OnDrawCell := nil;
      try
        TStringGrid(Sender).Canvas.Font.Color := clRed;
        TStringGrid(Sender).Canvas.Brush.Color := clHighlight;
        TStringGridEx(Sender).DrawCell(ACol, ARow, Rect, State);
      finally
        TStringGridEx(Sender).OnDrawCell := StringGrid1DrawCell;
      end;
    end;end.