将CListCtrl的列宽固定

#pragma once
#include
"HeaderCtrlEx.h"
// CMyListCtrl

class CMyListCtrl : public CListCtrl
{
    DECLARE_DYNAMIC(CMyListCtrl)

public:
    CMyListCtrl();
   
virtual ~CMyListCtrl();

protected:
    DECLARE_MESSAGE_MAP()
   
virtual void PreSubclassWindow();

public:
    CHeaderCtrlEx m_ctrlHeader;
};
/****************************************CMyListCtrl.h****************************************************/


/****************************************CMyListCtrl.cpp****************************************************/
//子类化CHeaderCtrlEx
void CMyListCtrl::PreSubclassWindow()
{
   
// TODO: Add your specialized code here and/or call the base class
    CListCtrl::PreSubclassWindow();
    m_ctrlHeader.SubclassWindow(::GetDlgItem(m_hWnd,
0));
}
/****************************************CMyListCtrl.cpp****************************************************/


/****************************************CHeaderCtrl.h****************************************************/
#pragma once
// CHeaderCtrlEx

class CHeaderCtrlEx : public CHeaderCtrl
{
    DECLARE_DYNAMIC(CHeaderCtrlEx)

public:
    CHeaderCtrlEx();
   
virtual ~CHeaderCtrlEx();

protected:
    DECLARE_MESSAGE_MAP()
public:
   
virtual BOOL OnChildNotify(UINT msg,WPARAM wp,LPARAM lp,LRESULT* pRes);
};
/****************************************CHeaderCtrl.h****************************************************/

/****************************************CHeaderCtrl.cpp****************************************************/
//固定列宽
BOOL CHeaderCtrlEx::OnChildNotify(UINT msg,WPARAM wp,LPARAM lp,LRESULT* pRes)  
{   
 NMHDR& nmh = *(NMHDR*)lp;
if (nmh.code == HDN_BEGINTRACKA || nmh.code == HDN_BEGINTRACKW) {
if (m_bLocked)
return *pRes=TRUE; // eat message to disallow sizing
}
else if(nmh.code == HDN_DIVIDERDBLCLICKA || nmh.code == HDN_DIVIDERDBLCLICKW)
{
if (m_bLocked)
return *pRes=TRUE; // eat message to disallow sizing
}

   
return   CHeaderCtrl::OnChildNotify(msg,wp,lp,pRes);  
}

 

 

或者用下面的方法:

CHeaderCtrl* pHeaderCtrl = (CHeaderCtrl*)m_ListCtrl.GetHeaderCtrl();  
pHeaderCtrl->EnableWindow( FALSE ) ;

 

你可能感兴趣的:(将CListCtrl的列宽固定)