I have a CListCtrl in a MFC dialog. What I have to do is disbling some the items based on a condition so that user can't select that.Till now I have changed the colour but thats not proper a solution. My code looks like this:
void CSomeDialog::OnCustomdrawElementList(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
*pResult = 0;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
COLORREF crText;
//Here I want to do disable based on some condition of the data related to the item//
if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 )
crText = RGB(255,0,0);
else if ( (pLVCD->nmcd.dwItemSpec % 2) == 1 )
crText = RGB(0,255,0);
// Storing the color back
pLVCD->clrText = crText;
*pResult = CDRF_DODEFAULT;
}
}
I am unable to find any reference on how to disable a row yet.
Another problem , even after managing the disbling part how to get the data related to the item here in this custom draw function? can pLVCD->nmcd.lItemlParam be used to call GetItemData(). Please help.
Drawing and disabling a row is something different.
To disable a row, so that it can not be selected, use LVN_ITEMCHANGING. Check ifthe status is changing from unselected to selected (compare uNewState and uOldState about a change to LVIS_SELECTED). If you don't want to allow a selection of this item, just return TRUE to the WM_NOTIFY message.
About to associate data to an item in a list view it is easy to use the free lParam field. Here you can store some user data, that may point to an internal structure that you have. You can also access this field in drawing routines.