热门标签 | 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.


推荐阅读
  • 在 Android 开发中,通过 Intent 启动 Activity 或 Service 时,可以使用 putExtra 方法传递数据。接收方可以通过 getIntent().getExtras() 获取这些数据。本文将介绍如何使用 RoboGuice 框架简化这一过程,特别是 @InjectExtra 注解的使用。 ... [详细]
  • 深入解析SpringMVC核心组件:DispatcherServlet的工作原理
    本文详细探讨了SpringMVC的核心组件——DispatcherServlet的运作机制,旨在帮助有一定Java和Spring基础的开发人员理解HTTP请求是如何被映射到Controller并执行的。文章将解答以下问题:1. HTTP请求如何映射到Controller;2. Controller是如何被执行的。 ... [详细]
  • 深入解析Spring启动过程
    本文详细介绍了Spring框架的启动流程,帮助开发者理解其内部机制。通过具体示例和代码片段,解释了Bean定义、工厂类、读取器以及条件评估等关键概念,使读者能够更全面地掌握Spring的初始化过程。 ... [详细]
  • 并发编程 12—— 任务取消与关闭 之 shutdownNow 的局限性
    Java并发编程实践目录并发编程01——ThreadLocal并发编程02——ConcurrentHashMap并发编程03——阻塞队列和生产者-消费者模式并发编程04——闭锁Co ... [详细]
  • Redux入门指南
    本文介绍Redux的基本概念和工作原理,帮助初学者理解如何使用Redux管理应用程序的状态。Redux是一个用于JavaScript应用的状态管理库,特别适用于React项目。 ... [详细]
  • 本文介绍如何从字符串中移除大写、小写、特殊、数字和非数字字符,并提供了多种编程语言的实现示例。 ... [详细]
  • 黑马头条项目:Vue 文章详情模块与交互功能实现
    本文详细介绍了如何在黑马头条项目中配置文章详情模块的路由、获取和展示文章详情数据,以及实现关注、点赞、不喜欢和评论功能。通过这些步骤,您可以全面了解如何开发一个完整的前端文章详情页面。 ... [详细]
  • 本文详细介绍了 Java 中 org.geotools.data.shapefile.ShapefileDataStore 类的 getCurrentTypeName() 方法,并提供了多个代码示例,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 深入解析ESFramework中的AgileTcp组件
    本文详细介绍了ESFramework框架中AgileTcp组件的设计与实现。AgileTcp是ESFramework提供的ITcp接口的高效实现,旨在优化TCP通信的性能和结构清晰度。 ... [详细]
  • 为了解决不同服务器间共享图片的需求,我们最初考虑建立一个FTP图片服务器。然而,考虑到项目是一个简单的CMS系统,为了简化流程,团队决定探索七牛云存储的解决方案。本文将详细介绍使用七牛云存储的过程和心得。 ... [详细]
  • 由二叉树到贪心算法
    二叉树很重要树是数据结构中的重中之重,尤其以各类二叉树为学习的难点。单就面试而言,在 ... [详细]
  • 本文介绍了如何使用JavaScript的Fetch API与Express服务器进行交互,涵盖了GET、POST、PUT和DELETE请求的实现,并展示了如何处理JSON响应。 ... [详细]
  • NTP服务器配置详解:原理与工作模式
    本文深入探讨了网络时间协议(NTP)的工作原理及其多种工作模式,旨在帮助读者全面理解NTP的配置参数和应用场景。NTP是基于RFC 1305的时间同步标准,广泛应用于分布式系统中,确保设备间时钟的一致性。 ... [详细]
  • InmyapplicationIhaveQGraphicsScenewithpixmapaddedandallisviewedinQGraphicsViewwithsc ... [详细]
  • Python 工具推荐 | PyHubWeekly 第二十一期:提升命令行体验的五大工具
    本期 PyHubWeekly 为大家精选了 GitHub 上五个优秀的 Python 工具,涵盖金融数据可视化、终端美化、国际化支持、图像增强和远程 Shell 环境配置。欢迎关注并参与项目。 ... [详细]
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社区 版权所有