`
fdyang
  • 浏览: 79235 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

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


分享到:
评论

相关推荐

    VC Listbox自绘,实现不同高度以及自动换行

    VC Listbox自绘,实现不同高度以及自动换行,我是使用VC6.0写的

    CListBox中设置文本的颜色(字体)

    这个主要是用vc开发的一个文本颜色的CListBox控件, 添加一个颜色处理类,并继承CListBox,然后添加颜色控件的变量,在addString(文本,rgb),就完成了,就这么简单,代码运行可以看到结果!

    可设置文字颜色的listbox

    重写了CListBox类,从网上收集的,很好很强大 使用方法:(适用于vc++ MFC) 把包里的头文件和cpp文件包含进你的工程 1)添加一个listbox control 2)设置此控件属性:Has Strings : true owner draw : fix or ...

    VC之美化界面篇本文专题讨论VC中的界面美化,适用于具有中等VC水平的读者。读者最好具有以下VC基础:

    要了解Windows下的绘图操作,要实现Windows界面的美化,就必须了解MFC封装的设备环境类和图形对象类。 2.1.1 设备环境类 Windows下的绘图操作说到底就是DC操作。DC(Device Context设备环境)对象是一个抽象的作图...

    自定义条目背景及Text颜色的listbox下拉控件

    VC/C++源码,界面编程,ListBox Listbox在很多时候我们都要用到,本程序是在Listbox控件基础上进行了改进,把其条目的背景和文字的背景重新定义,你可以自由选择颜色,这样会使Listbox更加的醒目,而且使用方法也也挺...

    VC6风格界面

    ...................\XListBox-自画条目背景和文字颜色的listbox控件.doc ...................\XP风格控件界面库.doc ...................\一个功能强大的MFC界面处理扩展库:CJ60Lib.doc ...................\...

    vc++ 应用源码包_6

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    vc++ 应用源码包_1

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    vc++ 应用源码包_2

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    vc++ 开发实例源码包

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    vc++ 应用源码包_3

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    vc++ 应用源码包_5

    ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件管理和监测系统源码 它包括客户端和服务端,客户端软件主要作用是监测本主机的活动,并将监测到的信息定时发送给服务器。服务器可以...

    Visual C++ 编程资源大全(英文源码 控件)

    ComboBox_Tut_demo.zip An entry level tutorial on using the CComboBox control(65KB)&lt;END&gt;&lt;br&gt;62,ListBox_Tut_src.zip An entry level tutorial on using the CListBox control(62KB)&lt;END&gt;&lt;br&gt;63,...

    Visual C++ 编程资源大全(源码 窗体)

    trayicon1.zip 屏幕保护托盘图标(SDK版本)(27KB)&lt;END&gt;&lt;br&gt;15,trayicon2.zip 屏幕保护托盘图标(MFC版本)(30KB)&lt;END&gt;&lt;br&gt;16,cj60lib.zip MFC扩展界面库(VC6升级版)(936KB)&lt;END&gt;&lt;br&gt;17,devstudio.zip ...

Global site tag (gtag.js) - Google Analytics