效果演示
当前例程实现一种网格效果的列表视控件,双击选项可编辑,效果如上图。
实现过程
也是建立自己的项目工程,这里建立基于对话框的。添加列表控件。
2。复制两文件GRIDLISTCTRL.H,GRIDLISTCTRL.CPP,导入工程,这样就可以使用类CGridListCtrl。
包含头文件,将列表控件与类关联。注意列表控件属性设置为报表形式。
然后就初始化列表控件,添加行与列。
- m_GridListCtrl.PrepareControl(0);
- TCHAR rgtsz[4][10] = {_T("Column 1"), _T("Column 2"), _T("Column 3"), _T("Column 4")};
-
- LV_ITEM lvitem;
- LV_COLUMN lvcolumn;
- CRect rect;
- m_GridListCtrl.GetWindowRect( &rect );
-
- // Insert Image Lists
- m_pImageList = new CImageList();
- m_pImageList->Create( IDB_BITMAP1, 16, 1, RGB(255,255,255) );
- m_GridListCtrl.SetImageList( m_pImageList, LVSIL_SMALL );
-
- // Insert columns using the order field
- int i;
- for(i = 0; i < 4; i++)
- {
- lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
- lvcolumn.fmt = LVCFMT_LEFT;
- lvcolumn.pszText = rgtsz[i];
- lvcolumn.iSubItem = i;
- lvcolumn.iOrder = i;
- lvcolumn.cx = rect.Width() / 4;
- m_GridListCtrl.InsertColumn(i, &lvcolumn);
- }
-
- int iItem, iSubItem, iActualItem;
- TCHAR buffer[30];
-
- for (iItem = 0; iItem < 50; iItem++) // will now insert the items and subitems into the list view.
- for (iSubItem = 0; iSubItem < 4; iSubItem++)
- {
- lvitem.mask = LVIF_TEXT | (iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? iItem : iActualItem;
- lvitem.iSubItem = iSubItem;
- lvitem.iImage = (iItem%2)?0:2;
- sprintf( buffer, "Cell( %d, %d )", iItem+1, iSubItem+1);
- lvitem.pszText = buffer;
-
- if (iSubItem == 0)
- iActualItem = m_GridListCtrl.InsertItem(&lvitem);
- else
- m_GridListCtrl.SetItem(&lvitem);
- }
复制代码 初始化时使用了一资源ID,IDB_BITMAP1用于列表行图标,
还有一个变量 CImageList *m_pImageList;用于管理这些图标。
添加了这些后,就可以成功编译,运行可以查看效果。
但双击列表选项,并没有进行编辑状态。通过下面进行实现
3.复制两文件INPLACEEDIT.H,INPLACEEDIT.CPP,并导入工程。
在主对话框包含头文件且添加变量
#include "InPlaceEdit.h"
CInPlaceEdit *m_pListEdit;
在对话框类中添加列表控件的LVN_ENDLABELEDIT,LVN_BEGINLABELEDIT消息处理函数。
- void CCTestDlg::OnEndlabeleditList2(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
- // TODO: Add your control notification handler code here
- int item = pDispInfo->item.iItem;
- int subitem = pDispInfo->item.iSubItem;
- // This is coming from the grid list control notification.
- if( m_pListEdit )
- {
- CString str;
- if( pDispInfo->item.pszText )
- m_GridListCtrl.SetItemText( item, subitem, pDispInfo->item.pszText );
- delete m_pListEdit;
- m_pListEdit = 0;
- }
- *pResult = 0;
- }
- void CCTestDlg::OnBeginlabeleditList2(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
- CString str = pDispInfo->item.pszText;
- int item = pDispInfo->item.iItem;
- int subitem = pDispInfo->item.iSubItem;
- // Construct and create the custom multiline edit control.
- // We could just as well have used a combobox, checkbox,
- // rich text control, etc.
- m_pListEdit = new CInPlaceEdit( item, subitem, str );
- // Start with a small rectangle. We'll change it later.
- CRect rect( 0,0,1,1 );
- DWORD dwStyle = ES_LEFT;
- dwStyle |= WS_BORDER|WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL;
- m_pListEdit->Create( dwStyle, rect, &m_GridListCtrl, 103 );
- // Have the Grid position and size the custom edit control
- m_GridListCtrl.PositionControl( m_pListEdit, item, subitem );
- // Have the edit box size itself to its content.
- m_pListEdit->CalculateSize();
- // Return TRUE so that the list control will hnadle NOT edit label itself.
- *pResult = 1;
- }
复制代码
4.。这样就可以编译运行例程,选中单一选项后,再点击可以编译选中的单项。
下载地址:
|