谢谢,我也想知道.
[email protected]
[email protected]

解决方案 »

  1.   

    TStringGrid = class(TDrawGrid)
      private
        FData: Pointer;
        FRows: Pointer;
        FCols: Pointer;
        FUpdating: Boolean;
        FNeedsUpdating: Boolean;
        FEditUpdate: Integer;
        procedure DisableEditUpdate;
        procedure EnableEditUpdate;
        procedure Initialize;
        procedure Update(ACol, ARow: Integer); reintroduce;
        procedure SetUpdateState(Updating: Boolean);
        function GetCells(ACol, ARow: Integer): string;
        function GetCols(Index: Integer): TStrings;
        function GetObjects(ACol, ARow: Integer): TObject;
        function GetRows(Index: Integer): TStrings;
        procedure SetCells(ACol, ARow: Integer; const Value: string);
        procedure SetCols(Index: Integer; Value: TStrings);
        procedure SetObjects(ACol, ARow: Integer; Value: TObject);
        procedure SetRows(Index: Integer; Value: TStrings);
        function EnsureColRow(Index: Integer; IsCol: Boolean): TStringGridStrings;
        function EnsureDataRow(ARow: Integer): Pointer;
      protected
        procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
        procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
          AState: TGridDrawState); override;
        function GetEditText(ACol, ARow: Longint): string; override;
        procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
        procedure RowMoved(FromIndex, ToIndex: Longint); override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        property Cells[ACol, ARow: Integer]: string read GetCells write SetCells;
        property Cols[Index: Integer]: TStrings read GetCols write SetCols;
        property Objects[ACol, ARow: Integer]: TObject read GetObjects write SetObjects;
        property Rows[Index: Integer]: TStrings read GetRows write SetRows;
      end;implementationconstructor TStringGrid.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Initialize;
    end;destructor TStringGrid.Destroy;
      function FreeItem(TheIndex: Integer; TheItem: Pointer): Integer; far;
      begin
        TObject(TheItem).Free;
        Result := 0;
      end;begin
      if FRows <> nil then
      begin
        TSparseList(FRows).ForAll(@FreeItem);
        TSparseList(FRows).Free;
      end;
      if FCols <> nil then
      begin
        TSparseList(FCols).ForAll(@FreeItem);
        TSparseList(FCols).Free;
      end;
      if FData <> nil then
      begin
        TSparseList(FData).ForAll(@FreeItem);
        TSparseList(FData).Free;
      end;
      inherited Destroy;
    end;procedure TStringGrid.ColumnMoved(FromIndex, ToIndex: Longint);  function MoveColData(Index: Integer; ARow: TStringSparseList): Integer; far;
      begin
        ARow.Move(FromIndex, ToIndex);
        Result := 0;
      end;begin
      TSparseList(FData).ForAll(@MoveColData);
      Invalidate;
      inherited ColumnMoved(FromIndex, ToIndex);
    end;procedure TStringGrid.RowMoved(FromIndex, ToIndex: Longint);
    begin
      TSparseList(FData).Move(FromIndex, ToIndex);
      Invalidate;
      inherited RowMoved(FromIndex, ToIndex);
    end;function TStringGrid.GetEditText(ACol, ARow: Longint): string;
    begin
      Result := Cells[ACol, ARow];
      if Assigned(FOnGetEditText) then FOnGetEditText(Self, ACol, ARow, Result);
    end;procedure TStringGrid.SetEditText(ACol, ARow: Longint; const Value: string);
    begin
      DisableEditUpdate;
      try
        if Value <> Cells[ACol, ARow] then Cells[ACol, ARow] := Value;
      finally
        EnableEditUpdate;
      end;
      inherited SetEditText(ACol, ARow, Value);
    end;procedure TStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
      AState: TGridDrawState);
    begin
      if DefaultDrawing then
        Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol, ARow]);
      inherited DrawCell(ACol, ARow, ARect, AState);
    end;procedure TStringGrid.DisableEditUpdate;
    begin
      Inc(FEditUpdate);
    end;procedure TStringGrid.EnableEditUpdate;
    begin
      Dec(FEditUpdate);
    end;procedure TStringGrid.Initialize;
    var
      quantum: TSPAQuantum;
    begin
      if FCols = nil then
      begin
        if ColCount > 512 then quantum := SPALarge else quantum := SPASmall;
        FCols := TSparseList.Create(quantum);
      end;
      if RowCount > 256 then quantum := SPALarge else quantum := SPASmall;
      if FRows = nil then FRows := TSparseList.Create(quantum);
      if FData = nil then FData := TSparseList.Create(quantum);
    end;procedure TStringGrid.SetUpdateState(Updating: Boolean);
    begin
      FUpdating := Updating; 
      if not Updating and FNeedsUpdating then
      begin
        InvalidateGrid;
        FNeedsUpdating := False;
      end;
    end;procedure TStringGrid.Update(ACol, ARow: Integer);
    begin
      if not FUpdating then InvalidateCell(ACol, ARow)
      else FNeedsUpdating := True;
      if (ACol = Col) and (ARow = Row) and (FEditUpdate = 0) then InvalidateEditor;
    end;function  TStringGrid.EnsureColRow(Index: Integer; IsCol: Boolean):
      TStringGridStrings;
    var
      RCIndex: Integer;
      PList: ^TSparseList;
    begin
      if IsCol then PList := @FCols else PList := @FRows;
      Result := TStringGridStrings(PList^[Index]);
      if Result = nil then
      begin
        if IsCol then RCIndex := -Index - 1 else RCIndex := Index + 1;
        Result := TStringGridStrings.Create(Self, RCIndex);
        PList^[Index] := Result;
      end;
    end;function  TStringGrid.EnsureDataRow(ARow: Integer): Pointer;
    var
      quantum: TSPAQuantum;
    begin
      Result := TStringSparseList(TSparseList(FData)[ARow]);
      if Result = nil then
      begin
        if ColCount > 512 then quantum := SPALarge else quantum := SPASmall;
        Result := TStringSparseList.Create(quantum);
        TSparseList(FData)[ARow] := Result;
      end;
    end;function TStringGrid.GetCells(ACol, ARow: Integer): string;
    var
      ssl: TStringSparseList;
    begin
      ssl := TStringSparseList(TSparseList(FData)[ARow]);
      if ssl = nil then Result := '' else Result := ssl[ACol];
    end;function TStringGrid.GetCols(Index: Integer): TStrings;
    begin
      Result := EnsureColRow(Index, True);
    end;function TStringGrid.GetObjects(ACol, ARow: Integer): TObject;
    var
      ssl: TStringSparseList;
    begin
      ssl := TStringSparseList(TSparseList(FData)[ARow]);
      if ssl = nil then Result := nil else Result := ssl.Objects[ACol];
    end;function TStringGrid.GetRows(Index: Integer): TStrings;
    begin
      Result := EnsureColRow(Index, False);
    end;procedure TStringGrid.SetCells(ACol, ARow: Integer; const Value: string);
    begin
      TStringGridStrings(EnsureDataRow(ARow))[ACol] := Value;
      EnsureColRow(ACol, True);
      EnsureColRow(ARow, False);
      Update(ACol, ARow);
    end;procedure TStringGrid.SetCols(Index: Integer; Value: TStrings);
    begin
      EnsureColRow(Index, True).Assign(Value);
    end;procedure TStringGrid.SetObjects(ACol, ARow: Integer; Value: TObject);
    begin
      TStringGridStrings(EnsureDataRow(ARow)).Objects[ACol] := Value;
      EnsureColRow(ACol, True);
      EnsureColRow(ARow, False);
      Update(ACol, ARow);
    end;procedure TStringGrid.SetRows(Index: Integer; Value: TStrings);
    begin
      EnsureColRow(Index, False).Assign(Value);
    end;
      

  2.   

    ^_^
    别吓着,其实就在这里:[delphi目录]\source\vcl\grids.pas
    其中的TStringGrid类
      

  3.   

    hpn_cao(小海) 你好,我已经加到100分了,小弟不才,可以解析一下代码吗?
    另外我需要的不是一个新类,我只是想对Delphi现有的StirngGrid这个类操作,合并FORM上某一StringGrid指定的Cell而已,可以给出操作代码吗?
      

  4.   

    真可惜,分又送不出了,摸索了一下,搞懂了原理,不敢独享,贴出来大家一起探讨一下有没有更好的方法procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
        if ((acol=1) and (arow=1)) or ((acol=1) and (arow=2)) then
        begin
           //showmessage(inttostr(rect.Bottom) + ' ' +inttostr(rect.Top));
           rect.Bottom:=rect.Bottom + (rect.Bottom-rect.Top);                       
           //showmessage(inttostr(rect.Bottom) + ' ' +inttostr(rect.Top));
        end;
        if  (not((acol=1) and (arow=2))) or (not((acol=1) and (arow=1))) then
        begin
            stringgrid1.Canvas.FillRect(Rect);
            stringgrid1.canvas.textout(rect.left,rect.top+18,StringGrid1.cells[acol,arow]);
        end;
    end;
      

  5.   

    fixedCol or fixedRow怎么合并到是比较实用