In the end, the answer, as often happens, is simpler than it might seem.
I write here what I found, maybe it can be useful for someone else.
When an item is created in the ListBox, immediately after the LB_ADDSTRING or LB_INSERTSTRING messages, the control sends a WM_MEASUREITEM to the parent.
At this point, the data part of the item is not ready yet, because the LB_SETITEMDATA message is processed only after the WM_MEASUREITEM message, so the only way to calculate the height of an item is before inserting it into the ListBox, and keep the value available outside the data part of the item to use it during the measuring stage.
It's also possible to ignore the WM_MEASUREITEM message, or use a constant value, and send a LB_SETITEMHEIGHT message, with the correct calculated value, during the drawing process of the items. When the WM_DRAWITEM message is sent from the ListBox, the LB_SETITEMDATA has already been processed, so it is possible to calculate all the correct value using every detail of the item.
This method works, but the vertical scroll bar, which values are calculated using the data collected from WM_MEASUREITEM, could not work correctly, unless the user manually scrolls the items some times after the insertion.