使用WTL,继承COwnerDraw实现自绘CListBox,现在碰到一个问题:向列表框添加三行字符串,结果只显示第一条字段串内容;当用鼠标点击列表框时显示出当前焦点行字符串,但其他内容又显示不出来了,不知道是哪里出了问题。
列表框的风格为:LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP以下是部分代码:
class CSlideListBox : public CWindowImpl<CSlideListBox, CListBox>,
public COwnerDraw< CSlideListBox >
{
void DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
if ((int)lpDIS->itemID < 0)
return; CDC dc(lpDIS->hDC); CBrush brush;
brush.CreateSolidBrush(lpDIS->itemState & ODS_SELECTED ? RGB(236,244,251) : RGB(255,255,0));
dc.FillRect(&lpDIS->rcItem, brush);
brush.DeleteObject(); // Set the background mode to TRANSPARENT to draw the text.
int nBkMode = dc.SetBkMode(TRANSPARENT); COLORREF crText = dc.SetTextColor(lpDIS->itemState & ODS_SELECTED ? RGB(27,44,150) : RGB(0, 0, 0)); // Get and display item text.
CString sText;
GetText(lpDIS->itemID, sText); // draw the text
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
dc.DrawText(sText, -1, &lpDIS->rcItem, nFormat); // restore the device context
dc.SetTextColor(crText);
dc.SetBkMode(nBkMode);
} void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 20;
lpMeasureItemStruct->itemWidth = 100;
}
}
列表框的风格为:LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP以下是部分代码:
class CSlideListBox : public CWindowImpl<CSlideListBox, CListBox>,
public COwnerDraw< CSlideListBox >
{
void DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
if ((int)lpDIS->itemID < 0)
return; CDC dc(lpDIS->hDC); CBrush brush;
brush.CreateSolidBrush(lpDIS->itemState & ODS_SELECTED ? RGB(236,244,251) : RGB(255,255,0));
dc.FillRect(&lpDIS->rcItem, brush);
brush.DeleteObject(); // Set the background mode to TRANSPARENT to draw the text.
int nBkMode = dc.SetBkMode(TRANSPARENT); COLORREF crText = dc.SetTextColor(lpDIS->itemState & ODS_SELECTED ? RGB(27,44,150) : RGB(0, 0, 0)); // Get and display item text.
CString sText;
GetText(lpDIS->itemID, sText); // draw the text
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
dc.DrawText(sText, -1, &lpDIS->rcItem, nFormat); // restore the device context
dc.SetTextColor(crText);
dc.SetBkMode(nBkMode);
} void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 20;
lpMeasureItemStruct->itemWidth = 100;
}
}
把 if ((int)lpDIS->itemID < 0)
return;
改成 if ((int)lpDIS->itemID < 1)
return;则初始显示第二条行字符串,感觉就像是之后的绘图被覆盖了一样。
http://www.codeproject.com/KB/wtl/customdrawlist_wtl.aspx