简体   繁体   English

StringGrid Delphi XE2的Cell是如何着色的?

[英]How colored the Cell of StringGrid Delphi XE2?

im start with Delphi. 我从Delphi开始。 I have a problem with TStringGrid and Colored the Cell. 我遇到了TStringGrid和Colored the Cell的问题。 im using this code for color the BackGround when is selected: 我选择BackGround时使用此代码进行颜色处理:

procedure TForm_Matrix.MatrizGeneralDrawCell(Sender: TObject;
  ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  ACol:=MatrizGeneral.Col;
  ARow:=MatrizGeneral.Row;
  begin
    if (RBAlto.Checked = True) then // Nivel de color ROJO - ALTO
      MatrizGeneral.Canvas.Brush.Color :=clRed;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBMedio.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clYellow;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBBajo.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clLime;
      MatrizGeneral.Canvas.FillRect(Rect);
  end;
end;

Its work, but when i try to change the color change the cell selected, and the first cell idk why. 它的工作,但当我尝试改变颜色改变选择的细胞,和第一个细胞idk为什么。

  1. When i select 3 cell with color red. 当我选择3个红色的单元格时。 (Work Fine) (工作正常) 在此输入图像描述

  2. Change the color of another cell, change the fist cell TT 改变另一个细胞的颜色,改变第一个细胞TT 在此输入图像描述

    http://i.stack.imgur.com/umG0r.png http://i.stack.imgur.com/1o93C.png http://i.stack.imgur.com/umG0r.png http://i.stack.imgur.com/1o93C.png

HELP!!! 救命!!!

If you want to color only the selected cells, you have to check the State parameter that is passed in, and only draw if the State is selected. 如果要仅为选定的单元格着色,则必须检查传入的State参数,并且仅在选择State时绘制。

Also, you are drawing the cell 3x in that routine . 此外,您正在该例程中绘制单元格3x。 Just put MatrizGeneral.Canvas.FillRect(Rect); 只需输入MatrizGeneral.Canvas.FillRect(Rect); once at the end, you do not need it with every IF block. 一旦结束,每个IF块都不需要它。

I use this rutine for colored the cell selected with a radiogroup: 我使用这个rutine为用放射性组选择的细胞着色:

if MatrizGeneral.Cells[ACol,ARow] <> '' then begin
case StrToInt(MatrizGeneral.Cells[ACol,ARow]) of
  0: BGColor := clRed;
  1: BGColor := clYellow;
  2: BGColor := clLime;
else
  BGColor := clWhite;
end;
with MatrizGeneral do begin
  Canvas.Brush.Color := BGColor;
  Canvas.FillRect(Rect);

  if (gdFocused in State) then
    Canvas.Font.Color := clWhite
  else
    Canvas.Font.Color := clBlack;
end;

end; 结束;

Works Great! 效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM