如何正确计算桌子的高度

发布于 2024-07-17 10:37:06 字数 283 浏览 15 评论 0原文

如何在 VB6 中计算 FlexGrid 表格的高度,使其仅包含填充的行数。

目前

myFlexGrid.Height = (myFlexGrid.CellHeight * myFlexGrid.Rows) ' paraphrased from code

每行大约短 3 个像素。 添加幻数有点hackish,并且希望无需求助于此即可完成此操作。

更新: 让事情变得复杂的是,它还需要处理多行单元格。

How do you calculate the height of a flexgrid table in VB6 so that it only contains the number of filled rows.

Currently

myFlexGrid.Height = (myFlexGrid.CellHeight * myFlexGrid.Rows) ' paraphrased from code

comes out at about 3 pixels short per line. Adding in the magic number is a bit hackish and would like to accomplish this without having to resort to that.

Update:
To complicate matters it also needs to handle multiline cells.

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

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

发布评论

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

评论(3

面犯桃花 2024-07-24 10:37:06

RS Coneley 很接近,但以下是考虑所有 DPI 设置的正确方法:

Me.MSFlexGrid1.Height = Me.MSFlexGrid1.CellHeight _
                      * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) _
                      + (Screen.TwipsPerPixelY * 2)

RS Coneley is close, but here is the correct way that accounts for all DPI settings:

Me.MSFlexGrid1.Height = Me.MSFlexGrid1.CellHeight _
                      * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) _
                      + (Screen.TwipsPerPixelY * 2)
℡寂寞咖啡 2024-07-24 10:37:06

您需要将

Me.MSFlexGrid1.Height = (Me.MSFlexGrid1.CellHeight) * (Me.MSFlexGrid1.Rows + _
Me.MSFlexGrid1.FixedRows) + 30

30 延长两个像素,以显示围绕柔性网格运行的黑色边框。

禁用垂直滚动条也有帮助。

You need to go

Me.MSFlexGrid1.Height = (Me.MSFlexGrid1.CellHeight) * (Me.MSFlexGrid1.Rows + _
Me.MSFlexGrid1.FixedRows) + 30

The 30 is to make it two pixels longer to show the black border running around the flexgrid.

Also disabling the vertical scrollbar helps as well.

梦魇绽荼蘼 2024-07-24 10:37:06

这是我想出的最终代码,

    For i = 0 To fgrComments.Rows - 1
        'Set MSFlexGrid to appropriate Cell
        myFlexGrid.Row = i

        'Set textbox to match the selected cell
        txtSizer.Width = myFlexGrid.ColWidth(2)
        txtSizer.Font = myFlexGrid.Font
        txtSizer.Text = myFlexGrid.Text

        'Call API to determine how many lines of text are in text box
        lLinesOfText = SendMessage(txtSizer.hwnd, EM_GETLINECOUNT, 0&, 0&)

        ' Update the running values
        lTotalNumberOfRows = lTotalNumberOfRows + lLinesOfText
        lCurrentHeight = lCurrentHeight + myFlexGrid.CellHeight
    Next i

    ' resize the grid
    Dim iSpacers As Integer
    iSpacers = Screen.TwipsPerPixelY * lTotalNumberOfRows
    myFlexGrid.Height = lCurrentHeight + iSpacers

您需要声明 SendMessage(请参阅此处看看如何)和 EM_GETLINECOUNT 的值,但你应该能够自己做到这一点:-)

它不会删除幻数,但它确实使它们合理化,这对我来说已经足够接近了。

This is the final code I came up with

    For i = 0 To fgrComments.Rows - 1
        'Set MSFlexGrid to appropriate Cell
        myFlexGrid.Row = i

        'Set textbox to match the selected cell
        txtSizer.Width = myFlexGrid.ColWidth(2)
        txtSizer.Font = myFlexGrid.Font
        txtSizer.Text = myFlexGrid.Text

        'Call API to determine how many lines of text are in text box
        lLinesOfText = SendMessage(txtSizer.hwnd, EM_GETLINECOUNT, 0&, 0&)

        ' Update the running values
        lTotalNumberOfRows = lTotalNumberOfRows + lLinesOfText
        lCurrentHeight = lCurrentHeight + myFlexGrid.CellHeight
    Next i

    ' resize the grid
    Dim iSpacers As Integer
    iSpacers = Screen.TwipsPerPixelY * lTotalNumberOfRows
    myFlexGrid.Height = lCurrentHeight + iSpacers

You will need to Declare the SendMessage (see here to see how) and the value of EM_GETLINECOUNT, but you should be able to do that yourself:-)

It doesn't remove the magic numbers but it does rationalise them which is close enough for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文