请问clistctrl怎么让某一个item不可选

请教clistctrl如何让某一个item不可选?
本帖最后由 shijiaoan1985 于 2014-09-07 19:05:39 编辑
listctrl是动态创建的 如下
m_list.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|WS_CLIPSIBLINGS|LVS_EDITLABELS|LVS_SHOWSELALWAYS, CRect(0,0,500,270), this, 10234);
m_list.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_ONECLICKACTIVATE|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_GRIDLINES);//LVS_EX_FLATSB);//|LVS_EX_FULLROWSELECT

使用
int index = 0;
m_list.SetItemState(index, CDIS_GRAYED|CDIS_DISABLED,CDIS_GRAYED|CDIS_DISABLED);
好像没有效果

该如何做?

listctrl创建出来的样子 请问clistctrl怎么让某一个item不可选

------解决思路----------------------
msdn:

Indeed there is no way to disable a row in a report mode list control except to use custom draw and change the color of the line, but that doesn't really disable it.

You can, however, disable a row in a listbox so you might be able to use a CCheckListBox instead.
------解决思路----------------------
我的DrawItem代码

void CListCtrlCl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

// TODO:  添加您的代码以绘制指定项
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);
CRect rtClient;
GetClientRect(&rtClient);
for ( int 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; 
}

CRect rcItem;   
if (!GetSubItemRect(lpDrawItemStruct->itemID,nCol,LVIR_LABEL,rcItem))   
continue;   

::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));
CRect rcTemp;
rcTemp = rcItem;

if (nCol==0)
{
rcTemp.left -=2;
}

if ( (lpDrawItemStruct->itemState & ODS_SELECTED) && nCol != 4 )
{
pDC->FillSolidRect(&rcTemp, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
else
{
COLORREF color;
color = GetBkColor();
pDC->FillSolidRect(rcTemp,color);

if (FindColColor(nCol,color))
{
pDC->FillSolidRect(rcTemp,color);
}
if (FindItemColor(nCol,lpDrawItemStruct->itemID,color))
{
pDC->FillSolidRect(rcTemp,color);
}

//pDC->SetTextColor(m_color);
}

pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));

UINT   uFormat    = DT_CENTER ;
if (m_Header.m_Format[nCol]=='0')
{
uFormat = DT_LEFT;
}
else if (m_Header.m_Format[nCol]=='1')
{
uFormat = DT_CENTER;
}
else if (m_Header.m_Format[nCol]=='2')
{
uFormat = DT_RIGHT;
}
TEXTMETRIC metric;
pDC->GetTextMetrics(&metric);
int ofst;
ofst = rcItem.Height() - metric.tmHeight;
rcItem.OffsetRect(0,ofst/2);
pDC->SetTextColor(m_color);
COLORREF color;
if (FindColTextColor(nCol,color))
{
pDC->SetTextColor(color);
}
if (FindItemTextColor(nCol,lpDrawItemStruct->itemID,color))
{
pDC->SetTextColor(color);
}
CFont nFont ,* nOldFont; 
nFont.CreateFont(m_fontHeight,m_fontWith,0,0,0,FALSE,FALSE,0,0,0,0,0,0,_TEXT("宋体"));//创建字体 
nOldFont = pDC->SelectObject(&nFont);
DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), 
&rcItem, uFormat) ;

pDC->SelectStockObject(SYSTEM_FONT) ;