Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Currently I am facing problem while selecting a subitem in listcontrol. Initially I made a listcontrol with LVS_SINGLESEL style which is making selection for the first column only when I am selecting the other subitems I was unable to get my subitem highlighted .As per some articles in code project suggesting to go for CustomDraw or Owner Draw.I am very new to this CustomDraw and owner draw ,but i got to know about customdraw by reading at an article in codeproject and everything is crystal clear .But, in all the examples provided in that article is entirely around coloring the text and background colour of the text.But I didn't get anything out of it .If i wanna go for Owner Draw then should i have class which should be derived from CListCtrl and implementing "DrawItem" method if so,this is thing done by me,

void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
TCHAR lpBuffer[256];

LV_ITEM lvi;

lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = lpDrawItemStruct->itemID ;
lvi.iSubItem = 0;
lvi.pszText = lpBuffer ;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));

LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;


CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nCol;
if(!m_SelectionFlag)
{
for ( nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if(nCol>0)
{
GetSubItemRect(lpDrawItemStruct->itemID, nCol,LVIR_BOUNDS, m_SelectionRect);
}
else
{ GetItemRect(lpDrawItemStruct->itemID, m_SelectionRect,LVIR_BOUNDS);
m_SelectionRect.right = GetColumnWidth(0);
m_SelectionRect.left = 0;
}
if(m_SelectionRect.PtInRect(m_Point))
{
m_SelectionFlag = TRUE;
// TRACE("\n***%d,%d",lpDrawItemStruct->itemID,nCol);
break;
}
else
m_SelectionFlag = FALSE;

}



if ( (lpDrawItemStruct->itemState & ODS_SELECTED) && m_SelectionFlag )
{
CRect rc=lpDrawItemStruct->rcItem;

rc.left=m_SelectionRect.left;
rc.right = m_SelectionRect.right;

pDC->FillSolidRect(&rc, /*RGB(000,000,205)*/GetSysColor(COLOR_HIGHLIGHT)) ;
//pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;

}
else
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
}

}

for ( nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if ( nCol > 0 )
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev) ;
lpDrawItemStruct->rcItem.left += lvcprev.cx ;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ;
}

// Get the text
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));

pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));

UINT uFormat = DT_LEFT ;

::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer),
&lpDrawItemStruct->rcItem, uFormat) ;

pDC->SelectStockObject(SYSTEM_FONT) ;
}

}


I derived a class CXListCtrl from CListCtrl in which I implemented this "DrawItem" function.
When I am using this pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ; and running the exe and if I had a click on the listcontrol then it is hiding the row on which I clicked and if comment that line and run the exe then it is working fine.

Can anyone please let me know the exact way to implement the OwnerDraw and CustomDraw.


Thanks in advance.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900