Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set ComboBox dropdown list width
#1
Hi Gintaras

I would like to expand/reduce the dropped width of an editable combobox to the lenght of the longest item.

I looked it up in msdn libary and found CB_SETDROPPEDWIDTH but how do you measure and compare the text lenght of all added items? and how do u keep it updating if one of the items gets deleted or added?

I know i have to work with WM_MEASUREITEM / MEASUREITEMSTRUCT but cant work out how to use it correctly.

I didnt give up yet but any help would be appreciated.

thank you
#2
Function CalculateTextSize
Code:
Copy      Help
;/
function $s SIZE&z [hFont] [dtFlags]

;Calculates text size for display on screen or in a control.

;s - text. Can be multiline.
;z - variable that receives text width and height.
;hFont - font handle. If 0 or omitted, uses default dialog font.
;dtFlags - flags for DrawText (look in MSDN library).
;;;These flags can be useful: DT_EXPANDTABS (include tabs), DT_NOPREFIX (include &), DT_SINGLELINE (ignore newlines)

;REMARKS
;Uses DrawText API with DT_CALCRECT flag and screen device context.


RECT r
int dc oldfont
dc=CreateCompatibleDC(0); oldfont=SelectObject(dc iif(hFont hFont _hfont))

if(DrawTextW(dc @s -1 &r dtFlags|DT_CALCRECT)) z.cx=r.right-r.left; z.cy=r.bottom-r.top
else z.cx=0; z.cy=0

SelectObject(dc oldfont); DeleteDC(dc)

example dialog
Function dialog_ComboBox_AutoWidth
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

str controls = "3"
str cb3
if(!ShowDialog("dialog_ComboBox_AutoWidth" &dialog_ComboBox_AutoWidth &controls)) ret

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;3 ComboBox 0x54230242 0x0 6 6 96 213 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030400 "*" "" ""

ret
;messages
sel message
,case WM_INITDIALOG
,;__Font-- t_font.Create("Arial" 14)
,;t_font.SetDialogFont(hDlg)
,
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case CBN_DROPDOWN<<16|3 ;;before combo box dropdown
,int hcb=lParam ;;lParam is combo box control handle
,;create list of random strings for testing
,str s
,rep(2) s.addline(_s.RandomString(1 60))
,;add items to the control
,SendMessage hcb CB_RESETCONTENT 0 0
,foreach(_s s) CB_Add hcb _s ;;add item for each line
,;calc max width
,SIZE z
,CalculateTextSize s z SendMessage(hcb WM_GETFONT 0 0) DT_NOPREFIX
,;set combo box list width
,SendMessage hcb CB_SETDROPPEDWIDTH z.cx+10 0
,
,case IDOK
,case IDCANCEL
ret 1
#3
Great! Thank you very much.

best regards
#4
Hi

Not that i want to complain its just for your information.

It doesnt work right with CB_DrawImages.

changed it to

Code:
Copy      Help
SendMessage hcb4 CB_SETDROPPEDWIDTH z.cx+18 0

Thanks again.


Forum Jump:


Users browsing this thread: 1 Guest(s)