如何让TStringGrid网格中每一行或列的字体颜色各不相同?(50分)

  • 主题发起人 主题发起人 zwh
  • 开始时间 开始时间
Z

zwh

Unregistered / Unconfirmed
GUEST, unregistred user!
如题所示。
 
StringAlignGrid 功能强大,支持CELL的字体颜色和背景风格不同
支持独立的CELL HINT
支持GRID的内容保存到HTML 和 CSV 文件
很好用,带源码,建议你一试

下载地址 http://delphi.lele.com.cn/delphi/com/aligrid.zip
 
Using TStringGrid.OnDrawCell methed to draw the text. Reffer the help
file and read the example (Replace the TDrawGrid to TStringGrid in it);
 
to 王寒松:
虽然您推荐的控件的功能很强大, 但目前我的产品是基于
Tstringgrid控件上开发,要换控件的话挺麻烦的。
To huizhang:
我也试过使用OnDrawCell方式来处理,但它一改变某一行的字体颜色,
网格里面的所有内容的字体颜色都跟着改变。真不道怎么办啊?
 
zwh, 在你的OnDrawCell事件处理过程中, 先声明几个局部变量。在画图之前先保存画布
的设置(比的色彩。。。)。画图结束后在将画布的设置恢复。
 
zwh:
"它一改变某一行的字体颜色,
网格里面的所有内容的字体颜色都跟着改变",不会是这样吧?
如 OnDrawCell中
if (Arow=1) then
with Stringgrid1.Canvas do
begin
FillRect(Rect);
Font.Color:=clred;
TextOut(Rect.left,Rect.top,Stringgrid1.cells[Acol,Arow]);
end;
只会改变第一行的字体颜色为红,其它的不会变呀!
 
如 OnDrawCell中
const
colors: array[0..5] of TColor = ( //个数更你的Grid的行数一样
clRed,
clBlue,
clYellow,
clBlack,
clGray,
clCyan);
begin
with StringGrid1.Canvas do
begin
FillRect(Rect);
Font.Color := colors[ARow];
TextOut(Rect.left,Rect.top,Stringgrid1.cells[Acol,Arow]);
end;
end;
 
请看下面的代码:
TStringGrid has an Objects property that you can assign objects. Create an object which contains a TColor variable and assign it to Objects[col,row], so you can retrieve it any time. You must assign an OnDrawCell Event to the StringGrid, drawing the text on the Correct color. To retrieve the selection, you can use Selection property, that contains the user Selection. This should be something like that:
type
TStrColor = class(TObject)
public
Color : TColor; {you could also define a private variable and
public access methods}
end;
...
procedure TForm1.FormCreate(Sender:TObject)
var
i,j : Integer;
begin
With StringList1 do
for i := 0 to ColCount-1
for j := 0 to RowCount-1
Objects[i,j] := TStrColor.Create;
end;
...
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
var
OldColor : TColor;
begin
with StringGrid1.Canvas do begin
OldColor := Font.Color;
Font.Color := (StringGrid1.Objects[col,row] as TStrColor).Color;
TextOut(Rect.Left+2,Rect.Top+2,StringGrid1.Cells[Col,Row]);
Font.Color := OldColor;
end;
end;
...
procedure TForm1.ProcessSelection(Sender: TObject);
var
i,j : Integer;
begin
With StringGrid1.Selection do
For i := left to right do
for j := top to bottom do
MessageDlg(IntToStr(i)+','+IntToStr(j)+'-'+
IntToStr((StringGrid1.Objects[i,j] as TStrColor).Color),
mtInformation,[mbOk],0);
end;
This component cannot allow multiple selections....

 
多人接受答案了。
 

Similar threads

回复
0
查看
873
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
832
DelphiTeacher的专栏
D
S
回复
0
查看
911
SUNSTONE的Delphi笔记
S
后退
顶部