CListBox 带有复选框

本文介绍如何在Visual C++中利用CCheckListBox类快速实现带复选框的列表控件。通过子类化普通列表控件并设置特定样式,即可轻松添加复选功能。
由于实际需要在项目中需要使用一个带有复选框的列表控件,没错,VB、Delphi……里现成就有,但由于项目是VC工程,VC里现成的CListBox或CListCtrl都没有复选框。说到这里,高手们可能会说,简单,自已重绘一个,或者偷懒一点的方法,也可以去网上找一个别人做好现成的类来用。 
其实还有一个更轻松的方法,网上找到这样一段话: 
How   to   use   the   CCheckListBox   class   in   a   dialog   box        
     
Create   in   your   resource   file   an   ordinary   list   box   in   a   dialog   box.   Whichever   other   attributes   that   you   choose,   the   list   box   must   be   ownerdrawn   and   include   the   hasstrings   attribute.   Assume   that   in   this   case,   you   have   assigned   an   ID   of   IDC_CHECKLISTBOX   to   the   listbox   .    
     
Create   an   instance   of   a   CCheckListBox   object   in   the   header   file   of   your   dialog   box.    
     
          CCheckListBox   m_CheckListBox;    
Over-ride   OnInitDialog()   in   your   dialog   box   and   subclass   the   list   box   that   you   created   in   your   resource   file.   Set   the   style   of   the   check   box   as   you   wish.   See   the   documentation   provided   with   your   compiler.    
     
          m_CheckListBox.SubclassDlgItem(IDC_CHECKLISTBOX,   this);    [Page]
          m_CheckListBox.SetCheckStyle(BS_AUTOCHECKBOX);    
Since   CCheckListBox   is   derived   from   CListBox,   you   have   access   to   all   the   class   members   of   CListBox.   Add   strings   to   the   list   box   using   AddString()   as   follows.    
     
          m_CheckListBox.AddString(\"Test   String\");    
CCheckListBox   provides   several   class   members   that   allow   you   to   access   or   set   the   check   state   of   the   items   in   the   list   box.   For   example,   to   see   whether   the   first   item   in   the   list   box   is   checked   (the   index   of   the   first   item   would   be   zero),   use   GetCheck().    
     
          int   iIndex   =   0;    
          int   iState;    
          iState   =   m_CheckListBox.GetCheck(iIndex);   
方法很巧,移花接木。 
MFC有一个CCheckListBox类支持复选框风格,所以我们可以直接使用ListBox控件,然后初始化时把它子类化成CCheckListBox,再设置一下风格参数就行。但要注意一下,成功的关键是要修改ListBox控件的两处属性,分别是Owner draw设置为Fixed(LBS_OWNERDRAWFIXED),Has strings设置为True(LBS_HASSTRINGS),否则不成功,运行时报错。 
具体实现步骤举例如下—— 
1、首先在窗口上拖放一个ListBox控件,假设其资源ID为IDC_LIST1;


2、如上所述修改该ListBox控件的属性(LBS_OWNERDRAWFIXED | LBS_HASSTRINGS); 
3、定义CCheckListBox对象,在窗口类的头文件里; [Page]
// XXXDlg.h




CCheckListBox m_CheckList; 
4、然后在CPP文件里,初始化的地方写下两行; 
// XXXDlg.cpp


BOOL CXXXDlg::OnInitDialog() 
...{ 
    // ...


    m_CheckList.SubclassDlgItem(IDC_LIST1, this); // IDC_LIST1是ListBox控件的资源ID 
    m_CheckList.SetCheckStyle(BS_AUTOCHECKBOX);


    // ... 

OK,就这么简单!m_CheckListBox.AddString(\"...\");加几条数据看看,是不是有复选框了! 
判断复选框是否被选中也很简单,m_CheckListBox.GetCheck(nIndex);就行!
内容概要:本文出自罗兰贝格关于工业4.0现状的报告,系统分析了制造业在数字化转型过程中的实际进展与挑战。报告指出,尽管“工业4.0”概念提出已逾十年,但多数企业仍未实现预期的智能化、自组织生产目标,主要受限于技术复杂性、组织孤岛、投资回报周期长及人才短缺等问题。通过对领先制造企业的研究,报告提炼出三大成功要素:一是制定基于现实的工业4.0愿景与全面战略,明确用例优先级;二是建立“中心辐射式”组织架构,设立专职数字化制造部门,推动跨职能协作与规模化落地;三是构建统一的IT/OT目标架构,强化数据生态与系统互操作性。报告特别强调,高价值用例如预测性维护、实时参数优化、视觉检测等已在汽车与半导体行业显现显著成效,企业应聚焦可量化回报的场景,结合资源现实,分阶段推进转型。; 适合人群:制造业企业管理者、数字化转型负责人、工业互联网从业者及政策制定者; 使用场景及目标:①帮助企业评估自身工业4.0成熟度并制定务实发展战略;②为制造企业设计组织架构与IT/OT技术路线图提供参考;③指导资源优先配置于高价值数字化用例,提升投资回报率; 阅读建议:建议结合企业实际生产场景阅读,重点关注“中心辐射式”运营模式与六大高价值用例的适用性分析,同时参考报告中的汽车行业案例,因地制宜地规划数字化路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值