stringgrid 单元格字体颜色问题!

access20121 2012-01-07 10:48:34
我想改变 stringgrid1 里面 cells[2,3]这个单元格的字体 为红色,请问怎么实现! 上网查了资料 有用到 StringGrid1DrawCell 这个事件,但是不知道具体怎么用! 改颜色我知道 Canvas.Brush.Color := clRed; 但是不知道怎么只改这个cells[2,3]单元格里的颜色!
...全文
92 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2012-01-08
  • 打赏
  • 举报
回复
DrawCell事件中自己画
whisht 2012-01-07
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080320/15/cb848954-d366-49c9-b117-9aaf5dbe3d67.html?1418588808
whisht 2012-01-07
  • 打赏
  • 举报
回复
由于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;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧