Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combo/list box with icons/bmp/jpg/gif
#1
Function CB_DrawImages
Code:
Copy      Help
;/
function# hDlg message wParam lParam ctrlId $icons [flags] [imgWidth] [imgHeight] [dtFlags] ;;flags: 1 listbox (default combobox), 16 not icons (bmp, jpg, gif)

;Adds icons or bmp/jpg/gif images to a combo box or list box.

;This is what you have to do to create a combo or list box with icons or images:
;1. The dialog must be a smart dialog, ie with dialog function.
;2. In the dialog editor, add a combo or list box.
;3. In the Styles dialog:
;;;;For combo box, select CBS_OWNERDRAWFIXED and CBS_HASSTRINGS styles. Make sure that CBS_SORT is not selected (sorted combo boxes are not supported).
;;;;For list box, select LBS_OWNERDRAWFIXED and LBS_HASSTRINGS styles. Make sure that LBS_SORT is not selected (sorted list boxes are not supported).
;4. In the dialog function, insert CB_DrawImages before 'sel messages' line.

;hDlg, message, wParam, lParam - hDlg, message, wParam, lParam.
;ctrlId - combo/list box id.
;icons - list of icon files. If flag 16 used, list of image files of type bmp, jpg or gif.
;imgWidth, imgHeight - width and height of images. Default: 16 pixels.
;dtFlags - DrawText flags. Documented in the MSDN Library on the Internet. Xored with DT_NOPREFIX.


int i n il hi hb hcb; str s
hcb=id(ctrlId hDlg)
if(!imgWidth) imgWidth=16
if(!imgHeight) imgHeight=16

sel message
,case WM_CREATE
,SendMessage hcb iif(flags&1 LB_SETITEMHEIGHT CB_SETITEMHEIGHT) 0 imgHeight+2
,
,case WM_INITDIALOG
,n=numlines(icons)
,il=ImageList_Create(imgWidth imgHeight ILC_MASK|ILC_COLOR32 0 n)
,for(i 0 n)
,,s.getl(icons -i)
,,if(flags&16)
,,,hb=LoadPictureFile(s 0); if(!hb) goto g1
,,,ImageList_Add(il hb 0)
,,,if(hb) DeleteObject hb
,,else
,,,hi=GetFileIcon(s 0 (imgWidth>=24 or imgHeight>=24))
,,,;g1
,,,ImageList_ReplaceIcon(il -1 iif(hi hi _dialogicon))
,,,if(hi) DestroyIcon hi
,SetProp hcb "qm_il" il
,
,case WM_DESTROY
,ImageList_Destroy GetProp(hcb "qm_il")
,
,case WM_DRAWITEM
,if(wParam=ctrlId)
,,DRAWITEMSTRUCT* ds=+lParam
,,RECT r=ds.rcItem
,,;background
,,int selected=ds.itemState&(ODS_SELECTED|ODS_COMBOBOXEDIT)=ODS_SELECTED
,,FillRect ds.hDC &ds.rcItem iif(selected COLOR_HIGHLIGHT COLOR_WINDOW)+1
,,;icon
,,il=GetProp(hcb "qm_il")
,,n=ImageList_GetImageCount(il); if(ds.itemID<n) n=ds.itemID; else n-1
,,ImageList_Draw il n ds.hDC r.left+1 r.top+1 ILD_NORMAL
,,;text
,,SetBkMode ds.hDC TRANSPARENT
,,SetTextColor ds.hDC GetSysColor(iif(selected COLOR_HIGHLIGHTTEXT COLOR_WINDOWTEXT))
,,GetItemText ds.hWndItem ds.itemID s flags&1
,,r.left+imgWidth+4; r.top+2
,,DrawText ds.hDC s -1 &r dtFlags^DT_NOPREFIX

Example
Creates dialog with 4 owner-drawn controls - read-only combo with small icons, editable combo with large icons, list box with small icons, list box with bmp/jpg/gif images.
Function dialog_od_combo
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

str controls = "3 4 5 6"
str cb3 cb4 lb5 lb6
cb3="&one[]two[]three"
cb4="&one[]two[]three"
lb5="&one[]two[]three"
lb6="&one[]two[13]lines and with word wrap[]three"
if(!ShowDialog("dialog_od_combo" &dialog_od_combo &controls)) ret
out cb3
out cb4
out lb5
out lb6

;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 204 164 "Dialog"
;1 Button 0x54030001 0x4 4 146 48 14 "OK"
;2 Button 0x54030000 0x4 56 146 48 14 "Cancel"
;3 ComboBox 0x54230253 0x0 4 6 96 215 ""
;4 ComboBox 0x54230252 0x0 4 26 96 215 ""
;5 ListBox 0x54230151 0x200 4 104 96 36 ""
;6 ListBox 0x54230151 0x200 110 6 90 134 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2020100 "" ""

ret
;messages

CB_DrawImages hDlg message wParam lParam 3 "mouse.ico[]qm.exe[]qm.exe,3"
CB_DrawImages hDlg message wParam lParam 4 "shell32.dll,0[]shell32.dll,1[]shell32.dll,2" 0 32 32 DT_VCENTER|DT_SINGLELINE
CB_DrawImages hDlg message wParam lParam 5 "keyboard.ico[]qm.exe,1[]qm.exe,4" 1
CB_DrawImages hDlg message wParam lParam 6 "$My Pictures$\60x90.bmp[]$My Pictures$\60x90.jpg[]$My Pictures$\60x90.gif" 1|16 60 90 DT_WORDBREAK

sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

Similar code also can be used to draw owner-drawn controls of other types (those that support WM_DRAWITEM). Other control types also may require to process WM_MEASUREITEM.
   
#2
CB_DrawImages hDlg message wParam lParam 4 "mouse.ico" 1


how can i add mouse.ico to each index, no matter how many there are?

thanks
#3
I updated CB_DrawImages code here. Now your code will work.
#4
thank you
#5
Is it possible to reload the listbox (lb6) with other image and text data without having to restart the whole dialog?
#6
With this function no. It creates imagelist when dialog starts, and cannot change it later.
#7
Ok thanks!


Forum Jump:


Users browsing this thread: 2 Guest(s)