分享
 
 
 

轻松实现DBGrid的多表头

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

用法:

设置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 }

//兄弟列子标题,当前列子标题

BrerLayerTitles, CurLayerTitles: TStringList;

SaveFont: TFont;

//根据当前数据列号和表头的层号获取表头的区域

function TitleLayerRect(LayerTitles: TStrings; TitleRect: TRect; LayerID, ACol: Integer): TRect;

//解出当前数据列标题为子标题并返回标题层数(子标题数)

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

begin

ExtractSubTitle(curLayerTitles, RawToDataColumn(ACol));

for i := 0 to curLayerTitles.Count - 1 do

begin

SubTitleRT := TitleLayerRect(curLayerTitles, ARect, i, RawToDataColumn(ACol));

CaptionRt := SubTitleRT;

Canvas.Brush.Color := FixedColor;

Canvas.FillRect(SubTitleRT);

DrawEdge(Canvas.Handle, SubTitleRT, BDR_RAISEDINNER, BF_TOPLEFT);

if i <> CurLayerTitles.Count - 1 then

begin

DrawEdge(Canvas.Handle, SubTitleRT, BDR_RAISEDOUTER, BF_BOTTOM);

Dec(SubTitleRT.Bottom, 2);

end else Dec(SubTitleRT.Bottom, 1);

Canvas.Pen.Color := clWhite;

Dec(SubTitleRT.Right, 1);

Canvas.MoveTo(SubTitleRT.Right, SubTitleRT.Top);

Canvas.LineTo(SubTitleRT.Right, SubTitleRT.Bottom);

Canvas.LineTo(SubTitleRT.Left, SubTitleRT.Bottom);

Column := Columns[RawToDataColumn(ACol)];

SubTitle := '';

if Assigned(Column) then

begin

SubTitle := CurLayerTitles[i];

SaveFont.Assign(Canvas.Font);

Canvas.Font.Assign(TitleFont);

try

InflateRect(SubTitleRT, -1, -1);

DrawText(Canvas.Handle, PChar(SubTitle), Length(SubTitle),

SubTitleRT, DT_CENTER or DT_SINGLELINE or DT_VCENTER);

finally

Canvas.Font.Assign(SaveFont);

end;

end;

end;

if dgIndicator in Options then

begin

IndicatorRT := Rect(0, 0, IndicatorWidth + 1, RowHeights[0]);

Canvas.FillRect(IndicatorRT);

IndicatorRT.Right := IndicatorRT.Right - 1;

Canvas.Rectangle(IndicatorRT);

IndicatorRT.Right := IndicatorRT.Right + 1;

DrawEdge(Canvas.Handle, IndicatorRT, BDR_RAISEDOUTER, BF_RIGHT);

end;

end

else begin

inherited;

if ACol = 0 then

DrawEdge(Canvas.Handle, ARect, BDR_SUNKENOUTER, BF_BOTTOMRIGHT);

end;

end;

function TADBGrid.ExtractSubTitle(LayerTitles: TStrings;

ACol: Integer): Integer;

var L, P: Integer;

SubTitle: string;

begin

Result := 0;

if Assigned(Columns[ACol]) then

SubTitle := Columns[ACol].Title.Caption

else Exit;

if LayerTitles <> nil then LayerTitles.Clear;

L := 0;

repeat

P := Pos('|', SubTitle);

if P = 0 then

begin

if LayerTitles <> nil then LayerTitles.Add(SubTitle);

end

else begin

if LayerTitles <> nil then LayerTitles.Add(Copy(SubTitle, 1, P - 1));

SubTitle := Copy(SubTitle, P + 1, Length(SubTitle) - P);

end;

L := L + 1;

until P = 0;

Result := L;

end;

procedure TADBGrid.Paint;

var

i, MaxLayer, Layer: Integer;

TM: TTextMetric;

begin

if ([csLoading, csDestroying] * ComponentState) <> [] then Exit;

MaxLayer := 0;

//获取表头最大层数

for i := 0 to Columns.Count - 1 do

begin

Layer := ExtractSubTitle(nil, i);

if Layer > MaxLayer then MaxLayer := Layer;

end;

SaveFont.Assign(Canvas.Font);

Canvas.Font.Assign(TitleFont);

try

GetTextMetrics(Canvas.Handle, TM);

//调整DBGrid的标题行高度

RowHeights[0] := (TM.tmHeight + TM.tmInternalLeading + 3) * MaxLayer;

finally

Canvas.Font.Assign(SaveFont);

end;

inherited;

end;

function TADBGrid.TitleLayerRect(LayerTitles: TStrings; TitleRect: TRect;

LayerID, ACol: Integer): TRect;

var

SubTitle: string;

i, j: Integer;

bBrer: Boolean;

begin

Result := TitleRect;

if Assigned(Columns[ACol]) then

SubTitle := Columns[ACol].Title.Caption

else Exit;

ExtractSubTitle(LayerTitles, ACol);

//联合左边的兄弟列

for i := ACol - 1 downto 0 do

begin

ExtractSubTitle(BrerLayerTitles, i);

bBrer := False;

//判断是否为兄弟列

if (BrerLayerTitles.Count = LayerTitles.Count) then

begin

for j := 0 to LayerID do

begin

bBrer := BrerLayerTitles[j] = LayerTitles[j];

if not bBrer then

Break;

end;

end;

if bBrer then

begin

Result.Left := Result.Left - Columns[i].Width;

if dgColLines in Options then

Result.Left := Result.Left - 1;

end

else Break;

end;

//联合右边的兄弟列

for i := ACol + 1 to Columns.Count - 1 do

begin

ExtractSubTitle(BrerLayerTitles, i);

bBrer := False;

//判断是否为兄弟列

if BrerLayerTitles.Count = LayerTitles.Count then

begin

for j := 0 to LayerID do

begin

bBrer := BrerLayerTitles[j] = LayerTitles[j];

if not bBrer then

Break;

end;

end;

if bBrer then

begin

Result.Right := Result.Right + Columns[i].Width;

if dgColLines in Options then

Result.Right := Result.Right + 1;

end

else Break;

end;

//调整表头区域

Result.Top := (RowHeights[0] div LayerTitles.Count) * LayerID;

Result.Bottom := (RowHeights[0] div LayerTitles.Count) * (LayerID + 1);

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有