Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Color Highlight of ListBox or ComboBox Entries
#9
Hybrid: CB_DrawImages + CB_ItemColor = CB_ItemImageAndColor.

Function CB_ItemImageAndColor
Code:
Copy      Help
;/
function# hDlg message wParam lParam ctrlId [$icons] [flags] [imgWidth] [imgHeight] [dtFlags] [callbackFunc] [param] ;;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.
;Also can draw items with custom background and text colors.

;This is what you have to do to create a combo or list box:
;1. The dialog must be a smart dialog, ie with dialog procedure.
;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 procedure, insert CB_ItemImageAndColor before 'sel messages' line, for each such control. If need colors, create a callback function and pass its address.

;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. Can be 0 if don't need images.
;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.
;callbackFunc - address of a callback function that sets text colors. Optional. See SampleCbItemColorProc.
;param - a user-defined value that will be passed to the callback function.


type CBITEMCOLOR str'text bkColor textColor hwnd item itemData !selected !isLB dtFlags param

int i n il hi hb hcb; str s
hcb=id(ctrlId hDlg)
if(!imgWidth and icons) 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 icons
,,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
,if(icons) ImageList_Destroy RemoveProp(hcb "qm_il")
,
,case WM_DRAWITEM
,if(wParam=ctrlId) goto gDraw
ret

;gDraw
DRAWITEMSTRUCT* ds=+lParam
if(ds.itemID<0) ret
RECT r=ds.rcItem

CBITEMCOLOR c
c.hwnd=ds.hWndItem
c.item=ds.itemID
c.itemData=ds.itemData
c.isLB=ds.CtlType=ODT_LISTBOX
c.param=param
c.dtFlags=dtFlags^DT_NOPREFIX
c.selected=ds.itemState&(ODS_SELECTED|ODS_COMBOBOXEDIT)=ODS_SELECTED
c.bkColor=GetSysColor(iif(c.selected COLOR_HIGHLIGHT COLOR_WINDOW))
c.textColor=GetSysColor(iif(c.selected COLOR_HIGHLIGHTTEXT COLOR_WINDOWTEXT))
GetItemText c.hwnd c.item c.text c.isLB

if(callbackFunc) call callbackFunc &c

;background
__GdiHandle brush=CreateSolidBrush(c.bkColor)
FillRect ds.hDC &r brush
;icon
if icons
,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 0
;text
SetBkMode ds.hDC TRANSPARENT
SetTextColor ds.hDC c.textColor
r.left+imgWidth+4; r.top+2
DrawTextW ds.hDC @c.text -1 &r c.dtFlags

example
Macro dialog_combo_images_and_colors
Code:
Copy      Help
\Dialog_Editor

str dd=
;BEGIN DIALOG
;0 "" 0x90C80A48 0x100 0 0 204 98 "Dialog"
;1 Button 0x54030001 0x4 4 80 48 14 "OK"
;2 Button 0x54030000 0x4 56 80 48 14 "Cancel"
;3 ComboBox 0x54230253 0x0 4 6 96 215 ""
;4 ComboBox 0x54230252 0x0 4 30 96 215 ""
;5 ListBox 0x54230151 0x200 106 6 96 64 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030109 "" "" ""

str controls = "3 4 5"
str cb3 cb4 lb5
cb3="&yellow background[]blue text[]default colors[]black & green"
cb4="&yellow background[]blue text[]default colors[]black & green"
lb5="yellow background[]blue text[]default colors[]black & green"
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
out cb3
out cb4
out lb5


#sub DlgProc
function# hDlg message wParam lParam

CB_ItemImageAndColor hDlg message wParam lParam 3 "$qm$\browse.ico[]$qm$\cut.ico[]$qm$\copy.ico[]$qm$\paste.ico" 0 0 0 0 &sub.ColorCallback
CB_ItemImageAndColor hDlg message wParam lParam 4 "shell32.dll,3" 0 32 32 DT_VCENTER|DT_SINGLELINE &sub.ColorCallback
CB_ItemImageAndColor hDlg message wParam lParam 5 "shell32.dll,3" 1 32 32 DT_WORDBREAK &sub.ColorCallback

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


#sub ColorCallback
function# CBITEMCOLOR&c

;This is a sample callback function that can be used with CB_ItemImageAndColor.
;Shows how to set combo/list box item background and text colors depending on item text.
;The function will be called for each item when displaying it.
;c contains various info that can be useful to draw the item.
;To set colors, change c.bkColor and/or c.textColor.
;Also you can change c.dtFlags and c.text.

;c.text - item text. In/out.
;c.bkColor - item background color. In/out.
;c.textColor - item text color. In/out.
;c.hwnd - control handle.
;c.item - item index.
;c.itemData - item data. To set item data, use message CB_SETITEMDATA or LB_SETITEMDATA, documented in MSDN.
;c.selected - 1 if the item is selected, 0 if not.
;c.isLB - 1 if the control is listbox, 0 if combobox.
;c.dtFlags - DrawText flags. Documented in the MSDN Library on the Internet. In/out.
;c.param - an user-defined value, passed to CB_ItemColor.


if(c.selected) ret

sel c.text
,case "yellow background" c.bkColor=0xa0ffff
,case "blue text" c.textColor=0xff0000
,case "black & green" c.bkColor=0; c.textColor=0x00ff00


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)