(VC/MFC) CListBox类 和 Listbox控件

CListBox

如下继承关系

COBject
  : CCmdTarget
       : CWnd
           : CListBox
          
*说明:下面英文部分摘自MSDN MFC Reference.

In a single-selection list box, the user can select only one item.
In a multiple-selection list box, a range of itmes can be selected.
When the user selects an item, it is highliaged and the list box sends a notification message to the parent window.

We can create a list box dirrectly in code:
 (1)Construct the CListBox object.
 (2)Then call the Create member function to create the Windows list-box control.
 (3)Attach it into the CListBox object.
 
Or create a list box from a template:  ( 法2 )
 (1)Declare a list-box variable in dialog box class.
 (2)Then use DDX_Control in dialog box class's DoDataExchange function to connect the member variable to the control.
     (ClassWizard does this automatically when adding a control variable to dialog box class)

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.
If you create a CListBox object within a dialog box (through a dialog resource), the CListBox object is automatically destroyed when the user closes the dialog box.
If you create a CListBox object within a window, you may need to destroy the CListBox object. If you create the CListBox object on the stack, it is destroyed automatically. If

you create the CListBox object on the heap by using the new function, you must call delete on the object to destroy it when the user closes the parent window.
If you allocate any memory in the CListBox object, override the CListBox destructor to dispose of the allocation.

#include <afxwin.h>

*说明:下面部分摘自互谅网.并稍做修改.

* VC++2005 MFC CListBox 下操作. (采用上述法2)

1. 声明CListBox变量和连接控件(Listbox Control).
   (1) 在Dialog类中,声明CListBox类型的成员变量.
    例如在listDlg.h 文件中:

class ClistDlg : public CDialog{

	 ...

public:

	CListBox m_list1;

};


 (2) DoDataExchang函数中,把m_list1成员变量连接到控件.

    在listDlg.cpp文件中

void Clist1Dlg::DoDataExchange(CDataExchange* pDX){

	CDialog::DoDataExchange(pDX);

 	DDX_Control(pDX, IDC_LIST1, m_list1);

}


2.向Listbox中添加数据
   在 listDlg.cpp 文件中: 

BOOL Clist1Dlg::OnInitDialog(){

    ....

m_list1.AddString(_T("Item A"));

m_list1.AddString(_T("Item B"));

}

//备注: 也可以定义个控件对象的指针,对对象进行操作: 如下: (这样就不需要上述的第一步了)

CListBox *pCtrl = (CListBox *)GetDlgItem(IDC_LIST1);

pCtrl->AddString(_T("A"));

pCtrl->AddString(_T("B"));

 

3.从Listbox 中获取数据

CString s;

m_list1.GetText(1,s);

MessageBox(s,_T("取得第2行数据"),MB_OK);

s.ReleaseBuffer();


4. 获取选择的数据
首先要将Listbox的Selection属性设置为Multiple;

int nSel;

nSel=m_ListBox_Content.GetCurSel();

CString s;

m_ListBox_Content.GetText(nSel,s);

MessageBox(s,_T("您选择的是"),MB_OK);

s.ReleaseBuffer();



5.获取选择ListBox项的多个数据

首先要将ListBox的Selection的属性设置为Multiple

int nSel = m_ListBox_Content.GetSelCount();

CArray< int,int& > arrayListSel;

arrayListSel.SetSize(nSel);    

m_ListBox_Content.GetSelItems(nSel,arrayListSel.GetData());    

CString s = _T("");

for( int i=0; i< nSel; i++ )

{

m_ListBox_Content.GetText( arrayListSel[i], s);

MessageBox(s,_T("您选择的是"),MB_OK);

}

 

6.双击删除所选项
添加一个Listbox的双击事件

m_ListBox_Content.DeleteString(m_ListBox_Content.GetCurSel());


7.通过对象指针来操作Listbox Control.

OnInitDialog();函数里初始化

// TODO: 在此添加额外的初始化代码

CListBox *pCtrl = (CListBox *)GetDlgItem(IDC_LIST1);

pCtrl->AddString(_T("A"));

pCtrl->AddString(_T("B"));


对CListBox的操作

void CUseControlDlg::OnBnClickedButtonOk()

{

    // TODO: 在此添加控件通知处理程序代码

    CListBox *m_lstInfo = (CListBox *)GetDlgItem(IDC_LIST1);

    

    //那么你可以用一个循环取出里面的值:

    /*

    CString str; //临时变量用来接收项的字符串

    CString strAll=_T(""); //所有项

    int nCount = m_lstInfo->GetCount();//得到项目总数

    for(int i = 0; i< nCount; ++i)

    {

        m_lstInfo->GetText(i,str);

        strAll = strAll + str + _T("\r\n");

    }

    AfxMessageBox(strAll);

    */



    //取出单选选中的值

    /*

    int index;

    CString selectStr;

    index = m_lstInfo->GetCurSel();

    m_lstInfo->GetText(index,selectStr);

    AfxMessageBox(selectStr);

    */



    //多选,设置selection为Multiple

    int nCount = m_lstInfo->GetSelCount();

    CString cCount;

    CArray<int,int> aryListBoxSel;



    aryListBoxSel.SetSize(nCount);

    m_lstInfo->GetSelItems(nCount, aryListBoxSel.GetData()); 

    //得到总数

    cCount.Format(_T("%d"),nCount);

    AfxMessageBox(cCount);

    //得到选中的多项

    for (int i=0;i<nCount;i++)

    {

        CString selStr;

        m_lstInfo->GetText(aryListBoxSel[i],selStr);

        AfxMessageBox(selStr);

    }


 

 

你可能感兴趣的:(listbox)