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

女神打Boss 女神打Boss 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大致代码如下
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项是我选择后恢复正常的字

求解决方法

...全文

相关推荐

MFC控件使用实例

C++ MFC控件使用实例源代码集合,是VC下实现的

liangchuan0919

VS控件介绍.docx

小白学VS SetWindowText:设置控件上要显示的标题文本,即用来设置控件的caption属性 GetWindowText:得到控件的标题文本 EnableWindow:设置控件的Disabled属性,即社会自控件是否可以接收键盘和鼠标的输入 SetWindowPos:改变窗口的大小、位置和Z轴次序。 MoveWindow:改变窗口的大小和位置 GetWindowRec:得到窗口的大小、位置(信息存放在一个矩形结构中)。 GetClientRect:得到窗口客户区的大小(信息存放在一个矩形结构中 ) ShowWindow:设置窗口的可见性(即窗口是否可见) SetWindowText/GetWindowText还可以用来设置/得到对话框的标题文本。

bingshan87

VC通用控件

VC通用控件的ppt,第12周

hrzhenpeng

CWnd-MFC中文手册

CWnd类提供了微软基础类库中所有窗口类的基本功能。 CWnd对象与Windows的窗口不同,但是两者有紧密联系。CWnd对象是由CWnd的构造函数和析构函数创建或销毁的。另一方面,Windows的窗口是Windows的一种内部数据结构,它是由CWnd的Create成员函数创建的,而由CWnd的虚拟析构函数销毁。DestroyWindow函数销毁Windows的窗口,但是不销毁对象。 CWnd类和消息映射机制隐藏了WndProc函数。接收到的Windows通知消息通过消息映射被自动发送到适当的CWnd OnMessage成员函数。你可以在派生类中重载OnMessage成员函数以处理成员的特定消息。 CWnd类同时还使你能够为应用程序创建Windows的子窗口。先从CWnd继承一个类,然后在派生类中加入成员变量以保存与你的应用程序有关的数据。在派生类中实现消息处理成员函数和消息映射,以指定当消息被发送到窗口时应该如何动作。 你可以经过两个步骤来创建一个子窗口。首先,调用构造函数CWnd以创建一个CWnd对象,然后调用Create成员函数以创建子窗口并将它连接到CWnd对象。 当用户关闭你的子窗口时,应销毁CWnd对象,或者调用DestroyWindow成员函数以清除窗口并销毁它的数据结构。 在微软基础类库中,从CWnd派生了许多其它类以提供特定的窗口类型。这些类中有许多,包括CFrameWnd,CMDIFrameWnd,CMDIChildWnd,CView和CDialog,被用来进一步派生。从CWnd派生的控件类,如CButton,可以被直接使用,也可以被进一步派生出其它类来。

我恨天

c++标准控件

几种常用的Windows控件的作用:命令按钮、单选按钮、编辑框、列表框、组合框

maomaochongdu
评论 9
收藏 已收藏
分享
返回顶部
女神打Boss
界面 15,976 成员 115,866 内容