没有合适的资源?快使用搜索试试~ 我知道了~
使VC中CListBox带有复选框
4星 · 超过85%的资源 需积分: 24 30 下载量 63 浏览量
2011-01-14
10:21:17
上传
评论
收藏 3KB TXT 举报
温馨提示
试读
2页
使VC中CListBox带有复选框使VC中CListBox带有复选框
资源推荐
资源详情
资源评论
使VC中CListBox带有复选框
由于实际需要在项目中需要使用一个带有复选框的列表控件,没错,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);
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对象,在窗口类的头文件里;
由于实际需要在项目中需要使用一个带有复选框的列表控件,没错,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);
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对象,在窗口类的头文件里;
winicehui
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页