多年以前写过一个给cxGrid加序列号函数,在CustomDrawIndicatorCell 事件中调用SetRowNumber,后来还做了N个版本的<<cxGrid加序号补丁>>,由于期间误格硬盘,所有自己写的古董级的代码全数牺牲,现在要找以前写的东西,还得去谷大神,百大姐一翻找能找到...
原来写的SetRowNumber函数不太完善,cxGrid加了序号后,原来左侧行标志会被覆盖掉,需要显示还得修改控件源代码... 最近使用时发现诸多不便(不想老跟着Dev版本的升级,而去修改它的代码),所以对原函数做了小小的修改.
uses cxLookAndFeelPainters; //引用这个是必须嘀~
procedure SetRowNumber(var Sender: TcxGridTableView; var AViewInfo: TcxCustomGridIndicatorItemViewInfo;
ACanvas: TcxCanvas; var ADone: boolean);
var
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
AFont: TFont;
AFontTextColor, AColor: TColor;
procedure DrawIndicatorImage(ACanvas: TcxCanvas;
const R: TRect; AKind: TcxIndicatorKind);
var
X, Y: Integer;
begin
if AKind = ikNone then Exit;
X := (R.Left + R.Right - cxLookAndFeelPainters.cxIndicatorImages.Width);
Y := (R.Top + R.Bottom - cxLookAndFeelPainters.cxIndicatorImages.Height) div 2;
cxLookAndFeelPainters.cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1);
end;
begin
try
AFont := ACanvas.Font;
AColor := clBtnFace;
AFontTextColor := clWindowText;
if (AViewInfo is TcxGridIndicatorHeaderItemViewInfo) then begin
ATextRect := AViewInfo.Bounds;
InflateRect(ATextRect, -1, -1);
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.Bounds,
ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
False, False, '序号', AFont, AFontTextColor, AColor);
ADone := True;
end;
if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
Exit;
ATextRect := AViewInfo.ContentBounds;
AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
InflateRect(ATextRect, -1, -1);
if Sender.DataController.RecordCount > 0 then begin
if AIndicatorViewInfo.GridRecord.Selected then
AFontTextColor := clRed
else
AFontTextColor := clWindowText;
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
AFont, AFontTextColor, AColor);
ADone := True;
except
end;
DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);
end;
使用
procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
begin
SetRowNumber(Sender,AViewInfo,ACanvas,ADone);
end;
最后,因现在已不使用Dev的皮肤,手头上也没有dxSkinLookAndFeelPainter.pas的源码,所以对使用了Dev皮肤控件的cxGrid的序号是显示不了的,当然,如果在使用了皮肤的cxGrid也只是参照DrawIndicatorImage作相应的修改即可了