CListCtrl重置水平滚动位置

时间:2017-06-12 19:16:09

标签: winapi mfc clistctrl

我在报告模式下遇到MFC CListCtrl问题,无论何时

  • 水平滚动位置不在开头
  • 并按下Shift键

只要通过鼠标左键单击选择了某个项目,水平滚动状态就会恢复到开头。

我正在使用Visual Studio 2015,并且能够使用示例应用程序重现这一点。一个带有CListCtrl,两列和两个项目的简单的基于对话框的MFC应用程序在Windows 7和Windows 10下显示了这种行为。

有没有办法防止这种情况,以便CListCtrl保持滚动位置?

更新

我在下面添加了一个MCVE,在那里我使用VS 2015的新项目向导来创建一个名为ListScroll的基于对话框的MFC应用程序,我在其中静态链接MFC。

我也注意到不需要选择一个项目。即使按下Shift键选择一个项目,也会在我的系统上显示。

Running example program showing the scroll issue

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();

   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

0 个答案:

没有答案