自绘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项是我选择后恢复正常的字

求解决方法

...全文
234 9 打赏 收藏 转发到动态 举报
写回复
用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模式的缘故。
你刷新的时候如果先将文字的背景刷一下,然后再绘制文字应该效果就可以了~
内容概要:本文档详细介绍了PlatforMax单机版5.0.1-rc6版本的完整安装流程,涵盖硬件、操作系统、硬盘、网络等前置环境要求,并提供了Ubuntu 20.04.3 Desktop与Server两种系统的安装步骤。重点强调系统需全新安装、禁用自动更新、正确设置磁盘分区以充分利用全部空间,以及创建指定用户名“amax”。随后指导用户通过运行离线安装包完成PlatforMax的部署,包括校验、解压、输入安装码、驱动与Docker配置等环节。安装完成后需通过浏览器访问初始化页面完成最终配置。文档还列举了常见问题及应对措施,特别是NVIDIA驱动不兼容时的处理方式,允许用户手动提供驱动或跳过安装以确保主程序顺利部署。; 适合人群:具备Linux系统操作基础,从事AI平台运维、系统集成或技术支持的相关技术人员,尤其适用于负责本地化部署高性能计算平台的工程师。; 使用场景及目标:①为满足AI训练与推理需求的企业级用户提供PlatforMax平台的本地单机部署方案;②指导技术人员完成从系统准备到平台上线的全流程安装,确保环境合规、数据安全和系统稳定运行;③解决新GPU驱动兼容性等问题,保障平台可扩展性和实用性。; 阅读建议:在实际操作前通读全文,重点关注硬件配置、磁盘管理、用户命名规则及驱动处理策略,建议在测试环境中先行演练,避免因误操作导致数据丢失或安装失败。

15,976

社区成员

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

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