zoukankan      html  css  js  c++  java
  • 关于重写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 ;
    }

  • 相关阅读:
    【tarjan】BZOJ 1051:受欢迎的牛
    【递推】BZOJ 1088: [SCOI2005]扫雷Mine
    【计算几何】多边形
    【贪心】Bzoj 2457:[BeiJing2011]双端队列
    【单调栈】Bzoj 1012: 最大数maxnumber
    [洛谷P3584] POI2015 LAS
    [洛谷P4049] JSOI2007 合金
    [51nod1533] 一堆的堆
    [AGC018E] Sightseeing Plan
    [CF1065E] Side Transmutations
  • 原文地址:https://www.cnblogs.com/yuzhould/p/4454973.html
Copyright © 2011-2022 走看看