Drawing in a derived CListCtrl - Programmer

This is a discussion on Drawing in a derived CListCtrl - Programmer ; Hi, This is the code I have for drawing in my derived CListctrl class.Everything works fine except that the background color does not fill the whole client area of the listctrl but just the rectangle where the columns and rows ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Drawing in a derived CListCtrl

  1. Drawing in a derived CListCtrl

    Hi,

    This is the code I have for drawing in my derived CListctrl
    class.Everything works fine except that the background color does not
    fill the whole client area of the listctrl but just the rectangle
    where the columns and rows are present.I tried GetClientRect() instead
    of getitemrect but then the rest of the drawing including highlighting
    etc goes for a toss.Can anyone help ??

    Thanks,
    Sekhar

    void CMyListCtrl:rawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rcItem(lpDrawItemStruct->rcItem);

    CString str;
    CRect rect;
    int nItem = lpDrawItemStruct->itemID;

    CRect rcAllLabels;

    GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);
    pDC->FillRect(rcAllLabels,&CBrush(iBkColor));
    pDC->SetTextColor(iTextColor);

    CHeaderCtrl * pHC;
    pHC = GetHeaderCtrl();
    int iColCnt;
    iColCnt = pHC->GetItemCount();

    for (int iC = 0; iC < iColCnt;iC++)
    {
    str = GetItemText ( nItem , iC );
    GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    }

    LV_ITEM lvi;
    lvi.mask = LVIF_STATE;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.stateMask = LVIS_SELECTED;
    GetItem(&lvi);

    if(lvi.state == LVIS_SELECTED)
    {
    pDC->FillRect(rcAllLabels, &CBrush(RGB(15,225,225)));
    pDC->SetTextColor(RGB(0,0,0));

    for(int iC = 0; iC < iColCnt; iC++)
    {
    str = GetItemText ( nItem , iC );
    GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    }
    }

    pDC->DeleteTempMap(); if(GetItemCount()<=0)
    return;
    }

  2. Re: Drawing in a derived CListCtrl

    Off the top of my head I would say you need to take over the WM_Erase
    background message and fill your window with the color you want.

    Ali R.

    "Sekhar" wrote in message
    news:ff4e3765.0310282358.13974895@posting.google.c om...
    > Hi,
    >
    > This is the code I have for drawing in my derived CListctrl
    > class.Everything works fine except that the background color does not
    > fill the whole client area of the listctrl but just the rectangle
    > where the columns and rows are present.I tried GetClientRect() instead
    > of getitemrect but then the rest of the drawing including highlighting
    > etc goes for a toss.Can anyone help ??
    >
    > Thanks,
    > Sekhar
    >
    > void CMyListCtrl:rawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    > {
    > CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    > CRect rcItem(lpDrawItemStruct->rcItem);
    >
    > CString str;
    > CRect rect;
    > int nItem = lpDrawItemStruct->itemID;
    >
    > CRect rcAllLabels;
    >
    > GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);
    > pDC->FillRect(rcAllLabels,&CBrush(iBkColor));
    > pDC->SetTextColor(iTextColor);
    >
    > CHeaderCtrl * pHC;
    > pHC = GetHeaderCtrl();
    > int iColCnt;
    > iColCnt = pHC->GetItemCount();
    >
    > for (int iC = 0; iC < iColCnt;iC++)
    > {
    > str = GetItemText ( nItem , iC );
    > GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    > pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    > }
    >
    > LV_ITEM lvi;
    > lvi.mask = LVIF_STATE;
    > lvi.iItem = nItem;
    > lvi.iSubItem = 0;
    > lvi.stateMask = LVIS_SELECTED;
    > GetItem(&lvi);
    >
    > if(lvi.state == LVIS_SELECTED)
    > {
    > pDC->FillRect(rcAllLabels, &CBrush(RGB(15,225,225)));
    > pDC->SetTextColor(RGB(0,0,0));
    >
    > for(int iC = 0; iC < iColCnt; iC++)
    > {
    > str = GetItemText ( nItem , iC );
    > GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    > pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    > }
    > }
    >
    > pDC->DeleteTempMap(); if(GetItemCount()<=0)
    > return;
    > }




  3. Re: Drawing in a derived CListCtrl

    Thanx Ali..guess I should have known that DrawItem obviously is meant
    just for the items and not the rest of the listctrl.
    A few more thing I wanted to know..This is what I do :
    switch(message)
    {
    case WM_ERASEBKGND:

    RECT crt;
    RECT hRect;
    HBRUSH Brush;
    HDC hdc;

    hdc=(HDC)wParam;
    Brush=CreateSolidBrush(iBkColor);
    GetClientRect(&crt);

    CHeaderCtrl * pH = GetHeaderCtrl();
    pH->GetClientRect(&hRect);

    crt.top += hRect.bottom;
    FillRect(hdc,&crt,Brush);
    DeleteObject(Brush);
    return TRUE;
    break;
    }

    1)Will the create solidbrush keep on adding memory or something
    like that ?I am assuming the deleteobject takes care of the freeing
    up.
    2)The drawing is still not perfect like the bground color spills
    over slightly by about one or two pixels into the dialogbox on the
    left until the next redraw etc.The crt.top = hRect.bottom above is one
    among the crude attempts to take care of the header part.Are there any
    elegant methods which draws to fit exactly ?

    Sekhar

    "Ali R." wrote in message news:...
    > Off the top of my head I would say you need to take over the WM_Erase
    > background message and fill your window with the color you want.
    >
    > Ali R.
    >
    > "Sekhar" wrote in message
    > news:ff4e3765.0310282358.13974895@posting.google.c om...
    > > Hi,
    > >
    > > This is the code I have for drawing in my derived CListctrl
    > > class.Everything works fine except that the background color does not
    > > fill the whole client area of the listctrl but just the rectangle
    > > where the columns and rows are present.I tried GetClientRect() instead
    > > of getitemrect but then the rest of the drawing including highlighting
    > > etc goes for a toss.Can anyone help ??
    > >
    > > Thanks,
    > > Sekhar
    > >
    > > void CMyListCtrl:rawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    > > {
    > > CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    > > CRect rcItem(lpDrawItemStruct->rcItem);
    > >
    > > CString str;
    > > CRect rect;
    > > int nItem = lpDrawItemStruct->itemID;
    > >
    > > CRect rcAllLabels;
    > >
    > > GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);
    > > pDC->FillRect(rcAllLabels,&CBrush(iBkColor));
    > > pDC->SetTextColor(iTextColor);
    > >
    > > CHeaderCtrl * pHC;
    > > pHC = GetHeaderCtrl();
    > > int iColCnt;
    > > iColCnt = pHC->GetItemCount();
    > >
    > > for (int iC = 0; iC < iColCnt;iC++)
    > > {
    > > str = GetItemText ( nItem , iC );
    > > GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    > > pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    > > }
    > >
    > > LV_ITEM lvi;
    > > lvi.mask = LVIF_STATE;
    > > lvi.iItem = nItem;
    > > lvi.iSubItem = 0;
    > > lvi.stateMask = LVIS_SELECTED;
    > > GetItem(&lvi);
    > >
    > > if(lvi.state == LVIS_SELECTED)
    > > {
    > > pDC->FillRect(rcAllLabels, &CBrush(RGB(15,225,225)));
    > > pDC->SetTextColor(RGB(0,0,0));
    > >
    > > for(int iC = 0; iC < iColCnt; iC++)
    > > {
    > > str = GetItemText ( nItem , iC );
    > > GetSubItemRect ( nItem , iC , LVIR_LABEL , rect );
    > > pDC->DrawText(str, -1, rect,DT_LEFT | DT_END_ELLIPSIS);
    > > }
    > > }
    > >
    > > pDC->DeleteTempMap(); if(GetItemCount()<=0)
    > > return;
    > > }


+ Reply to Thread