Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alternating Icons w/ DrawImages?
#1
Code:
Copy      Help
CB_DrawImages hDlg message wParam lParam 3 "\King Mofo\User - Boy.ico[]\King Mofo\User - Girl.ico"

There is what I tried, but the icon doesn't change with each new item that's added to the CB. I'm not using preset text in the CB. How can I make my two icons alternate for each new item added to the CB. BTW i'm using the QM Drag Drop code to add the items.
#2
added flag 2.
Function CB_DrawImages
Code:
Copy      Help
;/
function# hDlg message wParam lParam ctrlId $icons [flags] [imgWidth] [imgHeight] [dtFlags] ;;flags: 1 listbox (default combobox), 2 item data is image index, 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.
;flags - 1 the control is a listbox. 2 item image index is stored in item data (default - same as item index). 16 use bitmap (default - use icon).
;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 RemoveProp(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")
,,int ii=iif(flags&2 ds.itemData ds.itemID)
,,n=ImageList_GetImageCount(il); if(ii>=n) ii=n-1
,,ImageList_Draw il ii 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
Function dialog_od_combo2
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

str controls = "3"
str lb3
if(!ShowDialog("dialog_od_combo2" &dialog_od_combo2 &controls)) ret
out lb3

;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 ListBox 0x54230151 0x200 4 104 96 36 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2020100 "" ""

ret
;messages

CB_DrawImages hDlg message wParam lParam 3 "mouse.ico[]keyboard.ico" 1|2

sel message
,case WM_INITDIALOG
,int h=id(3 hDlg)
,LB_Add h "one" 0
,LB_Add h "two" 1
,LB_Add h "three" 0
,LB_Add h "four" 1
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
#3
This version also supports bmp file created by the QM imagelist editor. Requires QM 2.3.0 or later.
Function CB_DrawImages
Code:
Copy      Help
;/
function# hDlg message wParam lParam ctrlId $icons [flags] [imgWidth] [imgHeight] [dtFlags] ;;flags: 1 listbox (default combobox), 2 item data is image index, 4 icons is imagelist, 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. If flag 4 used, imagelist file.
;flags:
;;;;1 the control is a listbox.
;;;;2 item image index is stored in item data (default - same as item index).
;;;;4 icons is .bmp file created with QM imagelist editor.
;;;;16 use bitmap (default - use icon). Don't use with flag 4.
;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
,if(flags&4)
,,il=__ImageListLoad(icons)
,else
,,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 RemoveProp(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")
,,int ii=iif(flags&2 ds.itemData ds.itemID)
,,n=ImageList_GetImageCount(il); if(ii>=n) ii=n-1
,,ImageList_Draw il ii 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
Function dialog_od_combo3
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

;Shows how to draw icons in combo box using a bmp file created by the QM imagelist editor.

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

;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 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2020100 "" ""

ret
;messages

CB_DrawImages hDlg message wParam lParam 3 "$qm$\il_qm.bmp" 2|4
;note: il_qm.bmp is used by QM. It may be changed or deleted in the future. Create your own file with the imagelist editor. You can find the editor in the Icons dialog or in the floating toolbar.

sel message
,case WM_INITDIALOG
,int h=id(3 hDlg)
,CB_Add h "one" 0
,CB_Add h "two" 1
,CB_Add h "three" 15
,CB_Add h "four" 16
,CB_SelectItem h 0
,;third argument of LB_Add is icon index in the imagelist
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
#4
Thanks for the great help yet once again Gint.
Best Regards


Forum Jump:


Users browsing this thread: 1 Guest(s)