Delphi中如何找到带有滚动条的网格组件的实际宽度

发布于 2024-08-11 01:24:23 字数 124 浏览 2 评论 0原文

我有一个网格组件(DBGrid),上面有很多列。由于列数较多,创建了滚动条,因此网格的某些部分仍然隐藏。我需要找出DBGrid的真实宽度是多少,包括由于滚动条而未显示的部分。但 Width 属性仅给出组件本身的宽度。有人有什么想法吗?

I have a grid component (DBGrid) which has lots of columns on it. Because of large number of columns, a scrollbar was created, and thus some part of grid remains hidden. I need to find out what is the real width of DBGrid, including the part which is not shown due to scroll bar. But Width property gives only the width of the component itself. Anybody has any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弱骨蛰伏 2024-08-18 01:24:23

TDBGrid 有一个Columns 属性。每列都有自己的 Width 属性。因此,您可以循环遍历所有列并总结它们的宽度。

像这样:

function TotalColumnsWidth(var AGrid: TDBGrid);
var
  i: Integer;
begin
  Result := 0;
  for i := to AGrid.Columns.Count - 1 do
    Result := Result + AGrid.Columns[i].Width;
end;

TDBGrid has a Columns property. Each of the columns has its own Width property. So you could loop through all of the columns and sum up their widths.

Like this:

function TotalColumnsWidth(var AGrid: TDBGrid);
var
  i: Integer;
begin
  Result := 0;
  for i := to AGrid.Columns.Count - 1 do
    Result := Result + AGrid.Columns[i].Width;
end;
浮生面具三千个 2024-08-18 01:24:23

也许这会有所帮助。它是 TDBGrid 类帮助程序的一部分,可自动调整最后一列的大小,以便网格没有空白空间。应该很容易根据您的需求进行调整。

您可能会注意到,CalcDrawInfo 方法就是您正在寻找的方法。由于它是受保护的,因此您可以使用类助手或通常的受保护黑客来获取它。

procedure TDbGridHelper.AutoSizeLastColumn;
var
  DrawInfo: TGridDrawInfo;
  ColNo: Integer;
begin
  ColNo := ColCount - 1;
  CalcDrawInfo(DrawInfo);
  if (DrawInfo.Horz.LastFullVisibleCell < ColNo - 1) then Exit;

  if (DrawInfo.Horz.LastFullVisibleCell < ColNo) then
    ColWidths[ColNo] := DrawInfo.Horz.GridBoundary - DrawInfo.Horz.FullVisBoundary
  else
    ColWidths[ColNo] := ColWidths[ColNo] + DrawInfo.Horz.GridExtent - DrawInfo.Horz.FullVisBoundary
end;

Perhaps this may be helpful. It is part of a class helper for TDBGrid that auto sizes the last column, so that the grid has no empty space. Should be easy to adjust to your needs.

As you may notice, the CalcDrawInfo method is what you are seeking for. As it is protected you can either use a class helper or the usual protected-hack to get hands on it.

procedure TDbGridHelper.AutoSizeLastColumn;
var
  DrawInfo: TGridDrawInfo;
  ColNo: Integer;
begin
  ColNo := ColCount - 1;
  CalcDrawInfo(DrawInfo);
  if (DrawInfo.Horz.LastFullVisibleCell < ColNo - 1) then Exit;

  if (DrawInfo.Horz.LastFullVisibleCell < ColNo) then
    ColWidths[ColNo] := DrawInfo.Horz.GridBoundary - DrawInfo.Horz.FullVisBoundary
  else
    ColWidths[ColNo] := ColWidths[ColNo] + DrawInfo.Horz.GridExtent - DrawInfo.Horz.FullVisBoundary
end;
扬花落满肩 2024-08-18 01:24:23

我想我已经找到了解决方案(虽然看起来有点奇怪)。为了找到 DBgrid 的列宽和实际宽度之间的差异(这意味着找到最后一列之后留下的空白空间的宽度),我们需要跟踪现在显示在左侧的列(当前列是什么)滚动到的位置)。我们可以使用 OnDrawColumnCell 事件来做到这一点,因为它只会绘制现在滚动的列。然后我们需要计算所有可见列的宽度总和,并从 DBGrid 的宽度中减去该宽度。 PS 抱歉英语不好

Ex 代码:

     For i:=0 to Last do
     if Vis[i] then
     Begin
      Sum:=Sum+DBG.Columns[i].Width;
      Inc(Cnt);
     End;

     if dgColLines in DBG.Options then
     Sum := Sum + Cnt;

  //add indicator column width
    if dgIndicator in DBG.Options then
    Sum := Sum + IndicatorWidth;
    Dif:=DBG.ClientWidth - Sum;

I think I have found a solution (although it seems a little strange). In order to find the difference between column widths and real width of the DBgrid (that means find the width of the empty space left after last column), we need to keep track of which column is shown on the left now (what is current column that is scrolled to). We can do that using OnDrawColumnCell event, since it will draw only columns which are scrolled on now. Then we need to calculate sum of widths of all visible columns, and subtract that from DBGrid's width. P.S. Sorry for bad english

Ex code:

     For i:=0 to Last do
     if Vis[i] then
     Begin
      Sum:=Sum+DBG.Columns[i].Width;
      Inc(Cnt);
     End;

     if dgColLines in DBG.Options then
     Sum := Sum + Cnt;

  //add indicator column width
    if dgIndicator in DBG.Options then
    Sum := Sum + IndicatorWidth;
    Dif:=DBG.ClientWidth - Sum;
岛歌少女 2024-08-18 01:24:23

以下是我们过去使用过的函数。它考虑了基于字体的数据宽度,并且还补偿了垂直线(如果它们可见)

function GridTextWidth(fntFont : TFont; const sString : OpenString) :
  integer;
var
  f: TForm;
begin
  try
    f:=TForm.Create(nil);
    f.Font:=fntFont;
    result:=f.canvas.textwidth(sstring);
  finally
    f.Free;
    end;
end;




function CalcGridWidth(dbg : TDBGrid { the grid to meaure }): integer; { the "exact" width }
const cMEASURE_CHAR   = '0';
      iEXTRA_COL_PIX  = 4;
      iINDICATOR_WIDE = 11;
var i, iColumns, iColWidth, iTitleWidth, iCharWidth : integer;
begin
  iColumns := 0;
  result   := GetSystemMetrics(SM_CXVSCROLL);

  iCharWidth := GridTextWidth(dbg.font,cMeasure_char);

  with dbg.dataSource.dataSet do begin
    DisableControls;
    for i := 0 to FieldCount - 1 do with Fields[i] do
      if visible then
      begin
        iColWidth := iCharWidth * DisplayWidth;
        if dgTitles in dbg.Options then begin
          ititlewidth:=GridTextWidth(dbg.titlefont,displaylabel);
          if iColWidth < iTitleWidth then
            iColWidth := iTitleWidth;
        end;
        inc(iColumns, 1);
        inc(result, iColWidth + iEXTRA_COL_PIX);
      end;
      EnableControls;
    end;

  if dgIndicator in dbg.Options then
  begin
    inc(iColumns, 1);
    inc(result, iINDICATOR_WIDE);
  end;
  if dgColLines in dbg.Options then
    inc(result, iColumns)
  else
    inc(result, 1);
end;

Here are functions we have used in the past. It takes into account the width of data based on the font and also compensates for vertical lines if they are visible

function GridTextWidth(fntFont : TFont; const sString : OpenString) :
  integer;
var
  f: TForm;
begin
  try
    f:=TForm.Create(nil);
    f.Font:=fntFont;
    result:=f.canvas.textwidth(sstring);
  finally
    f.Free;
    end;
end;




function CalcGridWidth(dbg : TDBGrid { the grid to meaure }): integer; { the "exact" width }
const cMEASURE_CHAR   = '0';
      iEXTRA_COL_PIX  = 4;
      iINDICATOR_WIDE = 11;
var i, iColumns, iColWidth, iTitleWidth, iCharWidth : integer;
begin
  iColumns := 0;
  result   := GetSystemMetrics(SM_CXVSCROLL);

  iCharWidth := GridTextWidth(dbg.font,cMeasure_char);

  with dbg.dataSource.dataSet do begin
    DisableControls;
    for i := 0 to FieldCount - 1 do with Fields[i] do
      if visible then
      begin
        iColWidth := iCharWidth * DisplayWidth;
        if dgTitles in dbg.Options then begin
          ititlewidth:=GridTextWidth(dbg.titlefont,displaylabel);
          if iColWidth < iTitleWidth then
            iColWidth := iTitleWidth;
        end;
        inc(iColumns, 1);
        inc(result, iColWidth + iEXTRA_COL_PIX);
      end;
      EnableControls;
    end;

  if dgIndicator in dbg.Options then
  begin
    inc(iColumns, 1);
    inc(result, iINDICATOR_WIDE);
  end;
  if dgColLines in dbg.Options then
    inc(result, iColumns)
  else
    inc(result, 1);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文