How to 'manually' calculate the listbox thumb position relative to the item top index?

Eliza 21 Reputation points

How do windows calculate the top index relative to the scrollbar thumb position?

Example, when the thumb is at:

  • y17, then the top index is 0
  • y25, top index is 1
  • y32, top index is 2
  • etc


I'm working on creating/customizing a custom list box where each row can have a different height.

I found it easy to create a separate control to 'mimic' the scrollbar than try to customize the Listbox built-in scrollbar.

A draft: 189404-2022-04-01-19-41-39.gif

The problem is: whenever a new item is added to the list box I need to recalculate all this information of top index/thumb position again because these values changes according to the number of items/height, and this information is needed to 'mimic' the built-in Listbox scrollbar.

How I'm getting this info: a loop that iterates all Listbox items:


SendMessage with WM_VSCROLL and SB_LINEDOWN
to scroll one line down, and move between the indexes.

2- GetScrollBarInfo
to retrieve the thumb y position.

to retrieve the top index relative to the current thumb position.

But this loop (1-2-3) process takes a lot of time.

Does someone know if there's any 'formula' to calculate this information?

I mean, when the thumb is at y, the top index is x;

Or if there's a 'faster' option than looping all indexes as I'm doing.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,520 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,634 questions
{count} votes