Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC
Article

Editable Multi-line ListBox

Rate me:
Please Sign up or sign in to vote.
4.25/5 (19 votes)
25 Mar 2003 131.2K   2.5K   41   14
A ListBox control providing multi-line support.

Screenshot of ListBoxXP

Introduction

If you take a look at the snapshot and run the .exe file you`ll realise that there`s nothing I need to say here. I needed this control with item editing, insertion, deletion, and moving capabilities and I asked, "why not share it with people?"

Using the Code

It`s very easy to use just add a variable based on the class and that`s all. You work with it like a normal listbox.

//
// powered by NetControl.ro
//
// .............
void CListBoxXP::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    CString szBuf="";
    COLORREF col;
    if(((int)lpDrawItemStruct->itemID==edited)&&(IsWindow(hHelper)))
        return;
    {
        GetText(lpDrawItemStruct->itemID,szBuf);
         SetTextColor(lpDrawItemStruct->hDC,RGB(0,0,0));
         RECT r;
         memcpy((void*)&r,&lpDrawItemStruct->rcItem,sizeof(RECT));
         if(itm==(int)lpDrawItemStruct->itemID)
         {
            HBRUSH br;
            br=::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.9));
            ::FillRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct-&gt;rcItem,br);
            ::DeleteObject(br);
         }
         else
         {
             FillRect(lpDrawItemStruct-&gt;hDC,<BR>                &lpDrawItemStruct->rcItem,<BR>                (HBRUSH)GetStockObject(WHITE_BRUSH));
         }<BR>
         r.left+=5;
         r.top+=2;
         col=RGB(255,255,255);
         if(lpDrawItemStruct->itemState & ODS_SELECTED)
         {
            col=RGB(220,220,220);
            HBRUSH br;
            br=::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.75));
            ::FillRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct->rcItem,br);
            ::DeleteObject(br);
         }
         if(lpDrawItemStruct-&gt;itemState & ODS_FOCUS)
         {
            HBRUSH br;
            br = ::CreateSolidBrush(LightenColor(<BR>                GetSysColor(COLOR_HIGHLIGHT),0.5));
            ::FrameRect(lpDrawItemStruct->hDC,<BR>                &lpDrawItemStruct->rcItem,br);
            ::DeleteObject(br);
         }
         SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);<BR>
         r.left-=3;
         r.top-=2;
         if(CountLines(szBuf) > 1)
             DrawText(lpDrawItemStruct->hDC,szBuf,<BR>                strlen(szBuf),&r,<BR>                DT_LEFT|DT_TOP|DT_WORDBREAK);
         else
             DrawText(lpDrawItemStruct->hDC,szBuf,<BR>                strlen(szBuf),&r,<BR>                DT_LEFT|DT_VCENTER|DT_SINGLELINE);
    }
}

void CListBoxXP::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
    HDC hDC;
    TEXTMETRIC tm;
    char szBuf[512]="";
    ::SendMessage(this->m_hWnd, LB_GETTEXT,<BR>        (WPARAM)lpMeasureItemStruct->itemID,(LPARAM)szBuf);<BR>
    hDC = ::GetDC(this->m_hWnd);
    CRect r;
    GetClientRect(r);
    ::GetTextMetrics(hDC, &tm);
    lpMeasureItemStruct->itemHeight=(tm.tmHeight)*CountLines(szBuf);
    ::ReleaseDC(this->m_hWnd, hDC);
}
// ...<BR>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Romania Romania
I have been programming for the past 6 years in VBasic, Delphi, C, C++ .
I also have extended knowledge of webdesign (HTML, CSS, JavaScript, VBScript), webprogramming (PHP, ASP, ASP.NET (C#)) and database integration (mySql, MSSQL Server).
And when I`m not programming I`m working out or working on some personal projects .
Last but not least I`m looking for a project-based job in programming or webdesign .

Comments and Discussions

 
QuestionSave and Load data? Pin
chen27-Mar-03 19:42
chen27-Mar-03 19:42 
AnswerRe: Save and Load data? Pin
TomKat28-Mar-03 10:17
TomKat28-Mar-03 10:17 
GeneralNice! Pin
Ernest Laurentin26-Mar-03 16:52
Ernest Laurentin26-Mar-03 16:52 
GeneralCool! Pin
Wayzer26-Mar-03 7:01
Wayzer26-Mar-03 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.