如何在MFC中使用LISTBOX控件。实例如下: 一。首先新建一个Single或者对话框类型的MFC.EXE工种。 二。插入一个Dialog,然后新建一个类CDlgList,我们可以利用这个控件显示项目,也可实现多选,得到选项总数等.其源程序如下: // DlgList.cpp : implementation file// #include "stdafx.h"#include "testvc.h" #include "DlgList.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif /////////////////////////////////////////////////////////////////////////////// CDlgList dialog CDlgList::CDlgList(CWnd* pParent /*=NULL*/): CDialog(CDlgList::IDD, pParent){//{{AFX_DATA_INIT(CDlgList)//}}AFX_DATA_INIT} void CDlgList::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CDlgList)DDX_Control(pDX, IDC_BUTTON1, m_Del);DDX_Control(pDX, IDC_EDIT1, m_Edit);DDX_Control(pDX, IDC_LIST1, m_Lst);//}}AFX_DATA_MAP} BEGIN_MESSAGE_MAP(CDlgList, CDialog)//{{AFX_MSG_MAP(CDlgList)ON_BN_CLICKED(IDC_BUTTON1, OnAddString)ON_BN_CLICKED(IDC_BUTTON2, OnDel)ON_BN_CLICKED(IDC_BUTTON5, OnClear)ON_BN_CLICKED(IDC_BUTTON3, OnGetCount)ON_BN_CLICKED(IDC_BUTTON4, OnGetCurSel)//}}AFX_MSG_MAPEND_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////// CDlgList message handlers void CDlgList::OnAddString() {// TODO: Add your control notification handler code here CString str;m_Edit.GetWindowText(str);m_Lst.AddString(str); UpdateData(FALSE);} int CDlgList::DoModal() {// TODO: Add your specialized code here and/or call the base classreturn CDialog::DoModal();} void CDlgList::OnDel() {// TODO: Add your control notification handler code here m_Lst.DeleteString(m_Lst.GetCurSel()); UpdateData(FALSE);} void CDlgList::OnClear() {// TODO: Add your control notification handler code herefor(int i=0;i<m_Lst.GetCount();i++){m_Lst.DeleteString(i);i--;} UpdateData(FALSE);} void CDlgList::OnGetCount() {// TODO: Add your control notification handler code hereCString str; str.Format("List中共有%d项",m_Lst.GetCount());AfxMessageBox(str); } void CDlgList::OnGetCurSel() {// TODO: Add your control notification handler code here// AfxMessageBox(str); CString str,strMess; for (int i=0;i < m_Lst.GetCount();i++) { str.Format(_T("item %d: 您的选择状态是%s\r\n"),i,m_Lst.GetSel( i ) > 0 ? _T("true") : _T("false")); afxDump << str; strMess+=str; } AfxMessageBox(strMess);}

评论