輕松實現(xiàn)DBGrid的多表頭

字號:

用法:
    設(shè)置DBGrid的Column的Caption屬性
    例如:Column1的Caption為111|222
    Column2的Caption為111|333
    那么Column1和Column2公用一個表頭111
    unit ADBGrid;
    interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    Grids, DBGrids, Math;
    type
    TADBGrid = class(TDBGrid)
    private
    { Private declarations }
    //兄弟列子標(biāo)題,當(dāng)前列子標(biāo)題
    BrerLayerTitles, CurLayerTitles: TStringList;
    SaveFont: TFont;
    //根據(jù)當(dāng)前數(shù)據(jù)列號和表頭的層號獲取表頭的區(qū)域
    function TitleLayerRect(LayerTitles: TStrings; TitleRect: TRect; LayerID, ACol: Integer): TRect;
    //解出當(dāng)前數(shù)據(jù)列標(biāo)題為子標(biāo)題并返回標(biāo)題層數(shù)(子標(biāo)題數(shù))
    function ExtractSubTitle(LayerTitles: TStrings; ACol: Integer): Integer;
    protected
    { Protected declarations }
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
    procedure Paint; override;
    public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    published
    { Published declarations }
    end;
    procedure Register;
    implementation
    procedure Register;
    begin
    RegisterComponents(\'Samples\', [TADBGrid]);
    end;
    constructor TADBGrid.Create(AOwner: TComponent);
    begin
    inherited;
    BrerLayerTitles := TStringList.Create;
    curLayerTitles := TStringList.Create;
    SaveFont := TFont.Create;
    end;
    destructor TADBGrid.Destroy;
    begin
    BrerLayerTitles.Free;
    curLayerTitles.Free;
    SaveFont.Free;
    inherited;
    end;
    procedure TADBGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
    AState: TGridDrawState);
    var
    SubTitleRT, CaptionRt, IndicatorRT: TRect;
    Column: TColumn;
    SubTitle: string;
    i: Integer;
    begin
    if (ARow = 0) and (ACol > 0) then