delphi:如何在字符串网格中更改单元格的颜色 [英] delphi : how can I change color of a cell in string grid

查看:326
本文介绍了delphi:如何在字符串网格中更改单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只要一个单元格不是一行或一列。

>

我可以吗?






RRUZ:你的程序是正确的,在我的程序中不行。



我的程序:



x是整数的全局数组

  procedure TF_avalie_salon.StringGrid1DrawCell(Sender:TObject; ACol,
ARow:Integer; Rect:TRect; State:TGridDrawState);
var S:string;
begin
S:= StringGrid1.Cells [ACol,ARow];
StringGrid1.Canvas.FillRect(Rect);
SetTextAlign(StringGrid1.Canvas.Handle,TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,Rect.Left +(Rect.Right - Rect.Left)div 2,Rect.Top + 2,S);
if(AROW<> 0)AND(acol<> 0)AND(gridclick = true)then
begin
try
gridclick:= false;
x [acol +((strtoint(Edit_hafte.Text)-1)* 7),arow]:= strtoint(StringGrid1.Cells [ACol,ARow]);
除了
x [acol +((strtoint(Edit_hafte.Text)-1)* 7),arow]:= 0;
StringGrid1.Cells [acol,arow]:='0';
with TStringGrid(Sender)do
begin
Canvas.Brush.Color:= clGreen;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left + 2,Rect.Top + 2,Cells [ACol,ARow]);
结束
结束
结束
结束






当我在下面使用Canvas.Brush.Color代码,Canvas.Brush.Color不起作用。如果我没有下面的代码,我可以改变单元格的颜色。但是我需要两个。

  S:= StringGrid1.Cells [ACol,ARow]; 
StringGrid1.Canvas.FillRect(Rect);
SetTextAlign(StringGrid1.Canvas.Handle,TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,Rect.Left +(Rect.Right - Rect.Left)div 2,Rect.Top + 2,S);


解决方案

Rafael链接包含您需要的所有内容,使用 OnDrawCell 事件是绘制StrignGrid单元格的方法。检查这个仅绘制特定单元格背景的样本。

  procedure TForm1.StringGrid1DrawCell(Sender:TObject; ACol,ARow:整数;矩形:TRect;状态:TGridDrawState); 
开始
如果(ACol = 3)和(ARow = 2)然后
与TStringGrid(发件人)做
开始
//绘制背景绿色
Canvas.Brush.Color:= clGreen;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left + 2,Rect.Top + 2,Cells [ACol,ARow]);
结束
结束


I want to change background color ( not font ) of a cell in string grid in delphi .

Just one cell not a row or a column.

Can I?


RRUZ : your procedure is correct and works but in my procedure doesn't work.

My procedure:

x is a global array of integer

procedure TF_avalie_salon.StringGrid1DrawCell(Sender: TObject; ACol,
    ARow: Integer; Rect: TRect; State: TGridDrawState);
    var   S: string;
begin
    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    if (ARow<>0 )AND(acol<>0)AND(gridclick=true) then
    begin
        try
          gridclick:=false;
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=strtoint(StringGrid1.Cells[ACol, ARow]);
        except
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=0;
          StringGrid1.Cells[acol,arow]:='0';
          with TStringGrid(Sender) do
          begin
            Canvas.Brush.Color := clGreen;
            Canvas.FillRect(Rect);
            Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
          end;
        end;
    end;
end;


When I use Canvas.Brush.Color with below code , Canvas.Brush.Color doesn't work. If I inactive below code I can change the cells color. But I need both.

    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);

解决方案

The Rafael link contains all which you need, using the OnDrawCell event is the way to paint the cells of a StrignGrid. check this sample which paint only the background of an specific cell.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;  Rect: TRect; State: TGridDrawState);
begin
  if (ACol = 3) and (ARow = 2) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clGreen;
      Canvas.FillRect(Rect);
      Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
    end;
end;

这篇关于delphi:如何在字符串网格中更改单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆