关于控制 stringgrid 中每一行字体的颜色(100分)

  • 主题发起人 brucedai
  • 开始时间
B

brucedai

Unregistered / Unconfirmed
GUEST, unregistred user!
用以下代码控制 颜色并且把 stringgrid1.defautdrawing:=false
每一行的颜色是按照控制的方法变化了, 但是当前行变了之后 前面行的文字却不见了
急! 有什么好的解决办法吗?(currentrow 和 cellstate 是控制变量)
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var Area:TRect;
begin
if (arow=currentrow) and (cellstate=1) then
with StringGrid1,StringGrid1.Canvas do
begin
FillRect(Rect);
Font.Color:=clRed;
Area:=Rect;
InflateRect(Area, -2, -2);
DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_left);
end
 
FillRect(Rect);前设置一下 canvas.brush.color 试试
 
procedure TfrmQryWayPB.strGrdStockDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var Str: string;
begin
StrGrdStock.Canvas.Font.Assign(StrGrdStock.Font);
with StrGrdStock,StrGrdStock.Canvas do
begin
if (ARow = 0) then
begin
Font.Color := clBlue;
Font.Size := 9;
Font.Style:=[fsBold];
end;
if gdFixed in State then Brush.Color := FixedColor;
if (ARow = Row) and (ACol >= FixedCols) then Brush.Color := clRed;
FillRect(Rect);
InflateRect(Rect, -2, -2);
str := Cells[ACol, ARow];
// DrawText(Handle, PChar(Str),-1, RECT, DT_WORDBREAK) //自动换行
if (ARow = 0) and ((ACol = 5) or (ACol = 10) or (ACol = 13)) then
DrawText(Handle, PChar(Str),Length(Cells[ACol, ARow]), RECT, DT_CENTER) //居中
else if (ARow = 0) or (ACol = 14) then
DrawText(Handle, PChar(Str),Length(Cells[ACol, ARow]), RECT, DT_CENTER or DT_SINGLELINE or DT_VCENTER) //上下对齐居中
else if ACol < 2 then
DrawText(Handle, PChar(Str),Length(Cells[ACol, ARow]), RECT, DT_LEFT) //左对齐
else
DrawText(Handle, PChar(Str),Length(Cells[ACol, ARow]), RECT, DT_RIGHT); //右对齐
end;
end;
 
顶部