StringGrid自动调整列宽?????????

guest8188 2004-04-30 10:43:28
在StringGrid中,双击某列的右边,可根据本列的最大列宽自动调整大小,如何实现

就在Excel中双击第一行的某列中线时可自动调整大小一样
...全文
297 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
w78z 2004-05-01
  • 打赏
  • 举报
回复
UP
sandyboy 2004-04-30
  • 打赏
  • 举报
回复
关注
楚人无衣 2004-04-30
  • 打赏
  • 举报
回复
没有中线事件,你在单元格中双击事件中写入下面代码试试:
sg: TStringGrid;
...
procedure TfrmStrGrid.sgDblClick(Sender: TObject);
var
i, iWidth, iRow: Integer;
begin
iWidth := 0;
iRow := sg.Row;
for i := 0 to sg.RowCount - 1 do
begin
if self.Canvas.TextWidth(sg.Cells[sg.Col, sg.Row]) > iWidth then
iWidth := self.Canvas.TextWidth(sg.Cells[sg.Col, sg.Row]);
if sg.Row = sg.RowCount - 1 then Break;
sg.Row := sg.Row + 1;
end;
sg.Row := iRow;
iWidth := iWidth + 8;
sg.ColWidths[sg.Col] := iWidth;
end;
hahafan 2004-04-30
  • 打赏
  • 举报
回复
例如下:
需将StringGrid1.Options.goColSizing设为true
procedure TForm1.StringGrid1DblClick(Sender: TObject);
var
p : TPoint;
r : TRect;
ACol, ARow : Integer;
begin
if GetCursor = Screen.Cursors[crHSplit] then
begin
GetCursorPos(p);
p := StringGrid1.ScreenToClient(p);
StringGrid1.MouseToCell(p.X, p.Y, ACol, ARow);
r := StringGrid1.CellRect(ACol, ARow);
if p.X - r.Left < 10 then
ACol := ACol - 1;
if ACol = -1 then
ACol := StringGrid1.ColCount - 1;
StringGrid1.ColWidths[ACol] := 100;
end;
end;
hahafan 2004-04-30
  • 打赏
  • 举报
回复
在OnDblClick事件中可以通过鼠标形状的改变来判断是否双击在列中线

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧