Here is how i solve this:

TempWidth dword 0

hdc HDC ?
hListBox dword ?
cxChar dword ?
index dword ?
rect RECT <?>
bftemp byte 512 dup(?)
string1 byte 512 dup(?)


invoke GetDC,hListBox
mov hdc,eax
invoke GetTextMetrics,hdc,addr tm
mov eax,tm.tmAveCharWidth
mov cxChar,eax
invoke ReleaseDC,hListBox,hdc
invoke SendMessage,hListBox,LB_INSERTSTRING,index,addr string1
; set selection to item
invoke SendMessage,hListBox,LB_SETSEL,TRUE,index
; set caret to item
invoke SendMessage,hListBox,LB_SETCARETINDEX,index,FALSE ; full visible
; get text length without 0 terminate
invoke SendMessage,hListBox,LB_GETTEXT,index,addr bftemp
mul cxChar
push eax ; save
invoke GetWindowRect,hListBox,addr rect
mov eax,rect.right
sub eax,rect.left
pop edx ; restore
.if eax<edx
.if edx>TempWidth
mov TempWidth,edx
; set width in pixels and show horizontal scroll
invoke SendMessage,hListBox,LB_SETHORIZONTALEXTENT,edx,0
; remove selection from item
invoke SendMessage,hListBox,LB_SETSEL,FALSE,index

Simple. Get average width of font in list box control. Save this value.
Insert first string, select this string and set caret to it. Now get length
of this added string (without 0) and multiply number of chars with average
width of char in list box. Save this value. Get rectangle of list box and
calculate width of this list box. Restore width of all chars in selected item.
If width of list box is smaller then width of all chars in selected item
then continue to next condition, else remove selection from current selected
item and continue to next item.
If condition is true (eax is less then edx), check if width of chars is larger
then temp. width (in first step this temp. width is 0). If condition is true
save width of all chars, show horizontal scroll bar with new size. You can scroll
enough to see complete item string. Remove selection and go to next item.

Maybe someone needs this.

Posted on 2004-01-13 15:45:46 by stanks
I have to create listbox control with WS_VSCROLL and WS_HSCROLL styles.

Posted on 2004-01-13 23:41:23 by stanks