---
title: "Contribute some useful custom control: XListCtrl"
author: "eteran"
date: "2007-08-24T11:22:00Z"
tags: []
slug: "contribute-some-useful-custom-control-xlistctrl"
draft: false
lastmod: "2007-08-24T11:22:00Z"
id: 771917
url: "https://eteran.iteye.com/blog/771917"
---

In my VC MFC develop expericence, I always find that it is really poor of the Ctrls that microsoft provided, but we have no much project budget to bug a beautiful third-ctrl. So sometimes we need do some work to modify the standard Control. For the purpose of reuse, I have my library database, though lots of them come from internet, and I just did little work. but I still very pleasure to share with your guys. [Click here to download.](/Files/eteran/Tests.rar "Click here to download.")  
 

At first, I want introduce XListCtrl to you.  
This control was developed by [**Hans Dietrich**](http://www.codeproject.com/script/Articles/list_articles.asp?userid=15759) , you can [click here](http://www.codeproject.com/listctrl/xlistctrl.asp "click here") to view his web page. You can find this beautiful and powerful feature list, before that, let's have a look this beautiful picture:  
![](http://www.codeproject.com/listctrl/XListCtrl/XListCtrl4.png)

01. First column is specified with checkboxes
02. The second column shows subitem with bold text
03. The second column shows subitem with different background color
04. The third column contains progress bar in row 2
05. The fourth column also contains checkboxes
06. The fifth column shows subitem with different background color
07. The sixth column shows subitem with different text and background colors
08. The second column shows combobox
09. The sixth row is disabled
10. The fifth column shows edit control

What I do?  
At first, Let me show picture.  
![](http://images.cnblogs.com/cnblogs_com/eteran/images/cnblogs_com/eteran/XListCtrl1.JPG)

Keep all features from hans' xlistctrl.  
1, Modify the edit feature, add OK, Cancel button, It will be more friendly to user.  
2, Add change the font's style: italic

**How to Use:**

- Initial List:

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;CRect rect;  
    xlist.GetWindowRect(&rect);

    int w = rect.Width() - 2;  
    int colwidths\[6] = { 3, 26, 8, 19, 6, 15 };    // sixty-fourths  
    int cols = 0;  
    TCHAR *    lpszHeaders\[] = { \_T(""),  
                              \_T("File Name"),  
                              \_T("Speed(kb/s)"),  
                              \_T("Progress"),  
                              \_T("Time Left"),  
                              NULL };  
    int i;  
    int total\_cx = 0;  
    LV\_COLUMN lvcolumn;  
    memset(&lvcolumn, 0, sizeof(lvcolumn));

    // add columns  
    for (i = 0; ; i++)  
    {  
        if (lpszHeaders\[i] == NULL)  
            break;

        lvcolumn.mask = LVCF\_FMT | LVCF\_SUBITEM | LVCF\_TEXT | LVCF\_WIDTH;  
        lvcolumn.fmt = (i == 1 || i == 5) ? LVCFMT\_LEFT : LVCFMT\_CENTER;  
        lvcolumn.pszText = lpszHeaders\[i];  
        lvcolumn.iSubItem = i;  
        lvcolumn.cx = (lpszHeaders\[i+1] == NULL) ? w - total\_cx - 2 : (w * colwidths\[i]) / 64;  
        total\_cx += lvcolumn.cx;  
        xlist.InsertColumn(i, &lvcolumn);  
    }  
    cols = i; //total columns  
      
    // iterate through header items and attach the image list  
    HDITEM hditem;

    for (i = 0; i &lt; xlist.m\_HeaderCtrl.GetItemCount(); i++)  
    {  
        hditem.mask = HDI\_IMAGE | HDI\_FORMAT;  
        xlist.m\_HeaderCtrl.GetItem(i, &hditem);  
        hditem.fmt |=  HDF\_IMAGE;  
        if (i == 0)   
        {  
            hditem.iImage = XHEADERCTRL\_UNCHECKED\_IMAGE;  
            hditem.fmt = HDF\_CENTER;  
        }  
        else  
            hditem.iImage = XHEADERCTRL\_NO\_IMAGE;

        xlist.m\_HeaderCtrl.SetItem(i, &hditem);  
    }  
    // add edit button picture  
    HBITMAP hBitmapOK = LoadBitmap(AfxGetInstanceHandle(),  MAKEINTRESOURCE(IDB\_EDIT\_BTNOK));    
    HBITMAP hBitmapCancel = LoadBitmap(AfxGetInstanceHandle(),  MAKEINTRESOURCE(IDB\_EDIT\_BTNCANCEL));    
    m\_xlist.SetEditBtnIcon(hBitmapOK, hBitmapCancel);

- Add a new line:

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;            LVITEM itemData;  
            itemData.iSubItem = 0;  
            itemData.iItem = 0;  
            itemData.mask = LVIF\_TEXT;  
            itemData.pszText = \_T("");  
            itemData.lParam = (LPARAM)pJob;  
            CString szTmp;  
            nItem = m\_xlist.InsertItem(&itemData);  
            m\_xlist.SetCheckbox( nItem, 0, FALSE );  
            m\_xlist.SetItemText( nItem, 1, pJob-&gt;m\_szNewDocName.GetLength() == 0 ? pJob-&gt;m\_szFileName : pJob-&gt;m\_szNewDocName);        //doc name  
            m\_xlist.SetEditButton( nItem, 1);  
            m\_xlist.SetItemText( nItem, 3, \_T(""));                    //progress bar  
            pJob-&gt;GetLeftTimeString(szTmp);  
            m\_xlist.SetItemText( nItem, 4, szTmp );                    //time left estimate  
            szTmp.Format( "%.2lf", pJob-&gt;CountSpeed()/1024 );   
            m\_xlist.SetItemText( nItem, 2, szTmp );                    //transfer speed average

- Edit:

    ON\_REGISTERED\_MESSAGE(WM\_XLISTCTRL\_EDIT\_BTNOK\_CLICKED, OnRenameClicked)  
    ON\_REGISTERED\_MESSAGE(WM\_XLISTCTRL\_EDIT\_BTNEDIT\_CLICKED, OnRenameEditClicked)  
    ON\_REGISTERED\_MESSAGE(WM\_XLISTCTRL\_HEADER\_CHECKBOX\_CLICKED, OnHeaderChecked)

Implement these message function.

That' OK.