正文

列表视图ListCtrl事件小结2009-04-06 12:58:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/xman/41996.html

分享到:

ListviewReport风格的Listctrl

一.向ListView添加Records

1CListCtrl::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 );

2CListCtrl::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的参数ListViewLVITEM结构。参见MSDNPlatform SDK Document->User Interface Servies->Windows Common Controls->List-View Controls->List-View Control Structures->LVITEM.

3CListCtrl::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

1Platform SDK Document->User Interface Servies->Windows Common Controls->Header Controls->

Header Control Notification MessagesHDN_

2Platform SDK Document->User Interface Servies->Windows Common Controls->List-View  Controls->List-View Control Notification MessagesLVN_

3Platform SDK Document->User Interface Services->Windows Common Controls->

Common API->NotificationsNM_

三.添加列表视图控件消息

1.在资源管理视图中右击ClistCtrl,选择“Events”利用向导为其添加消息处理。

2.HDN_LVN_NM_主要有两个参数(NMHDR* pNMHDR, LRESULT* pResult

1)参数1- NMHDR

参考MSDN 0ct2001Platform 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 0ct2001Platform 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结构

NMITEMACTIVATENMLVCACHEHINTNMLVDISPINFONMLVFINDITEMNMLVGETINFOTIPNMLVKEYDOWNNMLVODSTATECHANGENMLVSCROLL

通过他们获取所需信息。

2)参数2-LRESULT

LRESULA  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

http://lijinshui.bokee.com/5516893.html

阅读(5045) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册