WinApi:计算ListView高度

时间:2013-05-01 12:43:34

标签: winapi listview vb6

我在详细信息模式下有一个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

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

  

返回值

     

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

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

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

How to Mimic HIWORD, LOWORD, HIBYTE, LOBYTE C Macros in VB

How To Package HiWord/LoWord Values Into a Long Parameter