启用CListCtrl选取框选择

时间:2018-04-21 11:55:14

标签: windows winapi mfc clistctrl

我在报告模式下使用MFC CListCtrl并通过

启用全行选择和alpha混合选取框选择
const DWORD extendedStyle = m_wndList.GetExtendedStyle();
m_wndList.SetExtendedStyle(extendedStyle | LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT);

选择框选择,即在按住鼠标左键的情况下跨多个项目选择矩形,如果我在CListCtrl窗口的未被项目占用的区域中开始选择,则可以正常工作。

但是,如果我单击某个项目并想从那里开始选择框,则不会创建选择矩形。这与例如Windows资源管理器形成对比,在该资源管理器中,如果从所选项目启动,则选取框选择也有效。

作为MCVE,我在Visual Studio 2017上使用了“新建项目向导”并创建了基于对话框的MFC应用程序,并在对话框资源上放置了“列表控件”。此列表控件通过DDX连接到成员变量CListCtrl m_wndList,并获得如上所述的扩展样式。

ListScroll.h

#pragma once

#ifndef __AFXWIN_H__
   #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"

class CListScrollApp : public CWinApp
{
public:
   CListScrollApp();

public:
   virtual BOOL InitInstance();
   virtual int ExitInstance();

   DECLARE_MESSAGE_MAP()
};

extern CListScrollApp theApp;

ListScroll.cpp

#include "stdafx.h"
#include "ListScroll.h"
#include "ListScrollDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

BEGIN_MESSAGE_MAP(CListScrollApp, CWinApp)
END_MESSAGE_MAP()

CListScrollApp::CListScrollApp() {}

CListScrollApp theApp;

BOOL CListScrollApp::InitInstance() {
   CWinApp::InitInstance();

   CListScrollDlg dlg;
   m_pMainWnd = &dlg;
   INT_PTR nResponse = dlg.DoModal();
   if (nResponse == IDOK) {
      // Do nothing
   } else if (nResponse == IDCANCEL) {
      // Do nothing
   } else if (nResponse == -1) {
      TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
   }

   return FALSE;
}

int CListScrollApp::ExitInstance() {
   return CWinApp::ExitInstance();
}

ListScrollDlg.h

#pragma once
#include "afxcmn.h"

class CListScrollDlg : public CDialog
{
public:
   CListScrollDlg(CWnd* pParent = NULL);

#ifdef AFX_DESIGN_TIME
   enum { IDD = IDD_LISTSCROLL_DIALOG };
#endif

protected:
   virtual void DoDataExchange(CDataExchange* pDX);
   virtual BOOL OnInitDialog();
   DECLARE_MESSAGE_MAP()

private:
   CListCtrl m_wndList;
};

ListScrollDlg.cpp

#include "stdafx.h"
#include "ListScroll.h"
#include "ListScrollDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CListScrollDlg::CListScrollDlg(CWnd* pParent /*=NULL*/)
   : CDialog(IDD_LISTSCROLL_DIALOG, pParent)
{}

void CListScrollDlg::DoDataExchange(CDataExchange* pDX) {
   CDialog::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_LIST1, m_wndList);
}

BEGIN_MESSAGE_MAP(CListScrollDlg, CDialog)
END_MESSAGE_MAP()

BOOL CListScrollDlg::OnInitDialog() {
   CDialog::OnInitDialog();

   const DWORD extendedStyle = m_wndList.GetExtendedStyle();
   m_wndList.SetExtendedStyle(extendedStyle | LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT);

   m_wndList.InsertColumn(1, _T("Test 1"), 0, 200);
   m_wndList.InsertColumn(1, _T("Test 2"), 0, 800);

   m_wndList.InsertItem(1, _T("Item 1"), 0);
   m_wndList.InsertItem(1, _T("Item 2"), 0);

   return TRUE;
}

ListScroll.rc

IDD_LISTSCROLL_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "ListScroll"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,209,179,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,263,179,50,14
    CTEXT           "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
    CONTROL         "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,7,7,306,162
END

有人能指出我正确的方向并解释,如何启用从列表控件中的单个项目开始的选取框选择?

编辑 :以下是Windows 10中Windows资源管理器选择选框的示例

Example of marquee selection from Windows Explorer

0 个答案:

没有答案