[Win32SDK基本]ListView Controls(2)Report (details) View 详解(续)
2015-07-15 14:41
645 查看
本文由CSDN用户zuishikonghuan所作,转载请注明出处:/article/9672513.html
在上一篇”[Win32SDK基本]ListView Controls(1)Report (details) View 详解“中,博主详细解说了报表列表框的使用,但是有一点忘了说,那就是listview自带的check box的功能。这回就在上一篇文章的基础上进一步开发。
先看看这两个风格:
LVS_EX_CHECKBOXES
Version 4.70. Enables check boxes for items in a list-view control. When set to this style, the control creates and sets a state image list with two images using DrawFrameControl. State image 1 is the unchecked box, and state image 2 is the checked box. Setting
the state image to zero removes the check box.
Version 6.00 and later Check boxes are visible and functional with all list view modes except the tile view mode introduced in ComCtl32.dll version 6. Clicking a checkbox in tile view mode only selects the item; the state does not change.
You can obtain the state of the check box for a given item with ListView_GetCheckState. To set the check state, use ListView_SetCheckState. If this style is set, the list-view control automatically toggles the check state when the user clicks the check box
or presses the space bar.
LVS_EX_AUTOCHECKSELECT
Windows Vista and later. Automatically select check boxes on single click.
加上LVS_EX_CHECKBOXES风格以后,来看看效果图:
![](http://img.blog.csdn.net/20150715145449500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
可以看到每一项前面都多了一个check box,单击即可选中
![](http://img.blog.csdn.net/20150715145537472?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
如果我们再把LVS_EX_AUTOCHECKSELECT风格加上:
![](http://img.blog.csdn.net/20150715145635633?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
可以看到只有鼠标悬停的那一项才会有check box,而且无法多选
![](http://img.blog.csdn.net/20150715145749661?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
但是如果我们把之前的LVS_SINGLESEL去掉
就会发现LVS_EX_CHECKBOXES | LVS_EX_AUTOCHECKSELECT风格会多出一个全选功能,同时可以多选了
![](http://img.blog.csdn.net/20150715150004801?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
ListView_GetCheckState 获取指定表项的checkbox选中状态
hwndLV:listview的句柄
iIndex:表项的序号
ListView_SetCheckState 设置指定表项的checkbox选中状态
hwndLV:listview的句柄
iIndex:表项的序号
fCheck:TRUE表示选中,FALSE表示不选中。
另外关于listview的其他几种(图标,小图标,列表)就不详细说了,和上一篇”[Win32SDK基本]ListView Controls(1)Report
(details) View 详解“中的换汤不换药,另外有必要说明的一点是,可以使用SetWindowLong动态设置listview的样式,就像资源管理器一样。
===============================================
2016.3.11补充:
纠正上一篇中的一个错误,ImageList_Create的第三个参数应该使用ILC_COLOR32才可以获取最佳效果。
有位朋友留言说要ListView分组的效果,就简单说一下吧。
首先看看LVGROUP结构:
成员含义在MSDN已经非常详细了,简单说mask是掩码,pszHeader是组头部的标题,iGroupId是组ID。
你会发现大部分特性需要Windows Vista以上版本的系统才能支持(#if _WIN32_WINNT >= 0x0600) 考虑到要兼容XP系统,因此对于cbSize我们不能草率的使用sizeof(LVGROUP);来处理,我们应该设置为LVGROUP_V5_SIZE。
这一点MSDN并没有提及,其实,如果你做过托盘气泡的XP处理,你会发现对于托盘气泡的NOTIFYICONDATA结构,size我们可以设置为NOTIFYICONDATA_V3_SIZE来兼容XP,这个在MSDN中有明确说明。在这儿,ListView的头文件中给出来了LVGROUP_V5_SIZE宏来使用XP的LVGROUP结构,如下:
还有一点必须要强调的是,如果你看的比较仔细肯已经发现了,这个结构中的字符串都是Unicode字符串!而不是TCHAR*,TCHAR会根据是否定义_UNICODE宏来展开为char和wchar_t,而这里只能是wchar_t。
可以使用ListView_InsertGroup来添加组,这个宏其实是发送的LVM_INSERTGROUP消息。
添加组的方法:
效果:
![](http://img.blog.csdn.net/20160311101649267?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
完整代码:
在上一篇”[Win32SDK基本]ListView Controls(1)Report (details) View 详解“中,博主详细解说了报表列表框的使用,但是有一点忘了说,那就是listview自带的check box的功能。这回就在上一篇文章的基础上进一步开发。
先看看这两个风格:
LVS_EX_CHECKBOXES
Version 4.70. Enables check boxes for items in a list-view control. When set to this style, the control creates and sets a state image list with two images using DrawFrameControl. State image 1 is the unchecked box, and state image 2 is the checked box. Setting
the state image to zero removes the check box.
Version 6.00 and later Check boxes are visible and functional with all list view modes except the tile view mode introduced in ComCtl32.dll version 6. Clicking a checkbox in tile view mode only selects the item; the state does not change.
You can obtain the state of the check box for a given item with ListView_GetCheckState. To set the check state, use ListView_SetCheckState. If this style is set, the list-view control automatically toggles the check state when the user clicks the check box
or presses the space bar.
LVS_EX_AUTOCHECKSELECT
Windows Vista and later. Automatically select check boxes on single click.
加上LVS_EX_CHECKBOXES风格以后,来看看效果图:
//。。。。 ListView_SetExtendedListViewStyle(listview1, LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES | LVS_EX_CHECKBOXES);//设置listview扩展风格 //。。。。
可以看到每一项前面都多了一个check box,单击即可选中
如果我们再把LVS_EX_AUTOCHECKSELECT风格加上:
可以看到只有鼠标悬停的那一项才会有check box,而且无法多选
但是如果我们把之前的LVS_SINGLESEL去掉
listview1 = CreateWindowEx(WS_EX_STATICEDGE, TEXT("SysListView32"), NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL, 10, 10, 400, 400, hwnd, (HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); ListView_SetExtendedListViewStyle(listview1, LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES | LVS_EX_CHECKBOXES | LVS_EX_AUTOCHECKSELECT);//设置listview扩展风格 //。。。。
就会发现LVS_EX_CHECKBOXES | LVS_EX_AUTOCHECKSELECT风格会多出一个全选功能,同时可以多选了
ListView_GetCheckState 获取指定表项的checkbox选中状态
BOOL ListView_GetCheckState( HWND hwndLV, UINT iIndex );
hwndLV:listview的句柄
iIndex:表项的序号
ListView_SetCheckState 设置指定表项的checkbox选中状态
void ListView_SetCheckState( HWND hwndLV, UINT iIndex, BOOL fCheck );
hwndLV:listview的句柄
iIndex:表项的序号
fCheck:TRUE表示选中,FALSE表示不选中。
另外关于listview的其他几种(图标,小图标,列表)就不详细说了,和上一篇”[Win32SDK基本]ListView Controls(1)Report
(details) View 详解“中的换汤不换药,另外有必要说明的一点是,可以使用SetWindowLong动态设置listview的样式,就像资源管理器一样。
===============================================
2016.3.11补充:
纠正上一篇中的一个错误,ImageList_Create的第三个参数应该使用ILC_COLOR32才可以获取最佳效果。
有位朋友留言说要ListView分组的效果,就简单说一下吧。
首先看看LVGROUP结构:
typedef struct LVGROUP { UINT cbSize; UINT mask; LPWSTR pszHeader; int cchHeader; LPWSTR pszFooter; int cchFooter; int iGroupId; UINT stateMask; UINT state; UINT uAlign; #if _WIN32_WINNT >= 0x0600 LPWSTR pszSubtitle; UINT cchSubtitle; LPWSTR pszTask; UINT cchTask; LPWSTR pszDescriptionTop; UINT cchDescriptionTop; LPWSTR pszDescriptionBottom; UINT cchDescriptionBottom; int iTitleImage; int iExtendedImage; int iFirstItem; UINT cItems; LPWSTR pszSubsetTitle; UINT cchSubsetTitle; #endif } LVGROUP, *PLVGROUP;
成员含义在MSDN已经非常详细了,简单说mask是掩码,pszHeader是组头部的标题,iGroupId是组ID。
你会发现大部分特性需要Windows Vista以上版本的系统才能支持(#if _WIN32_WINNT >= 0x0600) 考虑到要兼容XP系统,因此对于cbSize我们不能草率的使用sizeof(LVGROUP);来处理,我们应该设置为LVGROUP_V5_SIZE。
这一点MSDN并没有提及,其实,如果你做过托盘气泡的XP处理,你会发现对于托盘气泡的NOTIFYICONDATA结构,size我们可以设置为NOTIFYICONDATA_V3_SIZE来兼容XP,这个在MSDN中有明确说明。在这儿,ListView的头文件中给出来了LVGROUP_V5_SIZE宏来使用XP的LVGROUP结构,如下:
//in CommCtrl.h #define LVGROUP_V5_SIZE CCSIZEOF_STRUCT(LVGROUP, uAlign)
还有一点必须要强调的是,如果你看的比较仔细肯已经发现了,这个结构中的字符串都是Unicode字符串!而不是TCHAR*,TCHAR会根据是否定义_UNICODE宏来展开为char和wchar_t,而这里只能是wchar_t。
可以使用ListView_InsertGroup来添加组,这个宏其实是发送的LVM_INSERTGROUP消息。
添加组的方法:
LVGROUP group1; group1.cbSize = LVGROUP_V5_SIZE; group1.mask = LVGF_HEADER | LVGF_GROUPID; group1.pszHeader = L"组1"; group1.iGroupId = 0; ListView_InsertGroup(listview1, -1, &group1);//参数2:The index of the item where the group is added. If this is -1, the group is added at the end of the list. group1.pszHeader = L"组2 - Hello World"; group1.iGroupId = 1; ListView_InsertGroup(listview1, -1, &group1);创建Item时掩码要加上“LVIF_GROUPID”,并用item的iGroupId成员指定组ID
//创建项目 item1.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT | LVIF_GROUPID; item1.pszText = L"项目"; item1.iItem = 0;//项目号 item1.iImage = 0;//图片号 item1.iIndent = 0;//缩进 item1.iGroupId = 0;//组ID SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1);
效果:
完整代码:
//创建listview listview1 = CreateWindowEx(WS_EX_STATICEDGE, TEXT("SysListView32"), NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL, 10, 10, 400, 400, hwnd, (HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); ListView_SetExtendedListViewStyle(listview1, LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES);//设置listview扩展风格 SendMessage(listview1, WM_SETFONT, (WPARAM)GetStockObject(17), 0); SetWindowTheme(listview1, L"Explorer", NULL); SendMessage(listview1, LVM_ENABLEGROUPVIEW, TRUE, 0);//启用分组支持 LVGROUP group1; group1.cbSize = LVGROUP_V5_SIZE; group1.mask = LVGF_HEADER | LVGF_GROUPID; group1.pszHeader = L"组1"; group1.iGroupId = 0; ListView_InsertGroup(listview1, -1, &group1); group1.pszHeader = L"组2 - Hello World"; group1.iGroupId = 1; ListView_InsertGroup(listview1, -1, &group1); //创建图片列表 imglist1 = ImageList_Create(20, 20, ILC_COLOR32, 0, 0); ImageList_AddIcon(imglist1, LoadIcon((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), TEXT("ICON_2"))); ImageList_AddIcon(imglist1, LoadIcon((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), TEXT("ICON_1"))); HICON ico; ExtractIconEx(L"C:\\Program Files\\Microsoft Visual Studio 14.0\\Common7\\Tools\\spyxx.exe", 0, &ico, NULL, 1); ImageList_AddIcon(imglist1, ico); ListView_SetImageList(listview1, imglist1, LVSIL_SMALL); //内存清零 RtlZeroMemory(&list1, sizeof(LVCOLUMN)); RtlZeroMemory(&item1, sizeof(LVITEM)); //创建列 list1.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;//掩码 list1.fmt = LVCFMT_LEFT;//左对齐 list1.cx = 100;//列宽 list1.pszText = L"列1"; SendMessage(listview1, LVM_INSERTCOLUMN, 0, (LPARAM)&list1);//创建列 list1.pszText = L"list2"; list1.cx = 200; SendMessage(listview1, LVM_INSERTCOLUMN, 1, (LPARAM)&list1); list1.pszText = L"hello world"; SendMessage(listview1, LVM_INSERTCOLUMN, 2, (LPARAM)&list1); list1.pszText = L"happy"; SendMessage(listview1, LVM_INSERTCOLUMN, 3, (LPARAM)&list1); //创建项目 item1.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT | LVIF_GROUPID; item1.pszText = L"项目"; item1.iItem = 0;//项目号 item1.iImage = 0;//图片号 item1.iIndent = 0; item1.iGroupId = 0; SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); item1.iItem = 1; item1.iImage = 0; SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); item1.pszText = L"item 3"; item1.iItem = 2; item1.iImage = 1; item1.iIndent = 1;//缩进一个图像单位 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); item1.pszText = L"第四个"; item1.iItem = 3; item1.iImage = 2; item1.iIndent = 0; SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); item1.iGroupId = 1; item1.pszText = L"我在组2内"; item1.iItem = 4; item1.iImage = 1; item1.iIndent = 0; SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); item1.pszText = L"item 5"; item1.iItem = 2; item1.iImage = 0; item1.iIndent = 1; SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); //创建子项目 item1.mask = LVIF_TEXT | LVIF_IMAGE; item1.iItem = 1; item1.iSubItem = 1; item1.iImage = -1; item1.pszText = TEXT("子项目"); SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1); item1.iItem = 1; item1.iSubItem = 2; item1.pszText = TEXT("子项目"); SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1); item1.iItem = 1; item1.iSubItem = 3; item1.pszText = TEXT("子项目"); SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1);
相关文章推荐
- container_of的使用
- DDD(Domain Driver Designer) 领域驱动设计简介
- Scala之Trait的使用
- poj 1273 Drainage Ditches(最大流)
- CONTAINING_RECORD IN EFI
- RapidMiner studio之MainFrame源码分析
- Windows Mobile Emulator:Failed to open the VPC Network Driver.
- HDU 2686 Matrix 3376 Matrix Again(费用流)
- Microsoft.AlphaImageLoader滤镜讲--透明处理<转>
- HTTP Error 503. The service is unavailable
- Software caused connection abort: recv failed
- INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING
- Determining IP information for eth0... failed; no link present. Check cable?
- storm on yarn安装时 提交到yarn失败 failed
- storm on yarn安装时 提交到yarn失败 failed
- 配置OpenStack的glance出现错误Failed to show index. Got error: You are not authenticated.
- Grails最佳实践
- [tomcat error] failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
- [LeetCode] Climbing Stairs
- 根据windows域名找ip