在鼠标消息中模仿Windows Explore文件和文件夹的选择

 

维护旧代码中,有一个定制控件跟list contrl很类似,但是bug不少,真不如改用list control,哎。发现对控件内item的选择操作根本不对,自己又模仿Windows Explore中对文件和文件夹的选择,改了一下。在这里记下,以防以后再用到类似的跟list control一样的“轮子”:

 

void CMyWindow::OnLButtonDown(UINT nFlags, CPoint point) { static int nShiftStart = -1; int iSelIndex = IndexHitTest(point); if (-1 != iSelIndex) { // The focuse index should be remembered when mouse down in case the mouse move happens. m_nFocusIndex = iSelIndex; if (nFlags & MK_CONTROL) { if (nFlags & MK_SHIFT) { if (-1 != nShiftStart) { SelectItems(min(iSelIndex, nShiftStart), max(iSelIndex, nShiftStart)); } else { nShiftStart = iSelIndex; SelectItem(iSelIndex); } } else { nShiftStart = iSelIndex; } } else { if (nFlags & MK_SHIFT) { UnSelectAll(); if (-1 != nShiftStart) { SelectItems(min(iSelIndex, nShiftStart), max(iSelIndex, nShiftStart)); } else { nShiftStart = iSelIndex; SelectItem(iSelIndex); } } else { if (!current_item.selected) { UnSelectAll(); SelectItem(iSelIndex); } nShiftStart = iSelIndex; } } } else if (!(nFlags & MK_CONTROL) && !(nFlags & MK_SHIFT)) { m_nFocusIndex = -1; nShiftStart = -1; UnSelectAll(); } Invalidate(FALSE); CWnd::OnLButtonDown(nFlags, point); } void CMyWindow::OnLButtonUp(UINT nFlag, CPoint point) { int iSelIndex = IndexHitTest(point); if (iSelIndex != -1) { if (nFlags & MK_CONTROL) { if (nFlags & MK_SHIFT) { // Shift key dose not take effect when mouse up. } else { if (current_item.bSelected) { UnSelectItem(iSelIndex); } else { SelectItem(iSelIndex); } } } else { if (nFlags & MK_SHIFT) { // Shift key dose not take effect when mouse up. } else { // If not only the current item is selected, we unselect other items. if (GetSelCount() > 1) { UnSelectAll(); SelectItem(iSelIndex); } } } } Invalidate(FALSE); CWnd::OnLButtonUp(nFlag, point); }

你可能感兴趣的:(windows,list)