StringGrid显示问题 ( 积分: 100 )

  • 主题发起人 主题发起人 a_ping
  • 开始时间 开始时间
A

a_ping

Unregistered / Unconfirmed
GUEST, unregistred user!
1。我的StringGrid一共有四列,其第三第四列需将数据靠右,我通过下面的代码实现了
procedure TActiveFormX.DataGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
vCol, vRow : LongInt;
begin
vCol := ACol; vRow := ARow;
with Sender as TStringGrid, Canvas do
if ((vCol = 2) or (vCol = 3)) and (vRow > 0) then
begin ///对于第2列设置为右对齐
SetTextAlign(Handle, TA_RIGHT);
FillRect(Rect);
TextRect(Rect, Rect.RIGHT-2, Rect.Top+2, Cells[vCol, vRow]);
end;
end;
但现在问题是我的前面两列的数据都显示不出来,要点一下才能看见,怎么回事?
2。如果在StringGrid里某格输入一个负数,怎么让这个数字显示成红色?
 
1。我的StringGrid一共有四列,其第三第四列需将数据靠右,我通过下面的代码实现了
procedure TActiveFormX.DataGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
vCol, vRow : LongInt;
begin
vCol := ACol; vRow := ARow;
with Sender as TStringGrid, Canvas do
if ((vCol = 2) or (vCol = 3)) and (vRow > 0) then
begin ///对于第2列设置为右对齐
SetTextAlign(Handle, TA_RIGHT);
FillRect(Rect);
TextRect(Rect, Rect.RIGHT-2, Rect.Top+2, Cells[vCol, vRow]);
end;
end;
但现在问题是我的前面两列的数据都显示不出来,要点一下才能看见,怎么回事?
2。如果在StringGrid里某格输入一个负数,怎么让这个数字显示成红色?
 
第一个问题是SetTextAlign(Handle, TA_RIGHT);的原因
改成:
if ...
begin
FillRect(Rect);
DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol,
ARow]),Rect, DT_RIGHT);
end;
 
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if ((ACol = 2) or (ACol = 3)) And (ARow > 0) then
with StringGrid1,StringGrid1.Canvas do
begin
FillRect(Rect);
DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]),Rect, DT_RIGHT);//
end;
end;


2.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
cColor : TColor;
begin
if strtointdef(StringGrid1.cells[ACol,ARow],0) < 0 then
cColor := clred
eles
cColor := clBlack;
with stringgrid1.canvas do
begin
Font.color := cColor;
FillRect(Rect);
TextRect(Rect, Rect.Left+2, Rect.Top+2, Cells[vCol, vRow]);
end;
end;
 
1。你根本不需要用画的功能来做啊。你可以使用 Cells[i,j]:=text 给每一个单元格赋值的。

2.procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if strtofloat(StringGrid1.Cells[i,j])<0 then
begin
StringGrid1.Canvas.Font.Color:=clred;

end
end;
 
//至于颜色用下面的方法应该可以,对于位置你自己去试试吧
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
aColor:Tcolor;
begin
aColor:=clBlack;//默认的颜色
if ((aCol=0) or(aCol=1))and (aRow>0)then
begin
if stringGrid.cells[acol,arow]<0 then
acolor:=clred ;
stringGrid.canvas.font.color:=acolor;
stringGrid.canvas.textout(Rect.left+1,Rect.Top+1,stringGrid.cells.[acol,aRow]);
end;
if ((aCol=2) or(aCol=3))and(aRow>0) then
begin
if stringGrid.cells[acol,arow]<0 then
acolor:=clred ;
stringGrid.canvas.font.color:=acolor;
stringGrid.canvas.textout(Rect.right+1,Rect.Top+1,stringGrid.cells. [acol,aRow]);
end;
end;
 
第一个问题解决了,但显示红字始终不行,上面几位朋友的方法试了,不管用
 
下面代码测试过的:
var vCol, vRow : LongInt;
d:double;
old_clr:Tcolor;
begin
vCol := ACol; vRow := ARow;

with Sender as TStringGrid, Canvas do
if ((vCol = 2) or (vCol = 3)) and (vRow > 0) then
begin
old_clr:=font.Color;
if (trystrToFloat(Cells[ACol,ARow],d)) and (d<0) then
font.Color:=clRed;

FillRect(Rect);
DrawText(Handle, PChar(s),Length(Cells[ACol,
ARow]),Rect, DT_RIGHT);
font.Color:=old_clr;
end;
 
多人接受答案了。
 
后退
顶部