不是NM_CLICK,NM_CLICKNotifies a control's parent window that the user has clicked the left mouse button within the control应该是BN_CLICK,但你可以换另一种方法去做:在其父类中添加一个函数:void SetLVCheck(WPARAM ItemIndex, BOOL bCheck);其实现为: m_list 为CListCtrl变量 void CParentDlg::SetLVCheck(WPARAM ItemIndex, BOOL bCheck) { LV_ITEM lvi; lvi.stateMask = LVIS_STATEIMAGEMASK; lvi.state = UINT((int(bCheck) + 1) << 12); ::SendMessage (m_list.m_hWnd, LVM_SETITEMSTATE, (WPARAM)ItemIndex, (LPARAM)(LV_ITEM FAR*)&lvi); }你还可以增加其他的辅助函数:如 //******************************************************************** //功能描述:选择所有的项目 //******************************************************************** void CParentDlg::OnAddall() { // TODO: Add your control notification handler code here int count=m_list.GetItemCount(); for(int i=0;i<count;i++) SetLVCheck(i,TRUE); } //********************************************************************** //功能描述:选择部分项目(在此之前,要单击这个按钮取消以前所有的选择) //********************************************************************** void CDrawGoodDlg::OnCustomselect() { int count=m_list.GetItemCount(); for(int i=0;i<count;i++) SetLVCheck(i,FALSE); }
ListView_GetCheckState BOOL ListView_GetCheckState( HWND hwndLV, UINT iIndex );Determines if an item in a list view control is selected. This should be used only for list view controls that have the LVS_EX_CHECKBOXES style. Returns nonzero if the given item is selected, or zero otherwise. If this macro is applied to a list view control that does not have check boxes enabled, the return value is not reliable. hwndLV Handle to a list view control. iIndex Index of the item for which to retrieve the check state.
How to get notification when an item is checked / unchecked: void DemoDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; *pResult = 0; if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0) return; // No change BOOL bPrevState = (BOOL)(((pNMListView->uOldState & LVIS_STATEIMAGEMASK)>>12)-1); // Old check box state if (bPrevState < 0) // On startup there's no previous state bPrevState = 0; // so assign as false (unchecked) // New check box state BOOL bChecked=(BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1); if (bChecked < 0) // On non-checkbox notifications assume false bChecked = 0; if (bPrevState == bChecked) // No change in check box return;
// Now bChecked holds the new check box state // .... }
Adding Checkboxes to a list control By Eran Yariv A list view gives the GUI designer many options. One of the best is the ability to display tabular data in columns, sort columns, add images and more. This is well implemented by CListView in MFC. A checked list box enables the GUI designer to get the users picked options via a checkbox on every list item. This is well implemented by CCheckListBox in MFC. http://www.codeproject.com/listctrl/listcheckbox.asp
m_list 为CListCtrl变量
void CParentDlg::SetLVCheck(WPARAM ItemIndex, BOOL bCheck)
{
LV_ITEM lvi;
lvi.stateMask = LVIS_STATEIMAGEMASK;
lvi.state = UINT((int(bCheck) + 1) << 12);
::SendMessage (m_list.m_hWnd, LVM_SETITEMSTATE, (WPARAM)ItemIndex,
(LPARAM)(LV_ITEM FAR*)&lvi);
}你还可以增加其他的辅助函数:如
//********************************************************************
//功能描述:选择所有的项目
//********************************************************************
void CParentDlg::OnAddall()
{
// TODO: Add your control notification handler code here
int count=m_list.GetItemCount();
for(int i=0;i<count;i++)
SetLVCheck(i,TRUE);
}
//**********************************************************************
//功能描述:选择部分项目(在此之前,要单击这个按钮取消以前所有的选择)
//**********************************************************************
void CDrawGoodDlg::OnCustomselect()
{
int count=m_list.GetItemCount();
for(int i=0;i<count;i++)
SetLVCheck(i,FALSE);
}
BOOL ListView_GetCheckState(
HWND hwndLV,
UINT iIndex
);Determines if an item in a list view control is selected. This should be used only for list view controls that have the LVS_EX_CHECKBOXES style. Returns nonzero if the given item is selected, or zero otherwise. If this macro is applied to a list view control that does not have check boxes enabled, the return value is not reliable.
hwndLV
Handle to a list view control.
iIndex
Index of the item for which to retrieve the check state.
void CColorListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
OnLBDSetItemColor(nFlags, point);
CListCtrl::OnLButtonDown(nFlags, point);
}
void CColorListCtrl::OnLBDSetItemColor(UINT nFlags, CPoint point)
{
LVHITTESTINFO hittest;
hittest.pt = point;
SubItemHitTest(&hittest); if(hittest.flags == LVHT_ONITEMSTATEICON)
{
int checkItem = hittest.iItem;
if(!GetCheck(checkItem))
{
SetFullRowColor(checkItem, RGB(255,0,0),RGB(0,0,0));
}
else
{
SetFullRowColor(checkItem, RGB(255,255,255), RGB(0,0,0));
}
}
}
void DemoDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0; if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
return; // No change BOOL bPrevState = (BOOL)(((pNMListView->uOldState &
LVIS_STATEIMAGEMASK)>>12)-1); // Old check box state
if (bPrevState < 0) // On startup there's no previous state
bPrevState = 0; // so assign as false (unchecked) // New check box state
BOOL bChecked=(BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);
if (bChecked < 0) // On non-checkbox notifications assume false
bChecked = 0; if (bPrevState == bChecked) // No change in check box
return;
// Now bChecked holds the new check box state // ....
}
By Eran Yariv A list view gives the GUI designer many options. One of the best is the ability to display tabular data in columns, sort columns, add images and more. This is well implemented by CListView in MFC. A checked list box enables the GUI designer to get the users picked options via a checkbox on every list item. This is well implemented by CCheckListBox in MFC.
http://www.codeproject.com/listctrl/listcheckbox.asp