如何禁用CListCtrl选择选项 [英] How to disable the CListCtrl select option

查看:604
本文介绍了如何禁用CListCtrl选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何禁用CListCtrl选择选项。我想重写CListCtrl类方法或处理任何窗口命令?谢谢。

解决方案

如果您要停止用户在 CListCtrl ,你需要从 CListCtrl 派生自己的类,并为 LVN_ITEMCHANGING 通知添加一个消息处理程序。 / p>

因此,示例类 CMyListCtrl 会有一个头文件:



MyListCtrl.h

  #pragma once 

class CMyListCtrl:public CListCtrl
{
DECLARE_DYNAMIC(CMyListCtrl)

protected:
DECLARE_MESSAGE_MAP()

public:
// LVN_ITEMCHANGING通知处理程序
afx_msg void OnLvnItemchanging(NMHDR * pNMHDR,LRESULT * pResult);
};

然后MyListCtrl.cpp:

  #includeMyListCtrl.h

IMPLEMENT_DYNAMIC(CMyListCtrl,CListCtrl)

BEGIN_MESSAGE_MAP(CMyListCtrl,CListCtrl)
ON_NOTIFY_REFLECT LVN_ITEMCHANGING,& CMyListCtrl :: OnLvnItemchanging)
END_MESSAGE_MAP()

void CMyListCtrl :: OnLvnItemchanging(NMHDR * pNMHDR,LRESULT * pResult)
{
// LVN_ITEMCHANGING通知处理程序
LPNMLISTVIEW pNMLV = reinterpret_cast< LPNMLISTVIEW>(pNMHDR);

//是用户选择项目吗?
if((pNMLV-> uChanged& LVIF_STATE)&&(pNMLV-> uNewState& LVNI_SELECTED))
{
// yes - b $ b * pResult = 1;
}
else
{
//否 - 允许任何其他更改
* pResult = 0;
}
}

因此,您可以添加一个普通 CListCtrl 到一个对话框,然后为它创建一个成员变量(默认为 CListCtrl ),然后编辑对话框的头文件到 #includeMyListCtrl.h 并将列表控制成员变量从 CListCtrl 更改为 CMyListCtrl


I don't know how to disable the CListCtrl select option. I want to override the CListCtrl class method or handle any window command ? Thanks.

解决方案

If you want to stop the user selecting an item in a CListCtrl, you need to derive your own class from CListCtrl and add a message handler for the LVN_ITEMCHANGING notification.

So, an example class CMyListCtrl would have a header file:

MyListCtrl.h

#pragma once

class CMyListCtrl : public CListCtrl
{
    DECLARE_DYNAMIC(CMyListCtrl)

protected:
    DECLARE_MESSAGE_MAP()

public:
    // LVN_ITEMCHANGING notification handler
    afx_msg void OnLvnItemchanging(NMHDR *pNMHDR, LRESULT *pResult);
};

And then MyListCtrl.cpp:

#include "MyListCtrl.h"

IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
    ON_NOTIFY_REFLECT(LVN_ITEMCHANGING, &CMyListCtrl::OnLvnItemchanging)
END_MESSAGE_MAP()

void CMyListCtrl::OnLvnItemchanging(NMHDR *pNMHDR, LRESULT *pResult)
{
    // LVN_ITEMCHANGING notification handler
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

    // is the user selecting an item?
    if ((pNMLV->uChanged & LVIF_STATE) && (pNMLV->uNewState & LVNI_SELECTED))
    {
        // yes - never allow a selected item
        *pResult = 1;
    }
    else
    {
        // no - allow any other change
        *pResult = 0;
    }
}

So you can, for example, add a normal CListCtrl to a dialog, then create a member variable for it (by default it will be CListCtrl) then edit your dialog's header file to #include "MyListCtrl.h and change the list control member variable from CListCtrl to CMyListCtrl.

这篇关于如何禁用CListCtrl选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆