Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multicolumn CListCtrl & custom DrawItem

Multicolumn CListCtrl & custom DrawItem

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martijn van Kleef
    wrote on last edited by
    #1

    Hi, Can anyone please help me implement a custom (override) DrawItem method for a multicolumn CListCtrl, in particular for the individual columns / subitems? Here's what I have so far: void CColoredListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CString csItemText = GetItemText(lpDrawItemStruct->itemID, 0); CDC dc; dc.Attach(lpDrawItemStruct->hDC); // Save these value to restore them when done drawing COLORREF crOldTextColor = dc.GetTextColor(); COLORREF crOldBkColor = dc.GetBkColor(); if (lpDrawItemStruct->itemData != NULL) dc.SetTextColor((COLORREF)lpDrawItemStruct->itemData); if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } else { if ((lpDrawItemStruct->itemID % 2) == 1) dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_BTNFACE)); else dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_WINDOW)); } // Draw the text dc.DrawText(csItemText.GetBuffer(), csItemText.GetLength(), &lpDrawItemStruct->rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER); // Reset the background color and the text color back to their original values dc.SetTextColor(crOldTextColor); dc.SetBkColor(crOldBkColor); dc.Detach(); } I'm having trouble with the very first line in this method. It always gets the itemtext of the first column, but I'm having a multicolumn listctrl so I also need to know which subitem to get. This method is called from calls to CListCtrl::InsertItem and CListCtrl::SetItemText which get called in a multithreaded context. I was hoping the LPDRAWITEMSTRUCT would have subitem info but it doesn't seem to. Also, does the lpDrawItemStruct->itemData member contain the info that was set by calls to CListCtrl::SetItemData??

    M H 2 Replies Last reply
    0
    • M Martijn van Kleef

      Hi, Can anyone please help me implement a custom (override) DrawItem method for a multicolumn CListCtrl, in particular for the individual columns / subitems? Here's what I have so far: void CColoredListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CString csItemText = GetItemText(lpDrawItemStruct->itemID, 0); CDC dc; dc.Attach(lpDrawItemStruct->hDC); // Save these value to restore them when done drawing COLORREF crOldTextColor = dc.GetTextColor(); COLORREF crOldBkColor = dc.GetBkColor(); if (lpDrawItemStruct->itemData != NULL) dc.SetTextColor((COLORREF)lpDrawItemStruct->itemData); if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } else { if ((lpDrawItemStruct->itemID % 2) == 1) dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_BTNFACE)); else dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_WINDOW)); } // Draw the text dc.DrawText(csItemText.GetBuffer(), csItemText.GetLength(), &lpDrawItemStruct->rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER); // Reset the background color and the text color back to their original values dc.SetTextColor(crOldTextColor); dc.SetBkColor(crOldBkColor); dc.Detach(); } I'm having trouble with the very first line in this method. It always gets the itemtext of the first column, but I'm having a multicolumn listctrl so I also need to know which subitem to get. This method is called from calls to CListCtrl::InsertItem and CListCtrl::SetItemText which get called in a multithreaded context. I was hoping the LPDRAWITEMSTRUCT would have subitem info but it doesn't seem to. Also, does the lpDrawItemStruct->itemData member contain the info that was set by calls to CListCtrl::SetItemData??

      M Offline
      M Offline
      Milton Karimbekallil
      wrote on last edited by
      #2

      Use the GetColumn(), GetSubItemRect() and GetItemRect() methods to navigate to the subitems. See this sample for more details http://www.codeproject.com/listctrl/SubItemSel.asp[^] cheers..Milton KB

      1 Reply Last reply
      0
      • M Martijn van Kleef

        Hi, Can anyone please help me implement a custom (override) DrawItem method for a multicolumn CListCtrl, in particular for the individual columns / subitems? Here's what I have so far: void CColoredListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CString csItemText = GetItemText(lpDrawItemStruct->itemID, 0); CDC dc; dc.Attach(lpDrawItemStruct->hDC); // Save these value to restore them when done drawing COLORREF crOldTextColor = dc.GetTextColor(); COLORREF crOldBkColor = dc.GetBkColor(); if (lpDrawItemStruct->itemData != NULL) dc.SetTextColor((COLORREF)lpDrawItemStruct->itemData); if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } else { if ((lpDrawItemStruct->itemID % 2) == 1) dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_BTNFACE)); else dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_WINDOW)); } // Draw the text dc.DrawText(csItemText.GetBuffer(), csItemText.GetLength(), &lpDrawItemStruct->rcItem, DT_LEFT | DT_SINGLELINE | DT_VCENTER); // Reset the background color and the text color back to their original values dc.SetTextColor(crOldTextColor); dc.SetBkColor(crOldBkColor); dc.Detach(); } I'm having trouble with the very first line in this method. It always gets the itemtext of the first column, but I'm having a multicolumn listctrl so I also need to know which subitem to get. This method is called from calls to CListCtrl::InsertItem and CListCtrl::SetItemText which get called in a multithreaded context. I was hoping the LPDRAWITEMSTRUCT would have subitem info but it doesn't seem to. Also, does the lpDrawItemStruct->itemData member contain the info that was set by calls to CListCtrl::SetItemData??

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        See XListCtrl - A custom-draw list control with subitem formatting[^]maybe it is some helpful to you

        _**


        **_

        WhiteSky


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups