关于重写CListCtrl时候 MeasureItem不被调用的问题

MeasureItem不被调用的这个东西,网上很多朋友都遇到过,在此写出自己的原因。

h
class CListCtrlEx : public CListCtrl
{
 DECLARE_DYNAMIC(CListCtrlEx)
public:
 CListCtrlEx();
 virtual ~CListCtrlEx();
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
 afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
 DECLARE_MESSAGE_MAP()
}

.cpp
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
 ON_WM_MEASUREITEM_REFLECT()    //反射回控件
END_MESSAGE_MAP()

void CListCtrlEx::PreSubclassWindow()
{
 ModifyStyle(0, LVS_OWNERDRAWFIXED);
 CListCtrl::PreSubclassWindow();
}

void CListCtrlEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
               lpMeasureItemStruct->itemHeight = 18;
}

看起来很完美,响应反射消息,而且也设置了LVS_OWNERDRAWFIXED); 我的MeasureItem就是
死活不被调用,看啊,我DrawItem都响应了,为什么,为神马??楼主急了2个多小时,终于
崩溃的发现,原来这个消息还需要被触发的。看下面
void CListCtrlEx::PreSubclassWindow()
{
 ModifyStyle(0, LVS_OWNERDRAWFIXED);
 CListCtrl::PreSubclassWindow();

 CRect rcWin;
 GetWindowRect(&rcWin);
 WINDOWPOS wp;
 wp.hwnd = m_hWnd;
 wp.cx = rcWin.Width();
 wp.cy = rcWin.Height();
 wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
 SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);    //发消息触发MeasureItem。OK
}

当然你也可以这样写:
.h
void SetRowHeight(int   nHeight);

.cpp
void CListCtrl::SetRowHeight(int  nHeight)
{
          m_height = nHeight;
         CRect rcWin;
         GetWindowRect(&rcWin);
         WINDOWPOS wp;
          wp.hwnd = m_hWnd;
         wp.cx = rcWin.Width();
         wp.cy = rcWin.Height();
         wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
         SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}

void CListCtrlEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
               lpMeasureItemStruct->itemHeight = m_height ;
}

原文地址:https://www.cnblogs.com/yuzhould/p/4454973.html