0

我在详细信息模式下有一个 VB6 列表视图,显示 X 个项目。我想创建某种自动高度函数,它根据指定的宽度和项目数评估高度。

以下代码不起作用:

Public Declare Function MessageSend Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const LVM_FIRST As Long = &H1000
Public Const LVM_APPROXIMATEVIEWRECT    As Long = (LVM_FIRST + 64)

Public Sub GetAutoWidth(lvw as ListView)
    result = MakeLPAram(-1, 0)
    MessageSend lvw.hWnd, LVM_APPROXIMATEVIEWRECT, -1&, result
    lvw.height = loWord(result) * Screen.TwipsPerPixelX
End Sub

Public Function LoWord(value As Long) As Long
    LoWord = (value And &HFFFF&)
End Function

Public Function MakeLPAram(wLow As Long, wHigh As Long) As Long
    MakeLPAram = MakeLong(wLow, wHigh)
End Function

Public Function MakeLong(wLow As Long, wHigh As Long) As Long
    MakeLong = LoWord(wLow) Or (&H10000 * LoWord(wHigh))
End Function

谁能帮我这个?

4

1 回答 1

1

首先,您不在LVM_APPROXIMATEVIEWRECT这里保存消息的返回值:

MessageSend lvw.hWnd, LVM_APPROXIMATEVIEWRECT, -1&, result

您应该,因为返回值正是您首先发送消息的原因:

返回值

返回一个 DWORD 值,该值包含显示项目所需的近似宽度(在 LOWORD 中)和高度(在 HIWORD 中),以像素为单位。

其次,您的命名(GetAutoWidth),您的问题标题(“计算ListView高度”)和您的代码(lvw.height = loWord(result) * Screen.TwipsPerPixelX)非常不一致。

第三,阅读以下 MSDN 文章,因为您用于打包/解包单词和双字的代码也存在缺陷:

如何在 VB 中模拟 HIWORD、LOWORD、HIBYTE、LOBYTE C 宏

如何将 HiWord/LoWord 值打包成长参数

于 2013-05-01T23:38:39.597 回答