in CB, do this way:void __fastcall TForm1::StringGrid1DrawCell(.....){
if(Row==MyIntendedRow){
StringGrid1->Canvas->Brush->Color=clRed; //Replace with your choice
StringGrid1->Canvas->FillRect(Rect);
}
DrawText(StringGrid1->Canvas->Handle,
StringGrid1->Cells[Col][Row].c_str(),
-1, (RECT*)&Rect,
DT_SINGLELINE | DT_VCENTER |DT_CENTER
);
}
Delphi is exactly the same as CB on this point, DIY.
if(Row==MyIntendedRow){
StringGrid1->Canvas->Brush->Color=clRed; //Replace with your choice
StringGrid1->Canvas->FillRect(Rect);
}
DrawText(StringGrid1->Canvas->Handle,
StringGrid1->Cells[Col][Row].c_str(),
-1, (RECT*)&Rect,
DT_SINGLELINE | DT_VCENTER |DT_CENTER
);
}
Delphi is exactly the same as CB on this point, DIY.
Rect: TRect; State: TGridDrawState);
begin
if (ARow=2) and (ACol=2) then
with TStringGrid(Sender).Canvas do
begin
Brush.Color := clRed;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, TStringGrid(Sender).Cells[ACol, ARow]);
end;
end;procedure TForm3.Button1Click(Sender: TObject);
begin
//StringGrid1.RowCount := 5;
Stringgrid1.Cells[2,2]:= 'aaaa';
Stringgrid1.Cells[3,2]:= 'bbbb';
Stringgrid1.Cells[2,3]:= 'cccc';
Stringgrid1.Cells[3,3]:= 'dddd';end;
...
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);begin
SetRowColor(Sender,ACol,ARow,Rect);
end;procedure TForm1.Button1Click(Sender: TObject);
var
I, J, K : Integer;
begin
K := 0;
with StringGrid1 do
for I := 0 to ColCount - 1 do
for J:= 0 to RowCount - 1 do
begin
K := K + 1;
Cells[I,J] := IntToStr(K+100);
end;
end;procedure TForm1.SetRowColor(Sender: TObject;ACol, ARow: Integer;Rect: TRect);
begin
with TStringGrid(Sender).Canvas do
begin
if ARow=TStringGrid(Sender).RowCount-1 then
begin
Brush.Color:= clRed;
FillRect(Rect);
TextOut (Rect.Left+3,Rect.Top+4,TStringGrid(Sender).Cells[ACol,ARow]);
end;
end;
end;
如:
MyStringGrid.onDrawCell:=MyDrawCell;
with SG_ProjectInvest do
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clRED;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clred;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clAqua;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clred;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clred;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clred;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
begin
if Cells[0,ARow]='' then
begin
Canvas.Font.Color:=clBlack;
Canvas.Brush.Color:=clred;
Canvas.TextRect(rect,rect.Left+4,rect.top+4,Cells[Acol,arow])
end;
end;
1、用一个非动态创建的StringGrid, 将其OnDrawCell事件与你的事件处理代码关联,看是否会执行,如是,goto 2,如否,看看时那个属性设置问题
2. 使用动态创建的StringGrid,将其OnDrawCell事件与你的事件处理代码关联,看是否会执行。如否,检查哪个属性设置与前一个StringGrid不同
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{ StringGrid1->Canvas->Brush->Color=clRed;
StringGrid1->Canvas->FillRect(Rect);
StringGrid1->Canvas->Font->Color=clBlue;*/
//-------------
int x;
if(StringGrid1->Cells[ACol][ARow]=="ftp")
//可以变为 if(StringGrid1->Cells[ACol][ARow]->AsInteger>50)
{ StringGrid1->Canvas->Font->Color = clBlue;
StringGrid1->Canvas->Brush->Color = clRed;
StringGrid1->Canvas->FillRect(Rect);
StringGrid1->Canvas->TextOutA(Rect.left,Rect.top,StringGrid1->Cells[ACol][ARow]);
}
else{ StringGrid1->Canvas->Font->Color = clBlack;
StringGrid1->Canvas->FillRect(Rect);
StringGrid1->Canvas->TextOutA(Rect.left,Rect.top,StringGrid1->Cells[ACol][ARow]);
}
}