=================================版权声明=================================
版权声明:本文为博主原创文章 未经许可不得转载
请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我
未经作者授权勿用于学术性引用。
未经作者授权勿用于商业出版、商业印刷、商业引用以及其他商业用途。
本文不定期修正完善,为保证内容正确,建议移步原文处阅读。 <--------总有一天我要自己做一个模板干掉这只土豆
本文链接:http://www.cnblogs.com/wlsandwho/p/4736907.html
耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html
=======================================================================
我用的是VS2010,并没有打SP1包。
=======================================================================
MFC中CVSListBox看起来很好的样子,其实并不是像看起来那样。这个控件封装的很严密,在向导里并不能添加消息响应事件之类的东西。
微软MSDN上也没有什么有用的信息,只说了句类似“要文档没有,爱用用、不用Gun,想用自己看源文件”的话。
幸好略微提供了一个源码,说了怎样写派生类。
既然如此,那我就自己写一个吧。
=======================================================================
我知道并没有人想知道怎样实现的,我也没做过开源,可是,可是这次我就github了一次。
https://github.com/Simon-Wong/CVSListBoxWLS
这里只贴头文件吧。
=======================================================================
头文件:
1 #pragma once 2 // VSListBoxWLS.h : 定义文件 3 #include"afxvslistbox.h" 4 ////////////////////////////////////////////////////////////////////////// 5 // CVSListBoxWLS 6 //by wls 2015/08/12 13:55:23 7 //The memory leaks is made by MFC,not me,for the little buttons in CVSListBox called CMFCToolTipCtrl or something named XXXButton. 8 //If you change the Appearance of CVSListBox to false,it will work with no memory leaks. 9 //That means HEHE.10 11 typedef BOOL (*FUNCDOSOMETHING)(LPVOID /*lpRawData*/,LPVOID /*lpNewData*/,CObject* /*pObj*/);//by wls 2015/08/15 09:41:31 You know that12 13 BOOL FuncDoNothing(LPVOID,LPVOID,CObject*);//by wls 2015/08/15 09:40:15 An idle function to do nothing14 15 class CVSListBoxWLS : public CVSListBox16 {17 DECLARE_DYNAMIC(CVSListBoxWLS)18 19 public:20 CVSListBoxWLS();21 virtual ~CVSListBoxWLS();22 23 private:24 BOOL m_bEnableRepeatText;//True for enable to insert repeat text,false for disable.25 BOOL m_bDoAfterAddItemWhenever;//True for enable to do something whenever really insert text or not due to m_bEnableRepeatText,false for only really inserting text26 BOOL m_bDoAfterRenameWhenever;//usage like above27 BOOL m_bAddItem;28 BOOL m_bCheckTextLen;29 BOOL m_nTextLen;30 31 CString m_strWhenSelecting;//by wls 2015/08/13 08:53:28 the raw string32 CString m_strAfterOp;//by wls 2015/08/13 08:53:37 the new string33 CObject* m_pObjforOperatingFunc;34 35 FUNCDOSOMETHING m_fdsBeforeRemoveItem;36 FUNCDOSOMETHING m_fdsAfterAddItemNoRepeat;37 FUNCDOSOMETHING m_fdsAfterAddItemWhenever;38 FUNCDOSOMETHING m_fdsAfterRenameItemNoRepeat;39 FUNCDOSOMETHING m_fdsAfterRenameItemWhenever;40 41 private:42 BOOL IsExistText(CString strText);43 void GetItemTextAfterOp(int nItem);44 45 public:46 CString GetTextWhenSelecting();47 48 void EnableRepeatText(BOOL bRepeat=FALSE);//by wls 2015/08/15 09:51:42 It depends the client code in where you save all the data to the very end.49 void EnableDoAfterAddItemWhenever(BOOL bRepeat=FALSE);50 void EnableDoAfterRenameWhenever(BOOL bRepeat=FALSE);51 52 void EnableCheckTextLen(BOOL bChk=FALSE);53 void SetTextLegalLength(int nLen=10);54 55 void SetObjforOperation(CObject* obj=NULL);56 57 void SetOperationBeforeRemoveItem(FUNCDOSOMETHING fds);58 void SetOperationAfterAddItemNoRepeat(FUNCDOSOMETHING fds);59 void SetOperationAfterAddItemWhenever(FUNCDOSOMETHING fds);60 void SetOperationAfterRenameItemNoRepeat(FUNCDOSOMETHING fds);61 void SetOperationAfterRenameItemWhenever(FUNCDOSOMETHING fds);62 63 public:64 void SetItemText(int nIndex, const CString& strText);65 66 BOOL OnBeforeRemoveItem(int iItem);67 void OnAfterAddItem(int nItem);68 void OnAfterRenameItem(int nItem);69 void OnSelectionChanged();70 71 protected:72 DECLARE_MESSAGE_MAP()73 };
=======================================================================
一定会有人吐槽我不写详细注释,不解释了。
=======================================================================
使用起来很简单。
Enable开头的函数设置功能开关,默认关闭状态。
SetOp开头的函数设置类似回调函数的功能,默认执行空操作。
SetObj开头的函数设置类似回调函数中执行功能时可能用到的外部类变量。
Get和Set不解释。
On开头的函数是重写了基类的响应函数。
=======================================================================
原标题:问题解决——CVSListBox的使用
关键词: