Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MultiLine ListBox
#10
Added parameter: aFont.

Function DT_LbCbOwnerDraw
Code:
Copy      Help
;/
function! hDlg message wParam lParam [ctlId] [flags] [ARRAY(int)&aColorText] [ARRAY(int)&aColorBack] [selColorText] [selColorBack] [ARRAY(int)&aFont] ;;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 procedure, 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.
;aColorText, aColorBack - arrays containing colors of item text and background. The variables should have thread scope. If omitted or 0, uses default colors.
;selColorText, selColorBack - colors of selected item text and background. If omitted or 0, uses default selection colors.
;aFont - array containing font handles of item text. The variable should have thread scope. Can be set only some elements, others can be 0 (default font).


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 colB colT colB_used colT_used
,if(di.itemState&ODS_SELECTED)
,,colB=COLOR_HIGHLIGHT; colT=COLOR_HIGHLIGHTTEXT
,,if(selColorBack) colB=selColorBack; colB_used=1
,,if(selColorText) colT=selColorText; colT_used=1
,else
,,colB=COLOR_WINDOW; colT=COLOR_WINDOWTEXT
,,if(&aColorBack && i<aColorBack.len) colB=aColorBack[i]; colB_used=1
,,if(&aColorText && i<aColorText.len) colT=aColorText[i]; colT_used=1
,if(!colB_used) colB=GetSysColorBrush(colB); else colB=CreateSolidBrush(colB)
,if(!colT_used) colT=GetSysColor(colT)
,FillRect hdc &rt colB; if(colB_used) DeleteObject colB
,SetTextColor hdc colT
,SetBkMode hdc TRANSPARENT

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(&di and di.itemState&ODS_COMBOBOXEDIT) fl|DT_SINGLELINE
,,else if(flags&1) fl|DT_WORDBREAK
,,
,,int font
,,if(&aFont && i<aFont.len) font=aFont[i]
,,if(!font) font=SendMessageW(hwnd WM_GETFONT 0 0)
,,int oldfont=SelectObject(hdc font)
,,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)

example
Function dlg_listbox_colors_fonts_multiline
Code:
Copy      Help
\Dialog_Editor

str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;3 ListBox 0x54230161 0x200 0 0 108 114 ""
;4 ComboBox 0x54230262 0x0 118 14 102 215 ""
;5 ComboBox 0x54230263 0x0 118 54 102 215 ""
;6 Static 0x54000000 0x0 118 4 78 10 "Editable combo"
;7 Static 0x54000000 0x0 118 44 78 10 "Read-only combo"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "" "" ""

str controls = "3 4 5"
str lb3 cb4 cb5
;note: don't use dialog variable to add ownerdraw listbox/combobox items.

if(!ShowDialog(dd &sub.DlgProc &controls)) ret

;note: in dialog editor, select the listbox control and add styles LBS_OWNERDRAWVARIABLE and LBS_HASSTRINGS. Similar with combobox.


#sub DlgProc
function# hDlg message wParam lParam

;arrays for colors and fonts
ARRAY(int)-- t_act t_acb t_afont

;call this function before sel message
if(DT_LbCbOwnerDraw(hDlg message wParam lParam 0 1 t_act t_acb 0xffff 0x1 t_afont)) ret 1

sel message
,case WM_INITDIALOG
,;optionally set common font
,__Font-- t_f.Create("Comic Sans MS" 10 2)
,t_f.SetDialogFont(hDlg "3 4 5")
,
,;create arrays for colors
,t_act.create(4); t_act[0]=0xff; t_act[1]=0x8000; t_act[2]=0xff0000
,t_acb.create(4); t_acb[0]=0xffC0C0; t_acb[1]=0xC0FFff; t_acb[2]=0xC0ffC0; t_acb[3]=0xffffff
,
,;create array for fonts
,__Font-- t_f1.Create("Tahoma" 15 1) t_f2.Create("" 0 4 0 1 4)
,t_afont.create(4); t_afont[1]=t_f1; t_afont[2]=t_f2; t_afont[3]=t_f1 ;;and t_afont[0]=0 - default font
,
,;add items. Or can add/remove later. To remove all, send message LB_RESETCONTENT or CB_RESETCONTENT.
,ARRAY(str) a.create(4)
,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"
,a[3]="last"
,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]
,h=id(5 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


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)