网上下了一个源码,但是封装不太好,太多代码在dialog中,重新整合了一下
1.代码目录梳理
$(ProjectDir)/EditListCtrl
----inc,头文件
----src,源文件
2.继承CEditListCtrl :CListCtrl,复写PreSubClassWindow,并利用反射,添加其单击及双击响应消息,使这段代码与dialog脱离,完善封装
$(ProjectDir)/EditListCtrl/inc//EditListCtrl.h
#include "ListCtrlDateEdit.h" #include "ListCtrlEdit.h" #include "ListCtrlComboBox.H" #include "ListCtrlMaskEdit.H" #include "ListCtrlCurrencyEdit.H" class CEditListCtrl : public CListCtrl { public: DECLARE_MESSAGE_MAP() void PreSubclassWindow(); void OnClickList(NMHDR* pNMHDR, LRESULT* pResult); void OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult); public: CListCtrlCurrencyEdit m_ListCurrencyEdit; CListCtrlMaskEdit m_ListMaskEdit; CListCtrlDateEdit m_ListDateEdit; CListCtrlEdit m_ListEdit; CListCtrlComboBox m_ListComboBox; };
$(ProjectDir)/EditListCtrl/inc//EditListCtrl.cpp
#include "StdAfx.h" #include "EditListCtrl.h" BEGIN_MESSAGE_MAP(CEditListCtrl, CListCtrl) ON_NOTIFY_REFLECT(NM_CLICK, OnClickList) ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclkList) END_MESSAGE_MAP() void CEditListCtrl::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult) { CListCtrlEditBase::OnOpen(this, pNMHDR); *pResult = 0; } void CEditListCtrl::OnClickList( NMHDR* pNMHDR, LRESULT* pResult ) { int nItem, nSubItem; if (CListCtrlEditBase::HitTestEx(this, pNMHDR, nItem, nSubItem) && nSubItem == 4) { m_ListComboBox.ShowEdit(nItem, nSubItem); } *pResult = 0; } void CEditListCtrl::PreSubclassWindow() { ModifyStyle(0 , LVS_REPORT); SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); CImageList imagelist; int itemheight = 18; imagelist.Create(1, itemheight, ILC_COLOR4, 10, 10); SetImageList(&imagelist, LVSIL_SMALL); }
代码下载:含一个工程和示例主程序
http://download.csdn.net/detail/silyvin/9191243