1. CCustomDraw和LVS_OWNERDRAWFIXED
我发现我去掉了LVS_OWNERDRAWFIXED风格,继承了CCustomDraw的CListView自绘就OK了。
2. CCustomDraw<T>
CCustomDraw<T> 利用静多态,给派生类一个可以定制自绘Options的策略。
// custom draw return flags
// values under 0x00010000 are reserved for global custom draw values.
// above that are for specific controls
#define CDRF_DODEFAULT 0x00000000
#define CDRF_NEWFONT 0x00000002
#define CDRF_SKIPDEFAULT 0x00000004
#define CDRF_DOERASE 0x00000008 // draw the background
#define CDRF_SKIPPOSTPAINT 0x00000100 // don't draw the focus rect
#define CDRF_NOTIFYPOSTPAINT 0x00000010
#define CDRF_NOTIFYITEMDRAW 0x00000020
#if (_WIN32_IE >= 0x0400)
#define CDRF_NOTIFYSUBITEMDRAW 0x00000020 // flags are the same, we can distinguish by context
#endif
#define CDRF_NOTIFYPOSTERASE 0x00000040
// values under 0x00010000 are reserved for global custom draw values.
// above that are for specific controls
#define CDRF_DODEFAULT 0x00000000
#define CDRF_NEWFONT 0x00000002
#define CDRF_SKIPDEFAULT 0x00000004
#define CDRF_DOERASE 0x00000008 // draw the background
#define CDRF_SKIPPOSTPAINT 0x00000100 // don't draw the focus rect
#define CDRF_NOTIFYPOSTPAINT 0x00000010
#define CDRF_NOTIFYITEMDRAW 0x00000020
#if (_WIN32_IE >= 0x0400)
#define CDRF_NOTIFYSUBITEMDRAW 0x00000020 // flags are the same, we can distinguish by context
#endif
#define CDRF_NOTIFYPOSTERASE 0x00000040
3. 如何设置不同的Row Height?
LRESULT OnMeasureItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LPMEASUREITEMSTRUCT p = (LPMEASUREITEMSTRUCT)lParam;
return 0;
}
{
LPMEASUREITEMSTRUCT p = (LPMEASUREITEMSTRUCT)lParam;
return 0;
}
这是第一种吧,需要设置LVS_OWNERDRAWFIXED风格属性。但是这样就和我的CCustomDraw派生冲突了。
第二种是:
void SetEntryHeight(int nEntryHeight)
{
CImageList imageList;
imageList.Create(1, nEntryHeight - 1, ILC_COLOR, 1, 1);
CListViewCtrl::SetImageList(imageList.m_hImageList, LVSIL_SMALL);
imageList.Destroy();
{
CImageList imageList;
imageList.Create(1, nEntryHeight - 1, ILC_COLOR, 1, 1);
CListViewCtrl::SetImageList(imageList.m_hImageList, LVSIL_SMALL);
imageList.Destroy();
}
但是!我马上Google了很多文章,最后的结论是,OwnerDraw的CListCtrl才可以通过第一种方式设置不同的行高,而CustomDraw的不行。我想交替地Add/Remove
LVS_OWNERDRAWFIXED 风格,但是另外一篇BLOG说:
设置LVS_OWNERDRAWFIXED风格需要在Create或者PreSubclassWindow函数中进行,否则MeasureItem不会被调用。
看,这个世界总是有很多不如意的地方!
看来,解决的方案只能是完全Paint,既是使用LVS_OWNERDRAWFIXED,而不是
CCustomDraw。然后用MeasureItem来设置不同行的高度。