首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

stringgrid 单元格字体颜色有关问题

2012-02-24 
stringgrid 单元格字体颜色问题!我想改变stringgrid1 里面cells[2,3]这个单元格的字体 为红色,请问怎么实

stringgrid 单元格字体颜色问题!
我想改变 stringgrid1 里面 cells[2,3]这个单元格的字体 为红色,请问怎么实现! 上网查了资料 有用到 StringGrid1DrawCell 这个事件,但是不知道具体怎么用! 改颜色我知道 Canvas.Brush.Color := clRed; 但是不知道怎么只改这个cells[2,3]单元格里的颜色! 


[解决办法]
由于TStringGrid没有并没有提供类似的方法.所以只能自己画了.
以下代码 是假定 有一个名称为 Form2 的窗体 上面放着 一个名称为 sGrid的TStringGrid:
以下代码实现了这个StringGrid的OnDrawCell事件

procedure TForm2.sGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
TheGrid: TStringGrid; CellText: string;
begin
if not (ARow = 1) then Exit; //如果不满足条件,条件由你自行确定
TheGrid := Sender as TStringGrid; //强制类型转换Sender,可以让多个StringGrid来使用这个函数

with TheGrid.Canvas do
begin
//如果单元格被中了则绘制高亮否则,则绘制指定的颜色
if gdSelected in State then
begin
Brush.Color := clHighlight; Font.Color := clHighlightText;
end
else
begin
Brush.Color := clWindow; Font.Color := clRed; //这里是你需要设置的颜色,暂时这只为红色
end;

if gdFixed in State then
begin
Brush.Color := clBtnFace;
end;

FillRect(Rect); //代替原始内容,并绘制背景

CellText := TheGrid.Rows[ARow][ACol]; //获取单元格文字
Inc(Rect.Left,2); //让文本区域左缩进2像素
//利用Windows API函数绘制文本
DrawText(Handle,PChar(CellText),Length(CellText),Rect,
DT_LEFT or DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS);
end;
end;
[解决办法]
http://topic.csdn.net/u/20080320/15/cb848954-d366-49c9-b117-9aaf5dbe3d67.html?1418588808
[解决办法]
DrawCell事件中自己画

热点排行