我正在编写一个简单的Win32程序,该程序具有带复选框和启用多行选择的ListView。 :
似乎复选框检查和行选择是两种不同的行为。有没有办法同步这两种行为,每当选中一个复选框时,将选择该行,每当选中一行时,将检查相应的复选框?
答案 0 :(得分:2)
“每当选择一行时,都会选中相应的复选框”
检查WM_NOTIFY
和LVIS_SELECTED
标志以检测用户何时选择行。并使用ListView_SetCheckState
检查复选框:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
“只要选中复选框,就会选中该行”
选中WM_NOTIFY
和LVIS_STATEIMAGEMASK
标志以检测何时选中复选框,然后使用ListView_SetItemState
选择行。
这也可能导致递归调用,因为我们更改了响应复选框的行,并且我们更改了复选框以响应行选择。使用busy
变量来停止递归调用。
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;