StringGrid双击列分隔线调整列宽为最适合列宽 (30分)

  • 主题发起人 coolingxyz
  • 开始时间
C

coolingxyz

Unregistered / Unconfirmed
GUEST, unregistred user!
StringGrid双击列分隔线时,能调整列宽为最适合列宽。就象Excel中的一样:双击列头上的列分隔线,调整为最合适列宽。

这个功能在delphi的StringGrid中怎么实现?
 
先用canvas.textwidth得到字符串的宽度,然后再设置stringgrid。
 
procedure TFrmMain.StringGrid2DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
case StringGrid2.Tag of
0:
begin
StringGrid2.Canvas.Font.Color := clBlack;
StringGrid2.Canvas.textrect(Rect,Rect.left,Rect.Top,StringGrid2.Cells[ACol,ARow]);
end;
1:
begin
StringGrid2.Canvas.Font.Color := clRed;
StringGrid2.Canvas.textrect(Rect,Rect.left+2,Rect.Top+2,StringGrid2.Cells[ACol,ARow]);
end;
else;
end;
end;

我这么写的代码,能实现用不同颜色标记记录,但是不添加数据,而只是显示的时候也会触发StringGrid2DrawCell事件,它又把StringGrid中所有记录重画了,变成一个颜色显示了。

这个情况该怎么解决呀。
 
如下,
procedure TForm1.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with sender as tstringgrid do
if strtointdef(Cells[ACol,ARow],0)<0 then
begin//当值小于0时,显示红字
Canvas.Font.Color := clRed;
Canvas.textrect(Rect,Rect.left+2,Rect.Top+2,Cells[ACol,ARow]);
end
else
begin
Canvas.Font.Color := clBlack;
Canvas.textrect(Rect,Rect.left,Rect.Top,Cells[ACol,ARow]);
end;
end;
 
我的判断条件只有在填充的时候才知道,填充后就不管了,也无法得到。但好象每次刷新的时候他都会触发整个StringGrid的DrawCell事件。所以不能符合我的要求了,我现在改用listview了。
 
顶部