自绘CListBox,使用EnableWindow函数,字有点重影

女神打Boss 2019-05-29 11:22:39
大致代码如下
class CColorListBox : public CListBox
{
DECLARE_DYNAMIC(CColorListBox)



public:
CColorListBox();
virtual ~CColorListBox();

public:
int AddString(LPCTSTR lpszItem);
int AddString(LPCTSTR lpszItem, COLORREF rgb);

public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//cpp
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
if ((int)lpDIS->itemID < 0)
return;

CDC* pDC = CDC::FromHandle(lpDIS->hDC);

COLORREF crText;
CString sText;
COLORREF crNorm = (COLORREF)lpDIS->itemData; // Color information is in item data.
COLORREF crHilite = RGB(255 - GetRValue(crNorm), 255 - GetGValue(crNorm), 255 - GetBValue(crNorm));

// If item has been selected, draw the highlight rectangle using the item's color.
if ((lpDIS->itemState & ODS_SELECTED) &&
(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush(RGB(51, 153, 255));
pDC->FillRect(&lpDIS->rcItem, &brush);
}

// If item has been deselected, draw the rectangle using the window color.
if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
{
CBrush brush(::GetSysColor(COLOR_WINDOW));
pDC->FillRect(&lpDIS->rcItem, &brush);
}

// If item has focus, draw the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);

// If item does not have focus, redraw (erase) the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);


// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);

// If the item's color information is set, use the highlight color
// gray text color, or normal color for the text.
if (lpDIS->itemData)
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(crHilite);
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(crNorm);
}
// Else the item's color information is not set, so use the
// system colors for the text.
else
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}


// Get and display item text.
GetText(lpDIS->itemID, sText);
CRect rect = lpDIS->rcItem;

// Setup the text format.
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;

// Calculate the rectangle size before drawing the text.
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);

pDC->SetTextColor(crText);
pDC->SetBkMode(nBkMode);
}

//-------------------------------------------------------------------
void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
}

//-------------------------------------------------------------------
int CColorListBox::AddString(LPCTSTR lpszItem)
{
return ((CListBox*)this)->AddString(lpszItem);
}

//-------------------------------------------------------------------
int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
{
int nItem = AddString(lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
}
//调用
void CCListBoxTestDlg::OnBnClickedButton1()
{
m_colorListBox.AddString("111000", RGB(0, 0, 0));
m_colorListBox.AddString("111000", RGB(128, 128, 128));
}


void CCListBoxTestDlg::OnBnClickedCheck1()
{
// TODO: 在此添加控件通知处理程序代码
my_bCheck = !my_bCheck;
m_colorListBox.EnableWindow(my_bCheck);
}

效果如图

当调用EnableWindow函数时字会出现重影现象,前面3项是我选择后恢复正常的字

求解决方法

...全文
187 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
女神打Boss 2019-05-29
  • 打赏
  • 举报
回复
先画背景后写字就OK了
改成这样

// Calculate the rectangle size before drawing the text.
pDC->SetBkMode(nBkMode);
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);
pDC->SetTextColor(crText);
女神打Boss 2019-05-29
  • 打赏
  • 举报
回复
引用 6 楼 schlafenhamster 的回复:
那个 包含 ListBox 的 方框 是 什么 CStatic ?
是group box和check box 组合就这样子了,要设置ctrl + d的顺序就行了
女神打Boss 2019-05-29
  • 打赏
  • 举报
回复
引用 4 楼 Eleven 的回复:
我说的是你代码中写的
// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);
thanks
schlafenhamster 2019-05-29
  • 打赏
  • 举报
回复
那个 包含 ListBox 的 方框 是 什么 CStatic ?
女神打Boss 2019-05-29
  • 打赏
  • 举报
回复
引用 2 楼 schlafenhamster 的回复:
check1 是什么 ?
是一个check box控件,my_bCheck是控件value变量,我点击它调用
void CCListBoxTestDlg::OnBnClickedCheck1()
{
// TODO: 在此添加控件通知处理程序代码
my_bCheck = !my_bCheck;
m_colorListBox.EnableWindow(my_bCheck);
}
就是设置m_colorListBox控件EnableWindow启用或禁用的
Eleven 2019-05-29
  • 打赏
  • 举报
回复
我说的是你代码中写的
// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);
女神打Boss 2019-05-29
  • 打赏
  • 举报
回复
引用 1 楼 Eleven 的回复:
应该是设置了TRANSPARENT模式的缘故。
你刷新的时候如果先将文字的背景刷一下,然后再绘制文字应该效果就可以了~

我 TRANSPARENT属性是false啊
schlafenhamster 2019-05-29
  • 打赏
  • 举报
回复
check1 是什么 ?
Eleven 2019-05-29
  • 打赏
  • 举报
回复
应该是设置了TRANSPARENT模式的缘故。
你刷新的时候如果先将文字的背景刷一下,然后再绘制文字应该效果就可以了~

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧