Listview即Report风格的Listctrl。
一.向ListView添加Records
(1)CListCtrl::InsertColumn——Inserts a new column in a list view control.
int InsertColumn( int nCol, const LVCOLUMN* pColumn );
int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1 );
(2)CListCtrl::InsertItem——This method inserts an item into the list view control.
int InsertItem( const LVITEM* pItem );
int InsertItem( int nItem, LPCTSTR lpszItem );
int InsertItem( int nItem, LPCTSTR lpszItem, int nImage );
int InsertItem( UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam );
关于第四种InsertItem的参数ListView的LVITEM结构。参见MSDN:Platform SDK Document->User Interface Servies->Windows Common Controls->List-View Controls->List-View Control Structures->LVITEM.
(3)CListCtrl::SetItemText——This method changes the text of a list view item or subitem.
BOOL SetItemText( int nItem, int nSubItem, LPCTSTR lpszText );
(4)示例代码:
ClistCtrl m_reportList;
//以下为BOOL CListCtrlDemoDlg::OnInitDialog()中的部分代码:
//add 3 columns
m_reportList.InsertColumn(0, "Column0", LVCFMT_LEFT, 60);
m_reportList.InsertColumn(1, "Column1", LVCFMT_LEFT, 120);
m_reportList.InsertColumn(2, "Column2", LVCFMT_LEFT, 120);
CString strText;
int nColumnCount = m_reportList.GetHeaderCtrl()->GetItemCount();
// Insert 10 items in the list view control.
for (int i=0;i < 10;i++)
{
strText.Format(TEXT("item %d"), i);
// Insert the item, select every other item.
m_reportList.InsertItem(i, strText);
// Initialize the text of the subitems.
for (int j=1;j < nColumnCount;j++)
{
strText.Format(TEXT("sub-item %d %d"), i, j);
m_reportList.SetItemText(i, j, strText);
}
}
二.List-View的消息
List-View的消息主要有HDN_(HeaDeader Notification)、(LVN_)(ListView Notification)和NM_(Notification Message)三种。参考MSDN 0ct2001:
(1)Platform SDK Document->User Interface Servies->Windows Common Controls->Header Controls->
Header Control Notification Messages(HDN_)
(2)Platform SDK Document->User Interface Servies->Windows Common Controls->List-View Controls->List-View Control Notification Messages(LVN_)
(3)Platform SDK Document->User Interface Services->Windows Common Controls->
Common API->Notifications(NM_)
三.添加列表视图控件消息
1.在资源管理视图中右击ClistCtrl,选择“Events”利用向导为其添加消息处理。
2.HDN_、LVN_、NM_主要有两个参数(NMHDR* pNMHDR, LRESULT* pResult)
(1)参数1- NMHDR
参考MSDN 0ct2001:Platform SDK Document->User Interface Servies->Windows Common Controls->Common API->Structures
NMHDR:Notify Message HanDleR
Contains information about a notification message.
typedef struct tagNMHDR {
HWND hwndFrom; //Window handle to the control sending a message
UINT idFrom; Identifier of the control sending a message
UINT code; //Notification code. This member can be a control-specific notification code or it can be one of the common notification codes.
} NMHDR;
在WM_NOTIFY中,lParam中放的是一个称为NMHDR结构的指针,在wParam中放的则是控件的ID.
那么怎样由NMHDR参数获取具体列表视图控件的相关状态信息呢?
参考MSDN 0ct2001:Platform SDK Document->User Interface Servies->Windows Common Controls->List-View Controls->List-View Control Structures中有一个NMLISTVIEW结构。
NMLISTVIEW
Contains information about a list-view notification message. This structure is the same as the NM_LISTVIEW structure but has been renamed to fit standard naming conventions.
typedef struct tagNMLISTVIEW{
NMHDR hdr;
int iItem; //Identifies the list-view item, or -1 if not used.
int iSubItem; //Identifies the subitem, or zero if none.
UINT uNewState;
UINT uOldState;
UINT uChanged;
POINT ptAction;
LPARAM lParam;
} NMLISTVIEW, FAR *LPNMLISTVIEW;
由其定义可知此结构包含了NMHDR结构,由内存的存取规则和IS-A原理知可以将NMHDR通过强制转换来获取NMLISTVIEW结构指针,从而获取更多的信息。
List-View还有以下类似的结构定义中包含了NMHDR结构:
NMITEMACTIVATE、NMLVCACHEHINT、NMLVDISPINFO、NMLVFINDITEM、NMLVGETINFOTIP、NMLVKEYDOWN、NMLVODSTATECHANGE、NMLVSCROLL。
可通过他们获取所需信息。
(2)参数2-LRESULT
LRESUL:A 32-bit value returned from a window procedure or callback function。
pResult就是返回值,一般来说,返回1是不需要系统继续处理的,返回0需要系统处理。
(3)以下示例在列表视图(列标题)单击事件从NMHDR参数中获取单击的行号和列号以及项(列标题)内容
//ListViewDemoDlg.h中声明单击处理响应函数
protected:
afx_msg void OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnClickList1(NMHDR* pNMHDR, LRESULT* pResult);
//ListViewDemoDlg.cpp中添加NM_CLICK消息映射
BEGIN_MESSAGE_MAP(CListViewDemoDlg, CDialog)
//{{AFX_MSG_MAP(CListCtrlDemoDlg)
//……
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//ListViewDemoDlg.cpp中添加处理响应
//列表视图列标题单击事件
void CListCtrlDemoDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NMLISTVIEW* pNMLISTVIEW=(NMLISTVIEW*)pNMHDR;
int rowNo=pNMLISTVIEW->iItem;//行号,起始-1行
int columnNo=pNMLISTVIEW->iSubItem;//列号,起始列
//这里无法通过m_reportList.GetItemText(rowNo,columnNo)获取列标题!
//关于列标题的操作通常要获取要先获取CListCtrl的标题控件CHeaderCtrl
TCHAR lpBuffer[256];//提供内存存放标题
CHeaderCtrl *pHeaderCtrl=m_reportList.GetHeaderCtrl();//获取Column Header Ctrl
HDITEM hdi;//header item
hdi.mask = HDI_TEXT;
hdi.pszText = lpBuffer;
hdi.cchTextMax = 256;
pHeaderCtrl->GetItem(columnNo,&hdi);//获取第columnNo列标题hdi
CString str;
str.Format("%d行%d列:\"%s\"",rowNo,columnNo,hdi.pszText);
AfxMessageBox(str);
*pResult = 0;
}
//列表视图单击事件
void CListCtrlDemoDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NMLISTVIEW* pNMLISTVIEW=(NMLISTVIEW*)pNMHDR;
int rowNo=pNMLISTVIEW->iItem;//行号,起始-1行
int columnNo=pNMLISTVIEW->iSubItem;//列号,起始列
CString str;
str.Format("%d行%d列:\"%s\"",rowNo,columnNo,m_reportList.GetItemText(rowNo,columnNo));
AfxMessageBox(str);
*pResult = 0;
}
参考MSDN自带List-View实例:
Samples->MSDN Library Samples->Visual C++ Samples->MFC->Categorical List of MFC Samples->Windows Common Controls->
LISTHDR demonstrates the List View and Header Common Controls.
ROWLIST illustrates full row selection in the report mode of the CListView MFC common control class. Using toolbar buttons, you can toggle between views of the list items.
参考:
http://zhidao.baidu.com/question/89429307.html
http://hi.baidu.com/shallow_sleep/blog/item/7b531351ddef9f878c543087.html
http://topic.csdn.net/t/20040720/11/3191608.html
http://www.host01.com/article/software/cc/20060917233214357.htm
http://www.cppblog.com/justin-shi/archive/2008/06/14/53192.aspx
评论