StringGrid没有这样的属性设置,但利用自绘功能可以很容易地实现。如果Grid的DefaultDrawing属性设置为True(默认 值),Delphi会绘制单元格,包括背景色、边框、焦点框、固定单元格的三维效果。如果用户处理了OnDrawCell事件,Delphi会接着调用用 户的DrawCell过程。如果DefaultDrawing属性设置为False,Delphi只调用用户的DrawCell过程。我们在自行绘制单元 格时一般应设置DefaultDrawing属性为True,这样我们只需要绘制我们需要改变的单元,而其它单元交给Delphi去做好了。下面是一个简 单的例子:
在这里,我们调用Win32 API的DrawText绘制字体,而DrawText可以进行左或右对齐。在绘制过程中,你可以改变文字的字体。我们没有处理背景色、边框、固定单元格 的三维效果等,因为Delphi会替我们完成的。也可以进一步添加其他效果,如货币格式。
指定单元格 只读 或是可编辑
procedure TfrmShowResult.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
if (ARow =1) and ((ACol mod 10) <> 3) and ((ACol mod 10) <> 4) then
StringGrid1.Options :=StringGrid1.Options+[goediting]
else
StringGrid1.Options :=StringGrid1.Options-[goediting] ;
end;
指定单元格 着色
procedure TfrmShowResult.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
if (ACol mod 10) = 3 then
StringGrid1.Canvas.Brush.Color := $00FFE9D2
else
StringGrid1.Canvas.Brush.Color := clWhite;
if (ACol mod 10) = 4 then
StringGrid1.Canvas.Brush.Color := $00C1FFFF;
if (ARow =1) and ((ACol mod 10) <> 3) and ((ACol mod 10) <> 4) then
StringGrid1.Canvas.Brush.Color :=$00E8FFD0;
if (ARow =0)or(ACol<3) then
StringGrid1.Canvas.Brush.Color := clBtnFace;
end;