本文主要实现了在list control 上进行框选,获得框选区域的左上角及右下角位置坐标。
1、添加 List Control 控件ID设为IDC_Listcontrol
2、给控件添加变量 m_list
3、在头文件中定义: //框选
int RClickRow_Top;
int RClickCol_Left;
int RClickRow_Bottom;
int RClickCol_Right;
void Box_select();
4、为控件IDC_Listcontrol添加事件处理函数:OnLvnBegindragTraylist
void CDTHSDemoDlg::OnLvnBegindragTraylist(NMHDR *pNMHDR, LRESULT *pResult) //当控件内左键拖拽时触发
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
GetCursorPos(&point);
//将屏幕坐标转到客户区坐标
m_TrayList.ScreenToClient(&point);
LVHITTESTINFO info;
info.pt = point;
info.flags = LVHT_ABOVE;
int Itsub = m_TrayList.SubItemHitTest(&info);
RClickRow_Top = info.iItem+1;//行号从0开始
RClickCol_Left= info.iSubItem+1;//列号从0开始
itoa(RClickRow_Top, section, 10);
SetDlgItemText(IDC_EDIT_ROWNUMBER,section );
itoa(RClickCol_Left, section, 10);
SetDlgItemText(IDC_EDITCOLUMN,section );
CWnd *pWnd = GetDlgItem(IDC_TRAYLIST); //禁用list control以响应OnLButtonUp()函数
pWnd->EnableWindow(FALSE);
*pResult = 0;
}
5、为弹出框添加事件处理函数:OnLButtonUp//鼠标左键抬起即触发
void CDTHSDemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CWnd *pWnd = GetDlgItem(IDC_TRAYLIST); //解除list control的禁用
pWnd->EnableWindow(TRUE);
DWORD dwPos = GetMessagePos();
GetCursorPos(&point);
//将屏幕坐标转到客户区坐标
m_TrayList.ScreenToClient(&point);
LVHITTESTINFO info;
info.pt = point;
info.flags = LVHT_ABOVE;
int Itsub = m_TrayList.SubItemHitTest(&info);
RClickRow_Bottom = info.iItem+1;//行号从0开始
RClickCol_Right = info.iSubItem+1;//列号从0开始
Box_select();
CDialog::OnLButtonUp(nFlags, point);
}
6、自定义函数Box_select()
void CDTHSDemoDlg::Box_select()
{
int temp=0;
if(RClickRow_Top>RClickRow_Bottom) //若用户从右下角开始框选
{
temp=RClickRow_Bottom;
RClickRow_Bottom=RClickRow_Top;
RClickRow_Top=temp;
}
if(RClickCol_Left>RClickCol_Right)
{
temp=RClickCol_Right;
RClickCol_Right=RClickCol_Left;
RClickCol_Left=temp;
}
for(int i=RClickRow_Top-1;i<RClickRow_Bottom;i++)
{
for(int j=RClickCol_Left-1;j<RClickCol_Right;j++)
{
//自定义函数体,根据获得的框选区域的左上和右下角坐标进行遍历处理
}
}
}
7、效果:可实现左键框选功能文章来源:https://www.toymoban.com/news/detail-443414.html
文章来源地址https://www.toymoban.com/news/detail-443414.html
到了这里,关于MFC List Control 上实现左键框选的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!