The LVM_APPROXIMATEVIEWRECT message has been around forever but most developers don't use it. Some may have given it a try but abandoned it because it didn't return the values they wanted. Unfortunately, Microsoft dropped the ball on this one. There is no instruction manual or useful documentation on how to use this message correctly. See the Considerations section for more stuff.
The LVM_APPROXIMATEVIEWRECT message is used to calculate the approximate width and height of a ListView control required to display a given number of rows. Hopefully this document and these examples will help to use this message more effectively.
Stuff to Know
The LVM_APPROXIMATEVIEWRECT message can only be used on an existing ListView control. The reason for this is important to understand. The font, column header (or lack of one), icon size, number of rows, number of columns, and the width of each column, etc. all factor into the size of the ListView control and whether or not vertical and/or horizontal scroll bars are showing. Accurately calculating the size of the ListView control would be impossible without having all of this information.
In order to get accurate results, the developer must:
- Create the ListView control. The ListView control must have all the options, styles, and attributes that will be set when the control is displayed. This includes the font, column header, image list, etc.
- Populate the ListView control. This step is optional if the column width can be set without real data or if the LVM_APPROXIMATEVIEWRECT message is only used to calculate the height of the control.
- Set the width of the columns. The step is optional if the LVM_APPROXIMATEVIEWRECT message is only used to calculate the height of the control.
To avoid posting a bunch of code, the examples and function are attached to this post in a Zip file.
A few considerations:
- Trial & Error. The documentation for the LVM_APPROXIMATEVIEWRECT message is very limited. Worse yet, examples out in the wild are mostly nonexistent. For these reasons, the stuff in this document and the examples is mostly from trial and error. If you run into any problems, let me know.
- Report Only. The LVM_APPROXIMATEVIEWRECT message only appears to work with a ListView control in Report mode. Values returned while in other view modes are worthless.
- Proposed Dimensions. The LVM_APPROXIMATEVIEWRECT message has a parameter (lParam) to enter a "proposed" size. I have never figured out how to get the "proposed" feature to work. Setting this parameter to any value does nothing different. The feature may have never been implemented or it may be my version of Windows (Win7). If anyone has figured out how to use this parameter, please let me know.
- LVM_APPROXIMATEVIEWRECT message
- https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Some of the adjustments to the input/output of the LVM_APPROXIMATEVIEWRECT message were extracted from the AutoHotkey source. I've never seen these adjustments documented anywhere else so I would guess that they were created from trial and error.