本文由CSDN用户zuishikonghuan所作,转载请注明出处:http://blog.csdn.net/zuishikonghuan/article/details/46893113
在上一篇”[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的样式,就像资源管理器一样。