在界面数据显示时一般使用ListBox或支持分列显示的ListCtrl,如果同一个界面中有两个以上关联数据表格要显示,那么使用ListBox的好处在任何时候都可以清晰的表明数据行选中/未选中状态,从而表示表格之间的关系,而使用ListCtrl中则状态不明显,比如MFC类向导,其中的数据显示使用ListBox,在多个ListBox窗口中可以同时显示每个窗口的数据选中状态,注意一点,MFC类向导使用的ListBox是分列显示的。那么我们能否也实现一个分列显示的ListBox。在WTL可以轻易实现这一点。代码队在最后:
如何使用:在WTL使用时,需要注意的是在资源编辑器中将ListBox属性设计为LBS_OWNERDRAWFIXED和LBS_HASSTRINGS,需要在消息宏添加REFLECT_NOTIFICATION()以支持自画消息。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
在添加数据时数据之间使用“;”来进行列之间分开,当然你也可以修改源代码使用其他分隔符或者定义一个变量来指示分隔符,CDevListBoxImpl中的m_nDivider 用来控制列的宽度,如果某一行的列宽超过这个宽度将自动增加宽度来容纳内容,就象MFC的类向导所做的一样,我并没有指定具体一列的宽度,对于我来言,两列足够了。然后需要的话你也可以修改代码使用一个int数组来保存列宽,我相信这对你来言相当容易。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
CDevListBoxImpl的代码
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)