/* This is an advanced listview demo. The listview is created in report mode and two columns are added. The colours of the column headers are changed and the columns are prevented from resizing. Also shown are extended styles. Requires iwbasic 1.0 or greater Compile as a WINDOWS target */ $include "windowssdk.inc" 'for subclassing the listview CONTROL UINT hLV,origfp 'for subclassing the header CONTROL of the listview UINT hHC,origfpHC 'Necessary constants CONST GWL_WNDPROC = -4 CONST HDN_FIRST = (-300) CONST HDN_BEGINTRACKA = (HDN_FIRST-6) CONST HDN_BEGINTRACKW = (HDN_FIRST-26) CONST WM_NOTIFY = 0x4E CONST WM_SETCURSOR = 0x20 CONST NM_CUSTOMDRAW = (-12) CONST CDDS_PREPAINT = 1 CONST CDDS_ITEM = 0x10000 CONST CDDS_SUBITEM = 0x20000 CONST CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT) CONST CDDS_SUBITEMPREPAINT = (CDDS_SUBITEM | CDDS_ITEMPREPAINT) CONST CDRF_NOTIFYITEMDRAW = 0x20 CONST CDRF_NOTIFYSUBITEMDRAW = 0x20 CONST CDRF_DODEFAULT = 0 CONST CDRF_NEWFONT = 2 CONST DWL_MSGRESULT = 0 CONST LVM_FIRST = 0x1000 CONST LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54) CONST LVS_EX_FULLROWSELECT = 0x20 CONST LVS_EX_GRIDLINES = 1 CONST LVS_EX_FLATSB = 0x100 CONST LVS_EX_LABELTIP = 0x4000 DIALOG d1 'standard NMLISTVIEW UDT 'Windows sends a variable of this type in @LPARAM during a notification message. TYPE NMLISTVIEW UINT hWndFrom INT idFrom INT code INT iItem INT iSubItem UINT uNewState UINT uOldState UINT uChanged INT ptActionx INT ptActiony INT lParam ENDTYPE 'type for handling custom drawing of the header CONTROL TYPE NMCUSTOMDRAWINFO NMHDR hdr UINT dwDrawStage UINT hDC WINRECT rc UINT dwItemSpec UINT uItemState UINT lItemlParam ENDTYPE 'type for handling custom drawing of the list view CONTROL TYPE NMLVCUSTOMDRAW NMCUSTOMDRAWINFO nmcd UINT clrText UINT clrTextBk 'if internet explore version >= 4.0 int iSubItem ENDTYPE 'variable indicating whether columns are resizeable or not int bLocked = 1 'create the dialog and add the controls CREATEDIALOG d1,0,0,296,170,0x80C80080,0,"Advanced Listview demo",&handler CONTROL d1,@LISTVIEW,"",10,10,276,110,@LVSREPORT|@BORDER|@LVSSHOWSELALWAYS,100 CONTROL d1,@CHECKBOX,"Lock column resize",27,125,240,20,0,300 CONTROL d1,@STATIC,"Click on column headers",27,150,240,20,0,200 'show the dialog and wait for it to close DOMODAL d1 end 'the message handler for the dialog SUB handler(),INT select @MESSAGE case @IDINITDIALOG CENTERWINDOW d1 'insert columns and some items 'after an item is inserted we use @LVSETTEXT to change the subitems text CONTROLCMD d1,100,@LVINSERTCOLUMN,0,"Column1" CONTROLCMD d1,100,@LVINSERTCOLUMN,1,"Column2" CONTROLCMD d1,100,@LVINSERTCOLUMN,2,"Column3" CONTROLCMD d1,100,@LVINSERTCOLUMN,3,"Column4" 'the first columns item and sub items CONTROLCMD d1,100,@LVINSERTITEM,0,"Item 1" CONTROLCMD d1,100,@LVSETTEXT,0,1,"Sub 1" CONTROLCMD d1,100,@LVSETTEXT,0,2,"Sub 2" 'the second column CONTROLCMD d1,100,@LVINSERTITEM,1,"Item 2" CONTROLCMD d1,100,@LVSETTEXT,1,1,"Sub 1" 'the third column CONTROLCMD d1,100,@LVINSERTITEM,2,"Item 3" 'the fourth column CONTROLCMD d1,100,@LVINSERTITEM,3,"Item 4" CONTROLCMD d1,100,@LVSETTEXT,0,3,"Sub 3" 'change some of the listview extended styles SENDMESSAGE d1,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_LABELTIP,100 'Set column widths CONTROLCMD d1,100,@LVSETCOLWIDTH,0,68 CONTROLCMD d1,100,@LVSETCOLWIDTH,1,68 CONTROLCMD d1,100,@LVSETCOLWIDTH,2,68 CONTROLCMD d1,100,@LVSETCOLWIDTH,3,68 'subclass the listview CONTROL hLV = GetDlgItem(d1.hWnd,100) origfp=GetWindowLongA(hLV,GWL_WNDPROC) 'Replace it with our handler... SetWindowLongA(hLV,GWL_WNDPROC,&myLVHandler) 'subclass the header CONTROL hHC = GetDlgItem(hLV,0) 'if listview not in report mode then hHC will be zero so don't attempt to subclass header CONTROL if its not existent IF(hHC) origfpHC=GetWindowLongA(hHC,GWL_WNDPROC) SetWindowLongA(hHC,GWL_WNDPROC,&myHCHandler) ENDIF 'set the check box CONTROL to initially checked SETSTATE d1,300,TRUE case @IDCONTROL SELECT @CONTROLID CASE 100 SELECT @NOTIFYCODE case @LVNCOLUMNCLICK 'someone clicked on a column header. Read the data from @LPARAM which is 'a pointer to a NMLISTVIEW UDT. Using the C dereferencing style makes it much easier SETCONTROLTEXT d1,200,USING("Column ## clicked",*@LPARAM.iSubItem+1) case NM_CUSTOMDRAW 'if we want to change the colors of the actual items and subitems we need to do it here 'Note you don't need to subclass the listview just to set the item/subitem colors so its a quick addition SetWindowLongA(d1.hWnd,DWL_MSGRESULT,ColorListView(d1.hWnd,@LPARAM)) RETURN TRUE 'if used in a WINDOW use this LINE instead 'RETURN ColorListView(win.hWnd,@LPARAM) ENDSELECT CASE 300 'toggle the resize locking IF @NOTIFYCODE = 0 THEN bLocked = GETSTATE(d1,300) ENDSELECT CASE @IDDESTROY 'remove the subclasses if hHC AND origfpHC SetWindowLongA(hHC,GWL_WNDPROC,origfpHC) endif SetWindowLongA(hLV,GWL_WNDPROC,origfp) endselect return 0 ENDSUB SUB ColorListView(hWnd as UINT,lParam as UINT),UINT UINT rv SELECT *lParam.nmcd.dwDrawStage CASE CDDS_PREPAINT rv = CDRF_NOTIFYITEMDRAW CASE CDDS_ITEMPREPAINT '*lParam.nmcd.dwItemSpec is the zero based item the CONTROL is drawing 'but we want to COLOR each column item (subitem) individually rv =CDRF_NOTIFYSUBITEMDRAW CASE CDDS_SUBITEMPREPAINT '*lParam.iSubItem contains the zero based sub item number on system with IE 4.0 or greater installed SELECT *lParam.iSubItem CASE 0 'the ITEM COLOR *lParam.clrText = RGB(0,0,0) *lParam.clrTextBk = RGB(255,0,0) CASE 1 'the first sub item *lParam.clrText = RGB(0,0,0) *lParam.clrTextBk = RGB(0,255,0) DEFAULT 'the COLOR of the rest of the LINE *lParam.clrText = RGB(0,0,0) *lParam.clrTextBk = RGB(255,0,255) ENDSELECT rv = CDRF_NEWFONT DEFAULT rv = CDRF_DODEFAULT ENDSELECT RETURN rv ENDSUB 'message handler for the subclassed listview CONTROL SUB myLVHandler(hWnd:int,uMsg:int,wParam:int,lParam:pointer),int SELECT uMsg case WM_NOTIFY 'prevent columns from resizing. if (#lParam.code = HDN_BEGINTRACKA) OR (#lParam.code = HDN_BEGINTRACKW) if bLocked RETURN 1 endif ENDIF 'change the COLOR of the header CONTROL columns. IF (#lParam.code = NM_CUSTOMDRAW) SELECT(#lParam.dwDrawStage) CASE CDDS_PREPAINT RETURN CDRF_NOTIFYITEMDRAW CASE CDDS_ITEMPREPAINT 'COLOR the backound of the header columns first col blue, second yellow, third black, fourth green 'change the text COLOR of the first column while we are at it SELECT #lParam.dwItemSpec CASE 0 SetTextColor(#lParam.hDC,RGB(255,255,255)) SetBkColor(#lParam.hDC,RGB(50,50,255)) CASE 1 SetBkColor(#lParam.hDC,RGB(255,255,50)) CASE 2 SetTextColor(#lParam.hDC,RGB(255,255,255)) SetBkColor(#lParam.hDC,RGB(0,0,0)) CASE 3 SetTextColor(#lParam.hDC,RGB(0,0,0)) SetBkColor(#lParam.hDC,RGB(0,250,0)) ENDSELECT RETURN CDRF_NEWFONT ENDSELECT RETURN CDRF_DODEFAULT ENDIF ENDSELECT RETURN CallWindowProcA(origfp,hWnd,uMsg,wParam,lParam) ENDSUB 'message handler for the subclassed header CONTROL contained in the listview SUB myHCHandler(hWnd:int,uMsg:int,wParam:int,lParam:pointer),int 'return TRUE to prevent the header from changing the cursor SELECT uMsg CASE WM_SETCURSOR IF bLocked RETURN 1 ENDIF CASE @IDLBUTTONDBLCLK IF bLocked RETURN 1 ENDIF ENDSELECT RETURN CallWindowProcA(origfpHC,hWnd,uMsg,wParam,lParam) ENDSUB