CListBox:
当我第一次看MSDN里关于CListBox的介绍时,我想MFC真的很强大,因为封装了很多的方法实现CListBox的操作,操作起来如此简单,所以很有必要看看CListBox。
初识CListbox:
先来看看MSDN:
The CListBox class provides the functionality of a Windows list box. A list box displays a list of items, such as filenames, that the user can view and select.
In a single-selection list box, the user can select only one item. In a multiple-selection list box, a range of items can be selected. When the user selects an item, it is highlighted and the list box sends a notification message to the parent window.
You can create a list box either from a dialog template or directly in your code. To create it directly, construct the CListBox object, then call the Create member function to create the Windows list-box control and attach it to the CListBox object. To use a list box in a dialog template, declare a list-box variable in your dialog box class, then use DDX_Control in your dialog box class's DoDataExchange function to connect the member variable to the control. (ClassWizard does this for you automatically when you add a control variable to your dialog box class.)
MSDN清晰的介绍了listbox的功用和MFC中创建listbox的方法,
Listbox里可以存放列表项,例如文件名,用户可以用来查看和选择。
在一个单选的列表框中,用户只能选择其中一个项,在多选的列表框中,用户可以选择一个范围内的所有项,当用户选择一个项时,这一项会高亮显示并且会发送一个通知消息给它的父窗口,当然这要求我们自己进行处理。
当我们利用代码创建listbox时,首先应该生成一个CListbox对象,然后调用Create方法创建它。当我们在对话框模板上生成listbox时,先要在对话框类中声明一个CListbox的变量,然后点击鼠标拖放控件即可,此时MFC向导会做如下工作:使用DDX_Control在DoDataExChnage函数里将刚才我们声明的CListbox变量与拖放的listbox控件进行绑定。
现在我们分别以两种方案实现CListbox简单应用(包括创建和单击事件)
方案一:代码实现
在view类里声明一个CListbox对象:
private:
CListBox m_listbox;
然后再view类的OnCreate消息里处理创建,当然这个消息我们要自己添加(通过MFC向导添加WM_CREATE消息)
int CListBoxView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
m_listbox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|WS_HSCROLL|WS_BORDER ,
CRect(10,10,200,200), this, 1);
// Add 256 items to the list box.
CString str;
for (int i=0;i < 256;i++)
{
str.Format(_T("item string %d"), i);
m_listbox.AddString( str );
}
m_listbox.SetTopIndex(m_listbox.GetCount()-1);//初始化首项
return 0;
}
一切搞定,现在我们可以处理事件了:
先来看看MSDN:
If you want to handle Windows notification messages sent by a list box to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message.
Each message-map entry takes the following form:
ON_Notification( id, memberFxn )
where id specifies the child window ID of the list-box control sending the notification and memberFxn is the name of the parent member function you have written to handle the notification.
The parent’s function prototype is as follows:
afx_msg void memberFxn( );
Following is a list of potential message-map entries and a description of the cases in which they would be sent to the parent:
- ON_LBN_DBLCLK The user double-clicks a string in a list box. Only a list box that has the LBS_NOTIFY style will send this notification message.
- ON_LBN_ERRSPACE The list box cannot allocate enough memory to meet the request.
- ON_LBN_KILLFOCUS The list box is losing the input focus.
- ON_LBN_SELCANCEL The current list-box selection is canceled. This message is only sent when a list box has the LBS_NOTIFY style.
- ON_LBN_SELCHANGE The selection in the list box is about to change. This notification is not sent if the selection is changed by the CListBox::SetCurSel member function. This notification applies only to a list box that has the LBS_NOTIFY style. The LBN_SELCHANGE notification message is sent for a multiple-selection list box whenever the user presses an arrow key, even if the selection does not change.
- ON_LBN_SETFOCUS The list box is receiving the input focus.
- ON_WM_CHARTOITEM An owner-draw list box that has no strings receives a WM_CHAR message.
- ON_WM_VKEYTOITEM A list box with the LBS_WANTKEYBOARDINPUT style receives a WM_KEYDOWN message.
很多是不是,不过这里我们只会用到ON_LBN_DBLCLK消息,
显然,控件的事件响应都同出一辙,有没要看到上面说的按钮也和这一样
响应事件:
1. 添加函数
Public:
`afx_msg void OnListBoxDbClick();
2. 添加消息映射
BEGIN_MESSAGE_MAP(CListBoxView, CView)
ON_LBN_DBLCLK(1,&CListBoxView::OnListBoxDbClick)//这里的1是控件ID
END_MESSAGE_MAP()
3. 处理消息
afx_msg void CListBoxView::OnListBoxDbClick()
{
int nIndex = m_listbox.GetCurSel();
int nCount = m_listbox.GetCount();
CString str;
m_listbox.GetText(nIndex,str);
if ((nIndex != LB_ERR) && (nCount > 1))
{
MessageBox(str);
}
}
关于方案二,这里就不介绍了,因为就是简单的拖拉控件即可,消息响应和上面是一样