重绘CListBox问题
因为需要给CListBox每项增加图片, 所以派生了CNewListBox,并重载了DrawItem函数
在DrawItem中加入代码绝对没问题. 我在主窗口拉一个ListBox控件,并增加CNewListBox变量. 问题出现了.
如果我把属性中 "所有者绘制 "改成固定后, 运行了需要在listBox控件上点击一下才执行DrawItem内容. 为什么啊? 怎么解决?
而把属性中 "所有者绘制 "改成可变后, 直接不管你怎么样.都不会绘制. 为什么
[解决办法]
属性中 "所有者绘制 "改成可变后。。
我的英文版中该属性值为 NO FIXED VERABLE
估计你选了NO 就是说不采用用户自定义的绘图方案
DrawItem方法是没有问题的 建议你把相关代码贴出来
以下是我的代码 当窗体出现的时候DrawItem就已经调用了 给你做个参考
void CMyList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct-> CtlType == ODT_LISTBOX);
CString str;
LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct-> itemData;
str = lpszText;
ASSERT(lpszText != NULL);
CDC dc;
dc.Attach(lpDrawItemStruct-> hDC);
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct-> itemAction | ODA_SELECT) &&
(lpDrawItemStruct-> itemState & ODS_SELECTED))
{
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&lpDrawItemStruct-> rcItem,
::GetSysColor(COLOR_HIGHLIGHT));
}
else
dc.FillSolidRect(&lpDrawItemStruct-> rcItem, crOldBkColor);
// If this item has the focus, draw a red frame around the
// item 's rect.
if ((lpDrawItemStruct-> itemAction | ODA_FOCUS) &&
(lpDrawItemStruct-> itemState & ODS_FOCUS))
{
CBrush br(RGB(255, 0, 0));
dc.FrameRect(&lpDrawItemStruct-> rcItem, &br);
}
str+= "haha ";
// Draw the text.
dc.DrawText(
str,
strlen(str),
&lpDrawItemStruct-> rcItem,
DT_CENTER|DT_SINGLELINE|DT_VCENTER);
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);
dc.Detach();
}
在Dialog中的OnInitDialog方法中:
RECT rect;
rect.top=10;
rect.bottom=200;
rect.left=10;
rect.right=200;
p=new CMyList;
p-> Create(
WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL|
LBS_SORT|LBS_OWNERDRAWVARIABLE,
rect, this, 1);
p-> InsertString(0, "item1 ");
p-> InsertString(1, "item2 ");