(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);
    }


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM