Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Color Highlight of ListBox or ComboBox Entries
#2
Function CB_ItemColor
Code:
Copy      Help
;/
function# hDlg message wParam lParam ctrlId callbackFunc [param]

;Draws combo or list box items with custom background and text colors.

;This is how to create a combo or list box and set colors:
;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.
;;;;For list box, select LBS_OWNERDRAWFIXED and LBS_HASSTRINGS styles.
;4. Create callback function similar to SampleCbItemColorProc. The function sets colors.
;5. In the dialog function, call CB_ItemColor before 'sel messages' line. Pass address of the callback function. Call it for each such control.

;hDlg, message, wParam, lParam - hDlg, message, wParam, lParam.
;ctrlId - combo/list box id.
;callbackFunc - address of a callback function. 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

sel message
,case WM_DRAWITEM
,if(wParam=ctrlId)
,,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=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
,,
,,call callbackFunc &c
,,
,,SetBkMode ds.hDC TRANSPARENT
,,__GdiHandle brush=CreateSolidBrush(c.bkColor)
,,FillRect ds.hDC &r brush
,,SetTextColor ds.hDC c.textColor
,,
,,r.left+4; r.top+2
,,DrawTextW ds.hDC @c.text -1 &r c.dtFlags

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

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("dialog_combo_item_colors" &dialog_combo_item_colors &controls)) ret
out cb3
out cb4
out lb5

;BEGIN DIALOG
;0 "" 0x90C80A44 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 "" "" ""

ret
;messages

CB_ItemColor hDlg message wParam lParam 3 &SampleCbItemColorProc
CB_ItemColor hDlg message wParam lParam 4 &SampleCbItemColorProc
CB_ItemColor hDlg message wParam lParam 5 &SampleCbItemColorProc

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

sample callback function
Function SampleCbItemColorProc
Code:
Copy      Help
;/
function# CBITEMCOLOR&c

;This is a sample callback function that can be used with CB_ItemColor.
;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: 1 Guest(s)