CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Apr 2008
    Posts
    26

    Custom draw listview

    I'm using visual c++ 8. I am trying to implement custom drawn listview in pure win32(NO MFC) based on articles on the web, however it doesn't seem to work.What am i doing wrong? PLEASE help. (This code is based on an article i found on the web,which used the same code, to colour a lisview on a dialog.I am not using dialogs here) Here is the code ...

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include<shellapi.h>
    #pragma comment (lib,"comctl32.lib")
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    LRESULT ProcessCustomDraw (LPARAM lParam);
    
    
    HWND frmhwnd,lvhwnd;               
    HIMAGELIST hSmallImageList;
    
    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
    
    {
       
           /* Create form using CreateWindowEx function*/            
        
    	INITCOMMONCONTROLSEX iccx;
    	iccx.dwSize=sizeof(INITCOMMONCONTROLSEX);
    	iccx.dwICC=ICC_LISTVIEW_CLASSES;
    	InitCommonControlsEx(&iccx);     
    	lvhwnd=CreateWindowEx(0,                              
                          WC_LISTVIEW, 0, WS_CHILD | WS_VISIBLE | LVS_REPORT, 30,                                     
                          30,  700, 400,  frmhwnd,0, hThisInstance, 0);                                    
    
    	hSmallImageList=ImageList_Create(GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),ILC_COLOR32|ILC_MASK,1,1);
    	ListView_SetImageList(lvhwnd,hSmallImageList,LVSIL_SMALL);
    
    	ListView_SetImageList(lvhwnd,0,LVSIL_SMALL);
    	HICON hicon=ExtractIcon(0,L"C:\\icon1.ico",0);
    	ImageList_ReplaceIcon(hSmallImageList,-1,hicon);
    	hicon=ExtractIcon(0,L"C:\\icon2.ico",0);
    	ImageList_ReplaceIcon(hSmallImageList,-1,hicon);
    	hicon=ExtractIcon(0,L"C:\\icon3.ico",0);
    	ImageList_ReplaceIcon(hSmallImageList,-1,hicon);
    	ListView_SetImageList(lvhwnd,hSmallImageList,LVSIL_SMALL);
    		
            /*Just adding Random Data.You can avoid reading this part, if you find the code is too long*/
    
    	LV_COLUMN lv;
    	lv.fmt=0;
    	lv.cx = 250;
    	lv.mask=LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
    	lv.pszText=L"Column1";
    	lv.cchTextMax=wcslen(lv.pszText);
    	lv.iSubItem=0;
    	ListView_InsertColumn(lvhwnd,0,&lv);
    	lv.pszText=L"Column2";
    	lv.iSubItem=1;
    	ListView_InsertColumn(lvhwnd,1,&lv);
    	lv.pszText=L"Column3";
    	lv.iSubItem=2;
    	ListView_InsertColumn(lvhwnd,2,&lv);
    
    	LVITEM lvi={0};
    	lvi.mask=LVIF_TEXT | LVIF_IMAGE | LVIF_STATE ;  
    	lvi.state= 0;
    	lvi.stateMask= 0;
    	lvi.iSubItem=0;
    	lvi.iImage=0;                    //image list index
    	lvi.pszText=L"TEST";
    	//lvi.pszText=LPSTR_TEXTCALLBACK;
    	lvi.cchTextMax =wcslen(lvi.pszText);
    	lvi.iItem=0;
    	ListView_InsertItem(lvhwnd,&lvi);
    	lvi.mask=LVIF_TEXT | LVIF_IMAGE | LVIF_STATE ;  
    	lvi.state= 0;
    	lvi.stateMask= 0;
    	lvi.iImage=0;       
    	lvi.iSubItem=1;
    	lvi.pszText=L"TEST2";
    	lvi.cchTextMax =wcslen(lvi.pszText);
    	ListView_SetItem(lvhwnd,&lvi);
    
    	lvi.iSubItem=0;
    	lvi.iImage=1;                    //image list index
    	lvi.pszText=L"TE";
    	lvi.cchTextMax =wcslen(lvi.pszText);
    	lvi.iItem=1;
    	ListView_InsertItem(lvhwnd,&lvi);
    
    	lvi.iImage=2;                    //image list index
    	lvi.pszText=L"dfffff";
    	lvi.cchTextMax =wcslen(lvi.pszText);
    	lvi.iItem=2;
    	
    	ListView_InsertItem(lvhwnd,&lvi);
    
    
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
       
        return messages.wParam;
    }
    
    
    
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  
        {
            case WM_DESTROY:
                PostQuitMessage (0);       
                break;
    
    	case WM_NOTIFY:
    		LPNMLISTVIEW pnm;
    		pnm= (LPNMLISTVIEW)lParam;
    		if(pnm->hdr.hwndFrom == lvhwnd &&pnm->hdr.code == NM_CUSTOMDRAW )
                    {
                       SetWindowLong(hwnd, DWL_MSGRESULT, (LONG)ProcessCustomDraw(lParam));
                       return TRUE;
                    }
    
    		break;
            default:                      
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    LRESULT ProcessCustomDraw (LPARAM lParam)
    {
        LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
    
        switch(lplvcd->nmcd.dwDrawStage) 
        {
            case CDDS_PREPAINT : //Before the paint cycle begins
                //request notifications for individual listview items
                return CDRF_NOTIFYITEMDRAW;
                
            case CDDS_ITEMPREPAINT: //Before an item is drawn
                {
                    return CDRF_NOTIFYSUBITEMDRAW;
                }
                break;
        
            case CDDS_SUBITEM | CDDS_ITEMPREPAINT: //Before a subitem is drawn
                {
    				
                    switch(lplvcd->iSubItem)
                    {
                        case 0:
                        {
                          lplvcd->clrText   = RGB(255,255,255);
    					  if(lplvcd->nmcd.dwItemSpec==0)
    							lplvcd->clrText   = RGB(0,0,0);
    
    					  lplvcd->clrTextBk = RGB(240,55,23);
                          return CDRF_NEWFONT;
                        }
                        break;
                        
                        case 1:
                        {
                          lplvcd->clrText   = RGB(255,255,0);
                          lplvcd->clrTextBk = RGB(0,0,0);
                          return CDRF_NEWFONT;
                        }
                        break;  
    
                        case 2:
                        {
                          lplvcd->clrText   = RGB(20,26,158);
                          lplvcd->clrTextBk = RGB(200,200,10);
                          return CDRF_NEWFONT;
                        }
                        break;
    
                        case 3:
                        {
                          lplvcd->clrText   = RGB(12,15,46);
                          lplvcd->clrTextBk = RGB(200,200,200);
                          return CDRF_NEWFONT;
                        }
                        break;
    
                        case 4:
                        {
                          lplvcd->clrText   = RGB(120,0,128);
                          lplvcd->clrTextBk = RGB(20,200,200);
                          return CDRF_NEWFONT;
                        }
                        break;
    
                        case 5:
                        {
                          lplvcd->clrText   = RGB(255,255,255);
                          lplvcd->clrTextBk = RGB(0,0,150);
                          return CDRF_NEWFONT;
                        }
                        break;
    
                    }
     
                }
        }
        return CDRF_DODEFAULT;
    }
    Last edited by blastingblast; June 10th, 2008 at 11:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured