if (BST_CHECKED == IsDlgButtonChecked( IDC_CHECK1 ))
{
int n = m_list.GetTopIndex(); //大概知道问题出在这
int nlast = n + m_list.GetItemCount();
for (;n<nlast;n++)
{
m_list.SetItemState(n,LVIS_SELECTED,LVIS_SELECTED);
}
}
else
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
知道问题所在,但不知道该怎么去响应CheckBox的点击事件消息,望各位前辈给点意见
12 个解决方案
#1
得到listctrl中所有行的checkbox的状态
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
得到listctrl中所有行的checkbox的状态
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
判断是否点击在listctrl的checkbox上
添加listctrl控件的NM_CLICK消息相应函数
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//判断是否点在checkbox上
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("点在listctrl的checkbox上");
}
*pResult = 0;
}
如果想在listctrl中插入各种控件,可以用我写这个,附demo: http://download.csdn.net/detail/paschen/8927009
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
得到listctrl中所有行的checkbox的状态
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
判断是否点击在listctrl的checkbox上
添加listctrl控件的NM_CLICK消息相应函数
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//判断是否点在checkbox上
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("点在listctrl的checkbox上");
}
*pResult = 0;
}
如果想在listctrl中插入各种控件,可以用我写这个,附demo: http://download.csdn.net/detail/paschen/8927009
#2
1、头文件声明点击消息处理函数:
afx_msg void OnBnClickedBtnCheck();
2、添加消息映射:
3、消息处理函数实现:
当然,你通过类向导添加BN_CLICKED事件也是可以的
afx_msg void OnBnClickedBtnCheck();
2、添加消息映射:
BEGIN_MESSAGE_MAP(CXXOOdlg, CDialog)
ON_BN_CLICKED(IDC_CHECK1, &CXXOOdlg::OnBnClickedBtnCheck)
//others
END_MESSAGE_MAP()
3、消息处理函数实现:
void CXXOOdlg::OnBnClickedBtnCheck()
{
CButton *pBtn = (CButton *)GetDlgItem(IDC_CHECK1);
if(pBtn && pBtn->GetCheck())
{
int n = m_list.GetTopIndex(); //大概知道问题出在这
int nlast = n + m_list.GetItemCount();
for (;n<nlast;n++)
{
m_list.SetItemState(n,LVIS_SELECTED,LVIS_SELECTED);
}
}
else
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
}
当然,你通过类向导添加BN_CLICKED事件也是可以的
#3
谢谢两位的回答,长知识了,很抱歉我没讲清楚,我的CheckBox控件是在LIst控件外面的,不是在List里面的,而且我运行后发现我勾上Check控件,List是不会全选的,需要点击List的Header才会全选,取消勾选能实现取消全选,但是再点击List控件的Header时,除第一行外又全选了,大家能给啥意见吗
#4
这个跟我的代码实现效果是一样的,还能给点意见吗?
#5
要实现ListCtrl全选
m_List.SetItemState(-1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);就可以了
m_List.SetItemState(-1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);就可以了
#6
现在问题不是在全选啊,是这个点击响应变成了List表格的页眉了,而不是Check控件
感谢回答,还有没有建议的呢
#7
难道要先CheckBox.SetFocus();?
#8
上传截图,希望有人能解惑
#9
Multiple-Selection Operations
SetSel Selects or deselects a list-box item in a multiple-selection list box.
GetCaretIndex Determines the index of the item that has the focus rectangle in a multiple-selection list box.
SetCaretIndex Sets the focus rectangle to the item at the specified index in a multiple-selection list box.
GetSelCount Returns the number of strings currently selected in a multiple-selection list box.
GetSelItems Returns the indices of the strings currently selected in a list box.
SelItemRange Selects or deselects a range of strings in a multiple-selection list box.
SetAnchorIndex Sets the anchor in a multiple-selection list box to begin an extended selection.
GetAnchorIndex Retrieves the zero-based index of the current anchor item in a list box.
SetSel Selects or deselects a list-box item in a multiple-selection list box.
GetCaretIndex Determines the index of the item that has the focus rectangle in a multiple-selection list box.
SetCaretIndex Sets the focus rectangle to the item at the specified index in a multiple-selection list box.
GetSelCount Returns the number of strings currently selected in a multiple-selection list box.
GetSelItems Returns the indices of the strings currently selected in a list box.
SelItemRange Selects or deselects a range of strings in a multiple-selection list box.
SetAnchorIndex Sets the anchor in a multiple-selection list box to begin an extended selection.
GetAnchorIndex Retrieves the zero-based index of the current anchor item in a list box.
#10
CListBox::SetSel
int SetSel( int nIndex, BOOL bSelect = TRUE );
Return Value
LB_ERR if an error occurs.
Parameters
nIndex
Contains the zero-based index of the string to be set. If –1, the selection is added to or removed from all strings, depending on the value of bSelect.
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected. The specified string is selected and highlighted by default.
Remarks
Selects a string in a multiple-selection list box.
Use this member function only with multiple-selection list boxes.
CListBox Overview | Class Members | Hierarchy Chart
See Also CListBox::GetSel,LB_SETSEL
int SetSel( int nIndex, BOOL bSelect = TRUE );
Return Value
LB_ERR if an error occurs.
Parameters
nIndex
Contains the zero-based index of the string to be set. If –1, the selection is added to or removed from all strings, depending on the value of bSelect.
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected. The specified string is selected and highlighted by default.
Remarks
Selects a string in a multiple-selection list box.
Use this member function only with multiple-selection list boxes.
CListBox Overview | Class Members | Hierarchy Chart
See Also CListBox::GetSel,LB_SETSEL
#11
CListBox::SelItemRange
int SelItemRange( BOOL bSelect, int nFirstItem, int nLastItem );
Return Value
LB_ERR if an error occurs.
Parameters
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected.
nFirstItem
Specifies the zero-based index of the first item to set.
nLastItem
Specifies the zero-based index of the last item to set.
Remarks
Selects multiple consecutive items in a multiple-selection list box.
Use this member function only with multiple-selection list boxes. If you need to select only one item in a multiple-selection list box — that is, if nFirstIem is equal to nLastItem — call the SetSel member function instead.
CListBox Overview | Class Members | Hierarchy Chart
See Also LB_SELITEMRANGE, CListBox::GetSelItems
int SelItemRange( BOOL bSelect, int nFirstItem, int nLastItem );
Return Value
LB_ERR if an error occurs.
Parameters
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected.
nFirstItem
Specifies the zero-based index of the first item to set.
nLastItem
Specifies the zero-based index of the last item to set.
Remarks
Selects multiple consecutive items in a multiple-selection list box.
Use this member function only with multiple-selection list boxes. If you need to select only one item in a multiple-selection list box — that is, if nFirstIem is equal to nLastItem — call the SetSel member function instead.
CListBox Overview | Class Members | Hierarchy Chart
See Also LB_SELITEMRANGE, CListBox::GetSelItems
#12
谢谢各位的答复,已经找到方法了,是我对全选理解错了,需要依靠CHECKBOX来实现
#1
得到listctrl中所有行的checkbox的状态
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
得到listctrl中所有行的checkbox的状态
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
判断是否点击在listctrl的checkbox上
添加listctrl控件的NM_CLICK消息相应函数
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//判断是否点在checkbox上
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("点在listctrl的checkbox上");
}
*pResult = 0;
}
如果想在listctrl中插入各种控件,可以用我写这个,附demo: http://download.csdn.net/detail/paschen/8927009
dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
得到listctrl中所有行的checkbox的状态
m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<m_list.GetItemCount(); i++)
{
if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
{
str.Format(_T("第%d行的checkbox为选中状态"), i);
AfxMessageBox(str);
}
}
判断是否点击在listctrl的checkbox上
添加listctrl控件的NM_CLICK消息相应函数
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
m_list.ScreenToClient(&point);
LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;
UINT nFlag;
int nItem = m_list.HitTest(point, &nFlag);
//判断是否点在checkbox上
if(nFlag == LVHT_ONITEMSTATEICON)
{
AfxMessageBox("点在listctrl的checkbox上");
}
*pResult = 0;
}
如果想在listctrl中插入各种控件,可以用我写这个,附demo: http://download.csdn.net/detail/paschen/8927009
#2
1、头文件声明点击消息处理函数:
afx_msg void OnBnClickedBtnCheck();
2、添加消息映射:
3、消息处理函数实现:
当然,你通过类向导添加BN_CLICKED事件也是可以的
afx_msg void OnBnClickedBtnCheck();
2、添加消息映射:
BEGIN_MESSAGE_MAP(CXXOOdlg, CDialog)
ON_BN_CLICKED(IDC_CHECK1, &CXXOOdlg::OnBnClickedBtnCheck)
//others
END_MESSAGE_MAP()
3、消息处理函数实现:
void CXXOOdlg::OnBnClickedBtnCheck()
{
CButton *pBtn = (CButton *)GetDlgItem(IDC_CHECK1);
if(pBtn && pBtn->GetCheck())
{
int n = m_list.GetTopIndex(); //大概知道问题出在这
int nlast = n + m_list.GetItemCount();
for (;n<nlast;n++)
{
m_list.SetItemState(n,LVIS_SELECTED,LVIS_SELECTED);
}
}
else
m_list.SetItemState(nIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
}
当然,你通过类向导添加BN_CLICKED事件也是可以的
#3
谢谢两位的回答,长知识了,很抱歉我没讲清楚,我的CheckBox控件是在LIst控件外面的,不是在List里面的,而且我运行后发现我勾上Check控件,List是不会全选的,需要点击List的Header才会全选,取消勾选能实现取消全选,但是再点击List控件的Header时,除第一行外又全选了,大家能给啥意见吗
#4
这个跟我的代码实现效果是一样的,还能给点意见吗?
#5
要实现ListCtrl全选
m_List.SetItemState(-1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);就可以了
m_List.SetItemState(-1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);就可以了
#6
现在问题不是在全选啊,是这个点击响应变成了List表格的页眉了,而不是Check控件
感谢回答,还有没有建议的呢
#7
难道要先CheckBox.SetFocus();?
#8
上传截图,希望有人能解惑
#9
Multiple-Selection Operations
SetSel Selects or deselects a list-box item in a multiple-selection list box.
GetCaretIndex Determines the index of the item that has the focus rectangle in a multiple-selection list box.
SetCaretIndex Sets the focus rectangle to the item at the specified index in a multiple-selection list box.
GetSelCount Returns the number of strings currently selected in a multiple-selection list box.
GetSelItems Returns the indices of the strings currently selected in a list box.
SelItemRange Selects or deselects a range of strings in a multiple-selection list box.
SetAnchorIndex Sets the anchor in a multiple-selection list box to begin an extended selection.
GetAnchorIndex Retrieves the zero-based index of the current anchor item in a list box.
SetSel Selects or deselects a list-box item in a multiple-selection list box.
GetCaretIndex Determines the index of the item that has the focus rectangle in a multiple-selection list box.
SetCaretIndex Sets the focus rectangle to the item at the specified index in a multiple-selection list box.
GetSelCount Returns the number of strings currently selected in a multiple-selection list box.
GetSelItems Returns the indices of the strings currently selected in a list box.
SelItemRange Selects or deselects a range of strings in a multiple-selection list box.
SetAnchorIndex Sets the anchor in a multiple-selection list box to begin an extended selection.
GetAnchorIndex Retrieves the zero-based index of the current anchor item in a list box.
#10
CListBox::SetSel
int SetSel( int nIndex, BOOL bSelect = TRUE );
Return Value
LB_ERR if an error occurs.
Parameters
nIndex
Contains the zero-based index of the string to be set. If –1, the selection is added to or removed from all strings, depending on the value of bSelect.
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected. The specified string is selected and highlighted by default.
Remarks
Selects a string in a multiple-selection list box.
Use this member function only with multiple-selection list boxes.
CListBox Overview | Class Members | Hierarchy Chart
See Also CListBox::GetSel,LB_SETSEL
int SetSel( int nIndex, BOOL bSelect = TRUE );
Return Value
LB_ERR if an error occurs.
Parameters
nIndex
Contains the zero-based index of the string to be set. If –1, the selection is added to or removed from all strings, depending on the value of bSelect.
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected. The specified string is selected and highlighted by default.
Remarks
Selects a string in a multiple-selection list box.
Use this member function only with multiple-selection list boxes.
CListBox Overview | Class Members | Hierarchy Chart
See Also CListBox::GetSel,LB_SETSEL
#11
CListBox::SelItemRange
int SelItemRange( BOOL bSelect, int nFirstItem, int nLastItem );
Return Value
LB_ERR if an error occurs.
Parameters
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected.
nFirstItem
Specifies the zero-based index of the first item to set.
nLastItem
Specifies the zero-based index of the last item to set.
Remarks
Selects multiple consecutive items in a multiple-selection list box.
Use this member function only with multiple-selection list boxes. If you need to select only one item in a multiple-selection list box — that is, if nFirstIem is equal to nLastItem — call the SetSel member function instead.
CListBox Overview | Class Members | Hierarchy Chart
See Also LB_SELITEMRANGE, CListBox::GetSelItems
int SelItemRange( BOOL bSelect, int nFirstItem, int nLastItem );
Return Value
LB_ERR if an error occurs.
Parameters
bSelect
Specifies how to set the selection. If bSelect is TRUE, the string is selected and highlighted; if FALSE, the highlight is removed and the string is no longer selected.
nFirstItem
Specifies the zero-based index of the first item to set.
nLastItem
Specifies the zero-based index of the last item to set.
Remarks
Selects multiple consecutive items in a multiple-selection list box.
Use this member function only with multiple-selection list boxes. If you need to select only one item in a multiple-selection list box — that is, if nFirstIem is equal to nLastItem — call the SetSel member function instead.
CListBox Overview | Class Members | Hierarchy Chart
See Also LB_SELITEMRANGE, CListBox::GetSelItems
#12
谢谢各位的答复,已经找到方法了,是我对全选理解错了,需要依靠CHECKBOX来实现