你说的对。处理多选检查状态后,您可以在代码中设置列表控件句柄检查状态切换后进行切换。你必须修改你的代码。我只是假设您正在处理 LVN_KEYDOWN 消息。(toggleSelect 是全局的吗?为什么?)
void CYourDlgClass::OnLvnKeydownList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLVKEYDOWN pLVKeyDown = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR);
if(VK_SPACE == pLVKeyDown->wVKey && m_cListCtrl.GetSelectedCount() > 1)
{
toggleSelect();
*pResult = 1;
return;
}
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CYourDlgClass::toggleSelect()
{
POSITION pos = m_cListCtrl.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_cListCtrl.GetNextSelectedItem(pos);
BOOL check = m_cListCtrl.GetCheck(index);
m_cListCtrl.SetCheck(index, !check);
}
}
将 *pResult 设置为 0 表示消息已被处理,不需要进一步处理。我建议从 CListCtrl 派生类并以类似方式处理 WM_KEYDOWN:
void CMultiCheckListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(VK_SPACE == nChar && GetSelectedCount() > 1)
{
POSITION pos = GetFirstSelectedItemPosition();
while(pos)
{
int index = GetNextSelectedItem(pos);
BOOL check = GetCheck(index);
SetCheck(index, !check);
}
return;
}
CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}