使用下面的类,将需要隐藏的列的列宽设为0,设置m_nHideColumn为需要隐藏的列即可
#pragma once
// CMyListCtrl
class CMyListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CMyListCtrl)
public:
CMyListCtrl();
virtual ~CMyListCtrl();
int m_nHideColumn;
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
};
// MyListCtrl.cpp : 实现文件
//
#include "stdafx.h"
#include "test111.h"
#include "MyListCtrl.h"
// CMyListCtrl
IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)
CMyListCtrl::CMyListCtrl()
{
m_nHideColumn=-1;
}
CMyListCtrl::~CMyListCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
END_MESSAGE_MAP()
// CMyListCtrl 消息处理程序
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// TODO: 在此添加专用代码和/或调用基类
HD_NOTIFY *pHDNotify = (HD_NOTIFY*)lParam;
int m_nMinSize=0;
if( pHDNotify->iItem == m_nHideColumn )
{
switch( pHDNotify->hdr.code )
{
// How to force a minimum column width Cees Mechielsen.
// http://www.codeguru.com/mfc/comments/866.shtml
case HDN_ITEMCHANGINGA:
case HDN_ITEMCHANGINGW:
case HDN_ENDTRACK:
{
if( pHDNotify->pitem->mask & HDI_WIDTH &&
pHDNotify->pitem->cxy < m_nMinSize &&
pHDNotify->pitem->cxy >= 0 )
{
pHDNotify->pitem->cxy = m_nMinSize; // Set the column width
}
}
break;
// Prevent CListCtrl column resizing
case HDN_DIVIDERDBLCLICKA:
case HDN_DIVIDERDBLCLICKW:
case HDN_BEGINTRACKW:
case HDN_BEGINTRACKA:
{
*pResult = TRUE; // disable tracking
return TRUE;
}
}
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}
禁止列宽拖动 原文: http://blog.163.com/pirates_fish/blog/static/1833331502011919425065/