list control的用法

来源:互联网 发布:淘宝联盟ipad下载 编辑:程序博客网 时间:2024/05/18 02:37

本文全面介绍了如何编辑List Control里面的任何子项

 

list control - w.z.y2006 - 时间老人的博客

 

介绍

内容有点多,译出来也没多大意思,大致就是说一个VC程序员会常常碰到List Control,List Control有很多方法可以显示数据,可List Control里默认的没有编辑功能,故在此智短文里,那个牛叉的人教你怎么实现这个功能。这篇文章是基于VC MFC滴,用别的功具的娃们当然也可以看看,呵呵,不多说,先实现图上ok exit两个按钮:

 

void CMultipleColumnsDlg::OK(){    CDialog::EndDialog (0); // Add this line}void CMultipleColumnsDlg::OnExit(){    CDialog::EndDialog (0); // Add this line}

接下来添加一个ListCtrl控件,记得把ListCtrl的style设置成Report,这个是为了实现我们要多行显示的功能

然后增加一个文本框Edit Box去掉它的Border style属性

 

 

增加一个InsertItems() 成员函数,用来写入ListControl的显示内容

 

void CMultipleColumnsDlg::InsertItems(){    HWND hWnd = ::GetDlgItem(m_hWnd, IDC_LIST1);    // Set the LVCOLUMN structure with the required    // column information    LVCOLUMN list;    list.mask = LVCF_TEXT |LVCF_WIDTH|        LVCF_FMT |LVCF_SUBITEM;    list.fmt = LVCFMT_LEFT;    list.cx = 50;    list.pszText   = "S.No";    list.iSubItem = 0;    //Inserts the column    ::SendMessage(hWnd,LVM_INSERTCOLUMN,         (WPARAM)0,(WPARAM)&list);    list.cx = 100;    list.pszText   = "Name";    list.iSubItem = 1;    ::SendMessage(hWnd ,LVM_INSERTCOLUMN,        (WPARAM)1,(WPARAM)&list);    list.cx = 100;    list.pszText   = "Address";    list.iSubItem = 2;    ::SendMessage(hWnd ,LVM_INSERTCOLUMN,        (WPARAM)1,(WPARAM)&list);    list.cx = 100;    list.pszText   = "Country";    list.iSubItem = 2;    ::SendMessage(hWnd ,LVM_INSERTCOLUMN,        (WPARAM)1,(WPARAM)&list);    // Inserts first Row with four columns 插入行     SetCell(hWnd,"1",0,0);    SetCell(hWnd,"Prabhakar",0,1);    SetCell(hWnd,"Hyderabad",0,2);    SetCell(hWnd,"India",0,3);    // Inserts second Row with four columns .    SetCell(hWnd,"2",1,0);    SetCell(hWnd,"Uday",1,1);    SetCell(hWnd,"Chennai",1,2);    SetCell(hWnd,"India",1,3);}

自定义的SetCell( ) 函数,用来实现插入数据用的

 

void CMultipleColumnsDlg::SetCell(HWND hWnd1,         CString value, int nRow, int nCol){    TCHAR     szString [256];    wsprintf(szString,value ,0);    //Fill the LVITEM structure with the    //values given as parameters.    LVITEM lvItem;    lvItem.mask = LVIF_TEXT;    lvItem.iItem = nRow;    lvItem.pszText = szString;    lvItem.iSubItem = nCol;    if(nCol >0)        //set the value of listItem        ::SendMessage(hWnd1,LVM_SETITEM,             (WPARAM)0,(WPARAM)&lvItem);    else        //Insert the value into List        ListView_InsertItem(hWnd1,&lvItem);}//通过行和列得到项目里面的数据CString CMultipleColumnsDlg::GetItemText(     HWND hWnd, int nItem, int nSubItem) const{    LVITEM lvi;    memset(&lvi, 0, sizeof(LVITEM));    lvi.iSubItem = nSubItem;    CString str;    int nLen = 128;    int nRes;    do    {        nLen *= 2;        lvi.cchTextMax = nLen;        lvi.pszText = str.GetBufferSetLength(nLen);        nRes = (int)::SendMessage(hWnd,            LVM_GETITEMTEXT, (WPARAM)nItem,            (LPARAM)&lvi);    str.ReleaseBuffer();    return str;}}

//为窗口类添加两个成员变量:

int nItem, nSubItem;

用Class wizard 添加 NM_CLICK 响应 ,当用户点击任何位置时,就会对应出现一个Edit Box,并可以修改数据

 

void CMultipleColumnsDlg::OnClickList(        NMHDR* pNMHDR, LRESULT* pResult){    Invalidate();    HWND hWnd1 = ::GetDlgItem (m_hWnd,IDC_LIST1);    LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;    RECT rect;    //get the row number    nItem = temp->iItem;    //get the column number    nSubItem = temp->iSubItem;    if(nSubItem == 0 || nSubItem == -1 || nItem == -1)        return ;    //Retrieve the text of the selected subItem    //from the list    CString str = GetItemText(hWnd1,nItem ,        nSubItem);    RECT rect1,rect2;    // this macro is used to retrieve the Rectanle    // of the selected SubItem    ListView_GetSubItemRect(hWnd1,temp->iItem,        temp->iSubItem,LVIR_BOUNDS,&rect);    //Get the Rectange of the listControl    ::GetWindowRect(temp->hdr.hwndFrom,&rect1);    //Get the Rectange of the Dialog    ::GetWindowRect(m_hWnd,&rect2);    int x=rect1.left-rect2.left;    int y=rect1.top-rect2.top;       if(nItem != -1)    ::SetWindowPos(::GetDlgItem(m_hWnd,IDC_EDIT1),        HWND_TOP,rect.left+x,rect.top+4,        rect.right-rect.left - 3,        rect.bottom-rect.top -1,NULL);    ::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_SHOW);    ::SetFocus(::GetDlgItem(m_hWnd,IDC_EDIT1));    //Draw a Rectangle around the SubItem    ::Rectangle(::GetDC(temp->hdr.hwndFrom),       rect.left,rect.top-1,rect.right,rect.bottom);    //Set the listItem text in the EditBox    ::SetWindowText(::GetDlgItem(m_hWnd,IDC_EDIT1),str);    *pResult = 0;}To handle the ENTER key we need to write the virtual function OnOk (响应ENTER键)afx_msg void OnOK();In MultipleColumnsDlg.cpp write the following code.// This function handles the ENTER keyvoid CMultipleColumnsDlg::OnOK() {      CWnd* pwndCtrl = GetFocus();    // get the control ID which is    // presently having the focus    int ctrl_ID = pwndCtrl->GetDlgCtrlID();    CString str;    switch (ctrl_ID)    {   //if the control is the EditBox        case IDC_EDIT1:        //get the text from the EditBox        GetDlgItemText(IDC_EDIT1,str);        //set the value in the listContorl with the        //specified Item & SubItem values        SetCell(::GetDlgItem (m_hWnd,IDC_LIST1),            str,nItem,nSubItem);        ::SendDlgItemMessage(m_hWnd,IDC_EDIT1,           WM_KILLFOCUS,0,0);        ::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),            SW_HIDE);            break;            default:            break;    }}最后一步在OnInitDialog 中设置List Control的样式       ListView_SetExtendedListViewStyle(::GetDlgItem        (m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT |        LVS_EX_GRIDLINES);InsertItems();::ShowWindow(::GetDlgItem(m_hWnd,IDC_EDIT1),SW_HIDE); 

ListControl列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。MFC中使用CListCtrl类来封装列表控件的各种操作。通过调用

BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格: 
LVS_ICON ,LVS_SMALLICON ,LVS_LIST, LVS_REPORT 这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示 ..。


LVS_EDITLABELS 结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。 
LVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点 
LVS_SINGLESEL 同时只能选中列表中一项


首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用: 
CImageList* SetImageList( CImageList* pImageList, LVSIL_NORMAL); 
如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用: 
CImageList* SetImageList( CImageList* pImageList, LVSIL_SMALL); 
int InsertItem( int nItem, LPCTSTR lpszItem ); 插入行
nItem:指明插入位置
lpszItem:为显示字符。
除LVS_REPORT风格外其他三种风格都只需要直接调用InsertItem就可以了,但如果使用报表风格就必须先设置列表控件中的列信息。
int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth, int nSubItem);插入列
iCol:为列的位置,从零开始
lpszColumnHeading:为显示的列名
nFormat:为显示对齐方式
nWidth:为显示宽度
nSubItem:为分配给该列的列索引。 

BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText );设置每列的显示字符
nItem:为行位置
nSubItem:为列位置
lpszText:为显示字符

下面的代码演示了如何设置多列并插入数据: 
m_list.SetImageList(&m_listSmall,LVSIL_SMALL);//设置ImageList
m_list.InsertColumn(0,"Col 1",LVCFMT_LEFT,300,0);//设置列
m_list.InsertColumn(1,"Col 2",LVCFMT_LEFT,300,1);
m_list.InsertColumn(2,"Col 3",LVCFMT_LEFT,300,2);
m_list.InsertItem(0,"Item 1_1");//插入行
m_list.SetItemText(0,1,"Item 1_2");//设置该行的不同列的显示字符
m_list.SetItemText(0,2,"Item 1_3")

COLORREF GetTextColor( )/BOOL SetTextColor( COLORREF cr ):用于得到/设置显示的字符颜色。 
COLORREF GetTextBkColor( )/BOOL SetTextBkColor( COLORREF cr ):用于得到/设置显示的背景颜色。 
void SetItemCount( int iCount ):用于得到添加进列表中项的数量。 
BOOL DeleteItem(int nItem):用于删除某一项
BOOL DeleteAllItems( ):将删除所有项。 
BOOL SetBkImage(HBITMAP hbm, BOOL fTile , int xOffsetPercent, int yOffsetPercent):用于设置背景位图。 
CString GetItemText( int nItem, int nSubItem ):用于得到某项的显示字符。 

列表控件的消息映射同样使用ON_NOTIFY宏,形式如同:ON_NOTIFY( wNotifyCode, id, memberFxn ),wNotifyCode为通知代码,id为产生该消息的窗口ID,memberFxn为处理函数,函数的原型如同voidOnXXXList(NMHDR* pNMHDR, LRESULT* pResult),其中pNMHDR为一数据结构,在具体使用时需要转换成其他类型的结构。对于列表控件可能取值和对应的数据结构为: 
LVN_BEGINLABELEDIT 在开始某项编辑字符时发送,所用结构:
NMLVDISPINFO 
LVN_ENDLABELEDIT 在结束某项编辑字符时发送,所用结构:
NMLVDISPINFO 
LVN_GETDISPINFO 在需要得到某项信息时发送,(如得到某项的显示字符)所用结构:NMLVDISPINFO 
关于ON_NOTIFY有很多内容,将在以后的内容中进行详细讲解。 
关于动态提供结点所显示的字符:首先你在项时需要指明lpszItem参数为:
LPSTR_TEXTCALLBACK。在控件显示该结点时会通过发送TVN_GETDISPINFO来取得所需要的字符,在处理该消息时先将参数pNMHDR转换为
LPNMLVDISPINFO,然后填充其中item.pszText。通过item中的iItem,iSubItem可以知道当前显示的为那一项。下面的代码演示了这种方法: 
char szOut[8][3]={"No.1","No.2","No.3"};
//添加结点
m_list.InsertItem(LPSTR_TEXTCALLBACK,...)
m_list.InsertItem(LPSTR_TEXTCALLBACK,...)
//处理消息
void CParentWnd::OnGetDispInfoList(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pLVDI = (LV_DISPINFO*)pNMHDR;
pLVDI->item.pszText=szOut[pTVDI->item.iItem];
//通过iItem得到需要显示的字符在数组中的位置
*pResult = 0;
}

关于编辑某项的显示字符:(在报表风格中只对第一列有效)首先需要设置列表控件的LVS_EDITLABELS风格,在开始编辑时该控件将会发送
LVN_BEGINLABELEDIT,你可以通过在处理函数中返回TRUE来取消接下来的编辑,在编辑完成后会发送LVN_ENDLABELEDIT,在处理该消息时需要将参数
pNMHDR转换为LPNMLVDISPINFO,然后通过其中的item.pszText得到编辑后的字符,并重置显示字符。如果编辑在中途中取消该变量为NULL。下面的代码说明如何处理这些消息: 
//处理消息 LVN_BEGINLABELEDIT
void CParentWnd::OnBeginEditList(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pLVDI = (LV_DISPINFO*)pNMHDR;
if(pLVDI->item.iItem==0);//判断是否取消该操作
*pResult = 1;
else
*pResult = 0;
}
//处理消息 LVN_BEGINLABELEDIT
void CParentWnd::OnBeginEditList(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pLVDI = (LV_DISPINFO*)pNMHDR;
if(pLVDI->item.pszText==NULL);//判断是否已经取消取消编辑
m_list.SetItemText(pLVDI->item.iItem,0,pLVDI->pszText);
//重置显示字符
*pResult = 0;
}
上面讲述的方法所进行的消息映射必须在父窗口中进行(同样WM_NOTIFY的所有消息都需要在父窗口中处理)。
如何得到当前选中项位置:在列表控件中没有一个类似于ListBox中GetCurSel()的函数,但是可以通过调用GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);得到选中项位置。

list control控件中的风格选项:
m_list1.SetExtendedStyle( LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES );
LVS_EX_FULLROWSELECT表示可以点中行中的任意一个列选中这一条记录
LVS_EX_GRIDLINES表示列之间有分隔符号
LVS_EX_CHECKBOXES 表示每一行第一列是checkbox
LVCOLUMN:listviewcolumn
设置表头
lvColumn.mask = LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH|LVCF_FMT; 设置表头风格
lvColumn.fmt = LVCFMT_CENTER; 设置表头对齐方式
lvColumn.iSubItem = i; 表头列序
lvColumn.pszText = HeaderTxt[i]; 表头名称
lvColumn.cx = 90; 表头宽度
m_list.InsertColumn(i,&lvColumn); 插入列

转自:http://www.cnblogs.com/myths/articles/1917408.html

原创粉丝点击