OnCtrlColor Not Working?
I used the following overloaded method to change the text color to red in a listbox, in a Visual C++ MFC dialog based application. When I build the program in DEBUG mode, it works perfectly. But when I use the RELEASE mode the text color doesn't change. Why is this and how can I overcome this problem??
Thanks!!
HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if(nCtlColor == CTLCOLOR_LISTBOX)
{
if(bChangeTextColor)
{
pDC-开发者_运维技巧>SetTextColor(RGB(255, 0, 0));
return m_hRedBrush;
}
}
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
You must declare in MESSAGE_MAP
ex:
BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_CTLCOLOR() // your handle
END_MESSAGE_MAP()
Can you try to call the base implementation CDialog::OnCtlColor
before your custom code, like this:
HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hBrush=CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_LISTBOX)
{
if(bChangeTextColor)
{
pDC->SetTextColor(RGB(255, 0, 0));
hBrush=m_hRedBrush;
}
}
return hBrush;
}
CDialog::OnCtlColor
does some stuff internally which is skipped by your return
inside of your function. It's only a very vague idea but I have always used OnCtlColor
this way and never had a problem.
I have to use both previous answers to make it work.
I declared handle in MESSAGE_MAP
BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog) ON_WM_CTLCOLOR() // your handle END_MESSAGE_MAP()
I used
CDialog::OnCtlColor
before my custom code:HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hBrush=CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(nCtlColor == CTLCOLOR_LISTBOX) { if(bChangeTextColor) { pDC->SetTextColor(RGB(255, 0, 0)); hBrush=m_hRedBrush; } } return hBrush; }
精彩评论