02-13-2011, 08:48 AM
Function dlg_listbox_multiline
Function DT_LbCbOwnerDraw
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "3 4"
str lb3 cb4
;note: don't use dialog variable to add ownerdraw listbox/combobox items.
if(!ShowDialog("dlg_listbox_multiline" &dlg_listbox_multiline &controls)) ret
;note: in dialog editor, select the listbox control and add styles LBS_OWNERDRAWVARIABLE and LBS_HASSTRINGS. Similar with combobox.
;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 ListBox 0x54230161 0x200 0 0 108 114 ""
;4 ComboBox 0x54230262 0x0 118 0 102 215 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "" "" ""
ret
;messages
;call this function before sel message
if(DT_LbCbOwnerDraw(hDlg message wParam lParam 0 3)) ret 1
sel message
,case WM_INITDIALOG
,;optionally set font
,__Font-- t_f.Create("Comic Sans MS" 10 2)
,t_f.SetDialogFont(hDlg "3 4")
,
,;add items. Or can add/remove later. To remove all, send message LB_RESETCONTENT or CB_RESETCONTENT.
,ARRAY(str) a.create(3)
,a[0]="normal"
,a[1]="line[]line[]line"
,a[2]="wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap wrap"
,int i h
,h=id(3 hDlg)
,for(i 0 a.len) LB_Add h a[i]
,h=id(4 hDlg)
,for(i 0 a.len) CB_Add h a[i]
,
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
Function DT_LbCbOwnerDraw
;/dlg_listbox_multiline
function! hDlg message wParam lParam [ctlId] [flags] ;;flags: 1 wrap lines, 2 draw separators
;Draws items in owner-draw listbox or combobox control.
;Items can have multiple lines.
;Call this function from a dialog function, like in dlg_listbox_multiline.
;Returns 1 if draws, 0 if not.
;hDlg message wParam lParam - hDlg message wParam lParam.
;ctrlId - control id. If 0, draws all owner-draw listbox and combobox controls.
sel(message) case [WM_MEASUREITEM,WM_DRAWITEM] case else ret
int ct cid msg1 msg2
sel message
,case WM_MEASUREITEM
,MEASUREITEMSTRUCT& mi=+lParam
,ct=mi.CtlType
,cid=mi.CtlID
,
,case WM_DRAWITEM
,DRAWITEMSTRUCT& di=+lParam
,ct=di.CtlType
,cid=di.CtlID
if(ctlId and cid!ctlId) ret
sel ct
,case ODT_LISTBOX msg1=LB_GETTEXT; msg2=LB_GETTEXTLEN
,case ODT_COMBOBOX msg1=CB_GETLBTEXT; msg2=CB_GETLBTEXTLEN
,case else ret
int hwnd i hdc fl
RECT rt
hwnd=id(cid hDlg)
sel message
,case WM_MEASUREITEM
,i=mi.itemID
,hdc=GetDC(hwnd)
,RECT rc; GetClientRect hwnd &rc; rt.right=rc.right
,fl=DT_CALCRECT
,
,case WM_DRAWITEM
,i=di.itemID
,if(i<0) ret
,hdc=di.hDC
,rt=di.rcItem
,
,int colBk colTxt
,if(di.itemState&ODS_SELECTED) colBk=COLOR_HIGHLIGHT; colTxt=COLOR_HIGHLIGHTTEXT
,else colBk=COLOR_WINDOW; colTxt=COLOR_WINDOWTEXT
,FillRect hdc &rt GetSysColorBrush(colBk)
,SetBkMode hdc TRANSPARENT
,SetTextColor hdc GetSysColor(colTxt)
int tl=SendMessageW(hwnd msg2 i 0)
if tl>0
,BSTR s.alloc(tl)
,tl=SendMessageW(hwnd msg1 i s.pstr)
,if tl>0
,,if(flags&1) fl|DT_WORDBREAK
,,int oldfont=SelectObject(hdc SendMessageW(hwnd WM_GETFONT 0 0))
,,DrawTextW(hdc s tl &rt fl|DT_EXPANDTABS|DT_NOPREFIX)
,,SelectObject hdc oldfont
sel message
,case WM_MEASUREITEM
,ReleaseDC hwnd hdc
,mi.itemHeight=rt.bottom
,
,case WM_DRAWITEM
,if(di.itemState&ODS_FOCUS) DrawFocusRect hdc &rt
,else if flags&2
,,int pen=CreatePen(PS_SOLID 1 0xc0c0c0)
,,int oldpen=SelectObject(hdc pen)
,,MoveToEx hdc 0 rt.bottom-1 0; LineTo hdc rt.right rt.bottom-1
,,DeleteObject SelectObject(hdc oldpen)
ret DT_Ret(hDlg 1)