热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UsingtheCCheckListBoxandgettingcheckstatenotification

转自:http:www.codeproject.comkbcomboboxCCheckListBoxUsage.aspxIntroductionIliketheCC

转自:http://www.codeproject.com/kb/combobox/CCheckListBoxUsage.aspx

 

Introduction

I like the CCheckListBox class provided with MFC, however, it�s use isn�t obvious and the wizard assistance stops at the CListBox class. I�ll describe how to insert it easily in your project. (There may be easier ways to do it, but this is how I do it and it works).

I�ll also show how you can add event notification so that you can find out when the check box state changes.

Creating the CCheckListBox member


  • Create a new MFC Application or Dialog Application.
  • In the resource editor, add a "regular" list box to the dialog.
    • Right click the listbox properties, then the Styles tab;
    • Ensure the Owner Draw = Fixed;
    • Ensure Has Strings = checked;
  • Holding the CTRL key, double click on the listbox in the resource dialog.

The wizard will only give you the option to create it as a CListBox , choose that, we will change it in code.

In the header code, change the wizard generated code from:

Collapse

//
Dialog Data




//
{{AFX_DATA(CCheckListBoxCBNDlg)




enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };

CListBox m_ctlCheckList;

//
}}AFX_DATA

to:

Collapse

//
Dialog Data




//
{{AFX_DATA(CCheckListBoxCBNDlg)




enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };

//
}}AFX_DATA




CCheckListBox m_ctlCheckList;

In the body, change the following generated code from:

Collapse

void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//
{{AFX_DATA_MAP(CCheckListBoxCBNDlg)




DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);

//
}}AFX_DATA_MAP




}

to:

Collapse

void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//
{{AFX_DATA_MAP(CCheckListBoxCBNDlg)




//
}}AFX_DATA_MAP




DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);

}

Adding items to the CCheckListBox

Now, you can add stuff to the checklist in your OnInitDialog member, like:

Collapse

m_ctlCheckList.ResetContent();

//
m_ctlCheckList.SetCheckStyle( BS_AUTO3STATE );




m_ctlCheckList.SetCheckStyle( BS_3STATE );

m_ctlCheckList.AddString("
Fumble"
);

m_ctlCheckList.SetCheck( 0
, 0
);

m_ctlCheckList.AddString("
Bumble"
);

m_ctlCheckList.SetCheck( 1
, 1
);

m_ctlCheckList.AddString("
Gumble"
);

m_ctlCheckList.SetCheck( 2
, 2
);

Note that the MSDN documentation is a little flimsy when it comes to the description of BS_AUTO3STATE and BS_3STATE . If you use BS_3STATE , then you will not get check box notifications and the states are locked (changeable in code only). If you use BS_AUTO3STATE , then you will get notifications of state changes, and the check boxes will manage themselves. You will just have to experiment with them to see which one gives you the effect you want.

Determining check box state changes

You can still use the wizard for the check list control you�ve created, but you�ll see that the list is limited to CListBox specific items:

I wanted a handler to know when a check box state changed (not a selection change). To accomplish this, manually add an event handler in the header as shown below. Note that if the user clicks on a check box, you will get two events for the click, first, OnCheckchangeList1 , followed by OnSelchangeList1 .

Caution: This is important if you depend on the current selection to change the checkbox state in a structure. I.e., the call to GetCurSel will be the new selection in the OnCheck call, even though OnSelchange hasn�t been called.

Collapse

//
{{AFX_MSG(CCheckListBoxCBNDlg)




virtual
BOOL OnInitDialog();

afx_msg void
OnSysCommand(UINT nID, LPARAM lParam);

afx_msg void
OnPaint();

afx_msg HCURSOR OnQueryDragIcon();

afx_msg void
OnSelchangeList1();

afx_msg void
OnCheckchangeList1();

//
}}AFX_MSG

In the body, add the event handler to the message map:

Collapse

BEGIN_MESSAGE_MAP(CCheckListBoxCBNDlg, CDialog)

//
{{AFX_MSG_MAP(CCheckListBoxCBNDlg)




ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)

//
}}AFX_MSG_MAP




ON_CLBN_CHKCHANGE(IDC_LIST1, OnCheckchangeList1)

END_MESSAGE_MAP()

And add your implementation of the handler.

Collapse

void
CCheckListBoxCBNDlg::OnCheckchangeList1()

{

//
TODO: Add your control notification handler code here




TRACE( "
CCheckListBoxCBNDlg::OnCheckchangeList1/n"
);

}

Conclusion

At this point you have a check list box that you can easily extend. Several other CodeProject articles show multi check list box classes and list view report views with check boxes. This is the simplest implementation of the MFC CCheckListBox .

Some people do not like the CCheckListBox because it leads to some ambiguity, but it really depends on the context it�s used in. For example, does checking the item turn the thing on or does the thing get enabled. When does it get turned on, when I check it or when I press OK/Apply in the dialog. Use this control with caution.


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • java文本编辑器,java文本编辑器设计思路
    java文本编辑器,java文本编辑器设计思路 ... [详细]
  • Android Studio 中 Activity 组件详解
    本文介绍了 Android 开发中 Activity 的基本概念、生命周期、状态转换以及如何创建和管理 Activity。通过详细的解释和示例代码,帮助开发者更好地理解和使用 Activity。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细介绍了如何在MFC(Microsoft Foundation Classes)应用程序中使用树形控件(Tree Control)。通过创建基于对话框的应用程序,并逐步添加和配置树形控件,最终实现带有图标的树形结构。 ... [详细]
  • 请看|差别_Android 6.0 运行时权限处理解析
    请看|差别_Android 6.0 运行时权限处理解析 ... [详细]
  • 利用HTML5 Canvas高效构建电信网络拓扑图
    电信网络拓扑图在实际应用中具有很高的实用价值。本文介绍了一个基于HTML5 Canvas的电信网络拓扑图项目,不仅实现了基本的图形展示功能,还加入了自动布局和属性栏功能,使项目更加完善。此Demo经过细微调整即可直接应用于实际项目中。 ... [详细]
  • 本文介绍了基于Java的汽车租赁系统开发,涵盖了从车辆采购预算到车辆维护的全过程管理。该系统利用现代互联网技术和数据库技术,实现了汽车租赁行业的全面信息化。 ... [详细]
author-avatar
kaxiaoliog_334
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有