cxGrid显示行号的两种方式和两种代码
2013-10-07 16:23
363 查看
第一种方式:直接双击events中的对应OnCustomDrawIndicatorCell事件,使用此代码
//==============================================================================
// 显示cxGrid的行号
//==============================================================================
procedure TFormMainOperation.cxgrddbbandtv1CustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
// AStyle: TcxStyle;
aCV: TcxCanvas;
begin
if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
Exit;
aCV := ACanvas;
ATextRect := AViewInfo.ContentBounds;
AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
InflateRect(ATextRect, -2, -1);
if AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
begin
aCV.Font.Style := Canvas.Font.Style + [fsBold];
aCV.Font.Name := '黑体';
aCV.Font.Size := 12;
aCV.Font.Color := clRed;
end
else
begin
aCV.Font.Style := Canvas.Font.Style - [fsBold];
acv.Font.Color := Canvas.Font.Color;
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
// AStyle.Font, AStyle.TextColor, AStyle.Color);
acv.Font, acv.font.Color, acv.Brush.color);
ADone := True;
end;
第二种方式:使用封装类,建立储存过程,Form初始化OnCreate事件中调用
1、建立封装类和调用函数
unit UnitDevExpressVCLCustom;
interface
uses
cxGridTableView, cxGridDBBandedTableView, Winapi.Windows, System.SysUtils, cxClasses,
System.Classes, Vcl.Graphics, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters;
type
TMyCxGrid = class(TObject)
class procedure DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
end;
procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);
implementation
{-------------------------------------------------------------------------------
作者: 曾聪
日期: 2013.10.07
过程名: TMyCxGrid.DrawIndicatorCell
功能: 显示cxGrid的行号
参数: Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean
返回值: 无
说明:
-------------------------------------------------------------------------------}
class procedure TMyCxGrid.DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
FValue: string;
FBounds: TRect;
//method 2
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
AFont: TFont;
AFontTextColor, AColor: TColor;
begin
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, TcxAlignmentVert.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 AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
begin
AFont.Style := ACanvas.Font.Style + [fsBold];
AFont.Name := '黑体';
AFont.Size := 12;
AFont.Color := clRed;
end
else
begin
AFont.Style := ACanvas.Font.Style - [fsBold];
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, TcxAlignmentVert.vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
AFont, AFontTextColor, AColor);
ADone := True;
end;
{-------------------------------------------------------------------------------
作者: 曾聪
日期: 2013.10.07
过程名: ShowLineNo
功能: 设置cxGrid显示导航列、导航列宽、调用显示行号函数
参数: cxGridDBBandedTableView: TcxGridDBBandedTableView
返回值: 无
说明:
-------------------------------------------------------------------------------}
procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);
begin
cxGridDBBandedTableView.OptionsView.Indicator := True;
cxGridDBBandedTableView.OptionsView.IndicatorWidth := 30;
cxGridDBBandedTableView.OnCustomDrawIndicatorCell := TMyCxGrid.DrawIndicatorCell;
end;
end.
2、在Form的OnCreate事件中调用:
procedure TFormMainOperation.FormCreate(Sender: TObject);
begin
ShowLineNo(cxgrddbbandtv1);
end;
以上代码如果需要在右侧仍然显示指针效果:
1、在最后一行end之前增加一行代码:
Sender.LookAndFeelPainter.DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);
2、在DrawIndicatorImage 从这里跟进去(Ctrl+左键单击)
在 cxLookAndFeelPainters 单元中作如下修改:
class procedure TcxCustomLookAndFeelPainter.DrawIndicatorImage(ACanvas: TcxCanvas;
const R: TRect; AKind: TcxIndicatorKind);
var
X, Y: Integer;
begin
if AKind = ikNone then Exit;
with cxIndicatorImages, R do
begin
X := (Left + Right - Width);
//靠右,在各函数只修改这一个地方
Y := (Top + Bottom - Height) div 2; //居中
end;
cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1);
end;
注意,我已注明靠右的那一行, 就是去掉 DIV 2 了, 此为1226版本的修改,13.1.2修改类似也是去掉DIV
2,但是左右宽的代码使用的是变量R.Left等
3、还要改一个地方:
SKIN控件目录下的dxSkinLookAndFeelPainter单元,找到
TdxSkinLookAndFeelPainter.DrawIndicatorImage 函数
的
OffsetRect(ARect, (Left + Right - cx div 2) , (Top + Bottom - cy) div 2);
这一行,将 (Left + Right - cx div 2) 改为(Left + Right - cx) 也是去掉 div 2 就是靠右;
修改后: OffsetRect(ARect, (Left + Right - cx) , (Top + Bottom - cy) div 2);
使用
procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
begin
SetRowNumber(Sender,AviewInfo,ACanvas,ADone);
end;
序号列的列宽最好改为40左右,因为数据记录过多会出现重叠现象(数字和指针图标在一起了)!
//==============================================================================
// 显示cxGrid的行号
//==============================================================================
procedure TFormMainOperation.cxgrddbbandtv1CustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
// AStyle: TcxStyle;
aCV: TcxCanvas;
begin
if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
Exit;
aCV := ACanvas;
ATextRect := AViewInfo.ContentBounds;
AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
InflateRect(ATextRect, -2, -1);
if AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
begin
aCV.Font.Style := Canvas.Font.Style + [fsBold];
aCV.Font.Name := '黑体';
aCV.Font.Size := 12;
aCV.Font.Color := clRed;
end
else
begin
aCV.Font.Style := Canvas.Font.Style - [fsBold];
acv.Font.Color := Canvas.Font.Color;
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
// AStyle.Font, AStyle.TextColor, AStyle.Color);
acv.Font, acv.font.Color, acv.Brush.color);
ADone := True;
end;
第二种方式:使用封装类,建立储存过程,Form初始化OnCreate事件中调用
1、建立封装类和调用函数
unit UnitDevExpressVCLCustom;
interface
uses
cxGridTableView, cxGridDBBandedTableView, Winapi.Windows, System.SysUtils, cxClasses,
System.Classes, Vcl.Graphics, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters;
type
TMyCxGrid = class(TObject)
class procedure DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
end;
procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);
implementation
{-------------------------------------------------------------------------------
作者: 曾聪
日期: 2013.10.07
过程名: TMyCxGrid.DrawIndicatorCell
功能: 显示cxGrid的行号
参数: Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean
返回值: 无
说明:
-------------------------------------------------------------------------------}
class procedure TMyCxGrid.DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
FValue: string;
FBounds: TRect;
//method 2
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
AFont: TFont;
AFontTextColor, AColor: TColor;
begin
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, TcxAlignmentVert.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 AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
begin
AFont.Style := ACanvas.Font.Style + [fsBold];
AFont.Name := '黑体';
AFont.Size := 12;
AFont.Color := clRed;
end
else
begin
AFont.Style := ACanvas.Font.Style - [fsBold];
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, TcxAlignmentVert.vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
AFont, AFontTextColor, AColor);
ADone := True;
end;
{-------------------------------------------------------------------------------
作者: 曾聪
日期: 2013.10.07
过程名: ShowLineNo
功能: 设置cxGrid显示导航列、导航列宽、调用显示行号函数
参数: cxGridDBBandedTableView: TcxGridDBBandedTableView
返回值: 无
说明:
-------------------------------------------------------------------------------}
procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);
begin
cxGridDBBandedTableView.OptionsView.Indicator := True;
cxGridDBBandedTableView.OptionsView.IndicatorWidth := 30;
cxGridDBBandedTableView.OnCustomDrawIndicatorCell := TMyCxGrid.DrawIndicatorCell;
end;
end.
2、在Form的OnCreate事件中调用:
procedure TFormMainOperation.FormCreate(Sender: TObject);
begin
ShowLineNo(cxgrddbbandtv1);
end;
以上代码如果需要在右侧仍然显示指针效果:
1、在最后一行end之前增加一行代码:
Sender.LookAndFeelPainter.DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);
2、在DrawIndicatorImage 从这里跟进去(Ctrl+左键单击)
在 cxLookAndFeelPainters 单元中作如下修改:
class procedure TcxCustomLookAndFeelPainter.DrawIndicatorImage(ACanvas: TcxCanvas;
const R: TRect; AKind: TcxIndicatorKind);
var
X, Y: Integer;
begin
if AKind = ikNone then Exit;
with cxIndicatorImages, R do
begin
X := (Left + Right - Width);
//靠右,在各函数只修改这一个地方
Y := (Top + Bottom - Height) div 2; //居中
end;
cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1);
end;
注意,我已注明靠右的那一行, 就是去掉 DIV 2 了, 此为1226版本的修改,13.1.2修改类似也是去掉DIV
2,但是左右宽的代码使用的是变量R.Left等
3、还要改一个地方:
SKIN控件目录下的dxSkinLookAndFeelPainter单元,找到
TdxSkinLookAndFeelPainter.DrawIndicatorImage 函数
的
OffsetRect(ARect, (Left + Right - cx div 2) , (Top + Bottom - cy) div 2);
这一行,将 (Left + Right - cx div 2) 改为(Left + Right - cx) 也是去掉 div 2 就是靠右;
修改后: OffsetRect(ARect, (Left + Right - cx) , (Top + Bottom - cy) div 2);
使用
procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(
Sender: TcxGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
begin
SetRowNumber(Sender,AviewInfo,ACanvas,ADone);
end;
序号列的列宽最好改为40左右,因为数据记录过多会出现重叠现象(数字和指针图标在一起了)!
相关文章推荐
- 如何创建不可变的Java类或对象
- vs.net 2005 C# WinForm GroupBOX 的BUG?尝试读取或写入受保护的内存。这通常指示其他内存已损坏
- Java动态代理
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- Java序列化和克隆
- C++中有关内存的思考
- 斯坦福大学公开课《编程方法学》之笔记一的材料补充
- Eclipse搭建Spring开发环境
- Overload 和 Override 的区别
- hashtable的C++实现
- java调用c++动态库dll
- C语言常见经典面试题集锦(更新中)
- C++中的冒号“:”和双冒号“::”
- c++中的explicit关键字及隐式类型转换
- 日历分析(一) java中的日期类
- Xcode4.4中,代码无法高亮、无法自动补全
- java初探native
- 《语音识别软件》(Nuance Dragon NaturallySpeaking )v12.0 Premium ISO [光盘镜像]
- 《Java虚拟机规范》阅读(三):Class文件格式
- [Editing] TP-LINK740N v5 firmware Crack