本文介绍了我们如何在listcontrol中选择子项(子项应该突出显示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





目前我在listcontrol中选择子项目时遇到问题。最初我用LVS_SINGLESEL样式进行了listcontrol,只有当我选择其他子项时我才能选择第一列。我无法突出显示我的子项。根据代码项目中的一些文章建议去CustomDraw或Owner Draw.I我是这个CustomDraw和所有者绘图的新手,但我通过阅读codeproject中的一篇文章来了解自定义,一切都很清楚。但是,在该文章中提供的所有示例完全围绕着色文本和背景颜色但是我没有得到任何东西。如果我想去所有者抽奖,那么我是否应该从CListCtrl派生类并实施DrawItem方法,如果是这样,这是我做的事情,



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);

休息;

}

其他

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)

{

//获取上一列宽度以移动下一个显示项目

GetColumn(nCol-1,&lvcprev);

lpDrawItemStruct-> rcItem.left + = lvcprev.cx;

lpDrawItemStruct-> rcItem.right + = lpDrawItemStruct-> rcItem.left;

}



//获取文本

:: 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);

}



}





我从CListCtrl派生了一个类CXListCtrl,我实现了这个 DrawItem函数。

当我使用这个pDC-> SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));并运行exe,如果我点击listcontrol然后它隐藏了我点击的行,如果注释该行并运行exe,那么它工作正常。



任何人都可以告诉我实现OwnerDraw和CustomDraw的确切方法。





提前致谢。

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));

UINTuFormat = 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.

推荐答案

这篇关于我们如何在listcontrol中选择子项(子项应该突出显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:11