如何让ListCtrl控件失去焦点后还高亮,据说需要重绘,但遇到问题

yeaiping 2009-08-24 09:29:31
我派生了CListCtrl类,ClistCtrlEx,然后在它里面使用如下:
afx_msg void OnCustomDraw(NMHDR*,LRESULT*);
....
//消息映射
ON_NOTIFY_REFLECT(NM_CUSTIOMDRAW,OnCUstomDraw)
....

//函数
void CListCtrlEx::OnCustonDraw(NMHDR* pNMHDR,LRESULT* pResult)
{
LPNMLVCUSTOMDRAW lpLvcd=(LPNMLVCUSTOMDRAW)pNMHDR;
switch(lpLvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult=CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
**pResult=CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
//在这里重绘
//如何判断是对选中的Item进行重绘,我不会
。。。。。。。。。。
lpLvcd->clrText=RGB(0,0,0);// 黑色字体
lpLvcd->clrTextBk=RGB(0,0,255);//蓝色背景
*pResult=CDRF_DODEFULT;
break;
default:
*pResult=CDRF_DODEFULT;
break;
}
}
然后在主函数中定义: CListCtrlEx m_ctrlList;

以上代码都是网上找的,确实可以重绘,但是没有写如何重绘,重绘失去焦点的项目。
我猜应该是在失去焦点的事件中重绘,然后再判断是哪个选中了。上面的是把所有所有项都重绘了。
...全文
给本帖投票
1153 29 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
milk2008 2012-10-15
  • 打赏
  • 举报
回复
谢谢楼主分享解决方案
fancycow 2012-09-06
  • 打赏
  • 举报
回复
终于找到原因了,谢谢楼主分享解决方案。同样的问题,折磨我大半天。原来是创建时候问题
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 fandh 的回复:]
已经发给你了!
[/Quote]
我明白了,原来我设置了show selection always这个项,把它
LONG lStyle;
lStyle= GetWindowLong(m_ctrlListName.m_hWnd, GWL_STYLE); // 获取当前窗口style
lStyle&=~LVS_SHOWSELALWAYS; // 清除显示方式位
SetWindowLong(m_ctrlListName.m_hWnd, GWL_STYLE, lStyle); // 设置style
去掉就OK了,
谢谢你了,散分了
fandh 2009-08-24
  • 打赏
  • 举报
回复
已经发给你了!
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 fandh 的回复:]
你要不,将我的代码发到我的MAIL里面:wulebao602@163.com
[/Quote]
我的程序有一大部分是操作硬件的,访问驱动程序的,呵呵,而这个部分只是里面的一个小功能。要不你把你调通的项目工程发给我,我看看,因为,我对照了你的代码,发现我的就是粘贴你的,但是就是出不来。我的邮箱:332390405@qq.com或者yeaiping@Hotmail.com。可以加我好友,我是学硬件,所以对编程不是很懂
fandh 2009-08-24
  • 打赏
  • 举报
回复
你要不,将我的代码发到我的MAIL里面:wulebao602@163.com
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
希望你能帮我调一下,真郁闷,其实就是想把界面做漂亮一点,其他功能已经实现
fandh 2009-08-24
  • 打赏
  • 举报
回复
你那边出什么错误?
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 fandh 的回复:]
void CHighlightListCtrlDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
//This code based on Michael Dunn's excellent article on
//list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast <NMLVCUSTOMDRAW*>( pNMHDR );

    // Take the default processing unless we set this to something else below.
    *pResult = CDRF_DODEFAULT;

    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
        *pResult = CDRF_NOTIFYITEMDRAW;
}
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
        // This is the notification message for an item.  We'll request
        // notifications before each subitem's prepaint stage.

        *pResult = CDRF_NOTIFYSUBITEMDRAW;
}
    else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{

COLORREF clrNewTextColor, clrNewBkColor;
       
int    nItem = static_cast <int>( pLVCD->nmcd.dwItemSpec );

CString strTemp = m_ctListCtrl.GetItemText(nItem,pLVCD->iSubItem);
CString str = m_ctListCtrl.GetItemText(nItem,3);

if(strTemp == m_strName)
/*BOOL bSelect = FALSE;
POSITION pos = m_ctListCtrl.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctListCtrl.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}

if(bSelect)*/
{
clrNewTextColor = RGB(255,0,0); //Set the text to red
clrNewBkColor = RGB(255,255,0); //Set the bkgrnd color to blue
}
else
{

clrNewTextColor = RGB(0,0,0); //Leave the text black
clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}

pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;

       
        // Tell Windows to paint the control itself.
        *pResult = CDRF_DODEFAULT;
       
       
}
}
[/Quote]
我对照了一下,和你代码一摸一样,怎么搞的啊
void CDm9000_AppDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
//This code based on Michael Dunn's excellent article on
//list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.

*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{

COLORREF clrNewTextColor, clrNewBkColor;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

BOOL bSelect = FALSE;
POSITION pos =m_ctrlListName.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctrlListName.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}
if(bSelect)
{
clrNewTextColor = RGB(255,0,0); //Set the text to red
clrNewBkColor = RGB(0,0,255); //Set the bkgrnd color to blue
}
else
{
clrNewTextColor = RGB(0,0,0); //Leave the text black
clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}

pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;


// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;

}
}
fandh 2009-08-24
  • 打赏
  • 举报
回复
void CHighlightListCtrlDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
//This code based on Michael Dunn's excellent article on
//list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );

// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;

// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.

*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{

COLORREF clrNewTextColor, clrNewBkColor;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );

CString strTemp = m_ctListCtrl.GetItemText(nItem,pLVCD->iSubItem);
CString str = m_ctListCtrl.GetItemText(nItem,3);

if(strTemp == m_strName)
/*BOOL bSelect = FALSE;
POSITION pos = m_ctListCtrl.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctListCtrl.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}

if(bSelect)*/
{
clrNewTextColor = RGB(255,0,0); //Set the text to red
clrNewBkColor = RGB(255,255,0); //Set the bkgrnd color to blue
}
else
{

clrNewTextColor = RGB(0,0,0); //Leave the text black
clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}

pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;


// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;


}
}
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 fandh 的回复:]
是的,然后,将下面的if语句改成:if(bSelect)
说明是选中的!以前的那个if语句不要了!
[/Quote]
是吗,你真的通过了,我这边怎么点和以前一样呢,我是想这么实现,先选中(选中项肯定高亮),然后鼠标点击Edit控件,这样Clistctrl就失去焦点了,然后还需要把刚开始选中项高亮,谢谢你,能否把你调试通过的代码贴下来,谢谢。分都给你了
fandh 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yeaiping 的回复:]
int    nItem = static_cast <int>( pLVCD->nmcd.dwItemSpec );
这条语句能得到什么,是选中项的号吗?
BOOL bSelect = FALSE;
POSITION pos =m_ctrlListName.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctrlListName.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}
这段代码是得到选中的Item号
[/Quote]
是判断当前是否选中的!
fandh 2009-08-24
  • 打赏
  • 举报
回复
是的,然后,将下面的if语句改成:if(bSelect)
说明是选中的!以前的那个if语句不要了!
fandh 2009-08-24
  • 打赏
  • 举报
回复
呵呵,我这边已经通过运行了!
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
这条语句能得到什么,是选中项的号吗?
BOOL bSelect = FALSE;
POSITION pos =m_ctrlListName.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctrlListName.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}
这段代码是得到选中的Item号
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 fandh 的回复:]
我上面已经有代码了!你按照那个改动一下,至于里面的颜色,你自己修改成你想要的颜色!
[/Quote]
很感谢你,不过你的代码好像是不对,没成功
fandh 2009-08-24
  • 打赏
  • 举报
回复
我上面已经有代码了!你按照那个改动一下,至于里面的颜色,你自己修改成你想要的颜色!
yeaiping 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fandh 的回复:]
这个是选中你要选的高亮,如果想改成你选中的高亮,要改一下判断项!
[/Quote]
你说的没错,我太笨了。不会举一反三,我不知道怎么样得到当选中ClistCtrl项后,我要把焦点移开,鼠标点别的地方去的时候,还能让之前选中项高亮,但是怎么样判断我不会
fandh 2009-08-24
  • 打赏
  • 举报
回复
CString strTemp = m_ctListCtrl.GetItemText(nItem,pLVCD->iSubItem);
CString str = m_ctListCtrl.GetItemText(nItem,3);

if(strTemp == m_strName)


改成:
BOOL bSelect = FALSE;
POSITION pos = m_ctListCtrl.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_ctListCtrl.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}

if(bSelect)
fandh 2009-08-24
  • 打赏
  • 举报
回复
这个是选中你要选的高亮,如果想改成你选中的高亮,要改一下判断项!
加载更多回复(9)

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部