Multicolumn CListCtrl & custom DrawItem
-
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?? -
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??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 -
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??See XListCtrl - A custom-draw list control with subitem formatting[^]maybe it is some helpful to you
_**
**_
WhiteSky