I am trying to change the color of static text (and also checkbox items) in a dialog window in my MFC application.
Following this (MFC - change text color of a cstatic text control) and similar suggestions, I did the following on ON_WM_CTLCOLOR() message:
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
pDC->SetTextColor(RGB(255, 0, 0));
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
The problem is that this only affects edit text boxes and not static text or checkboxes. those still have black texts.
I also tried to look for sth similar to winapi's WM_CTLCOLORSTATIC message as that worked well in win32 applications but did not find any equivalent in MFC. Any idea how to change the color of the static texts and checkbox text?
This works for me:
Put this in the message map:
ON_WM_CTLCOLOR()
And implement something like this:
HBRUSH CSomeDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = __super::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_SOMESTATIC)
{
// display the static control IDC_SOMESTATIC in red
pDC->SetTextColor(RGB(255, 0, 0));
}
return hbr;
}
When you add a Static Text control from the Toolbox, it will get the ID IDC_STATIC.
You need to rename this ID to something else, and then use OnCtlColor. Assuming you name it IDC_STATIC_1, In OnCtlColor, use:
case IDC_STATIC_1:
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(100,110,120);
Related
I'm trying to find a way to change background color of checkbox inside CListCtrl, please help me to find a working solution. Maybe there is some way to work with CListCtrl subitems inside the OnCtlColor function? I can change the text color and backcolor of CListCtrl, but can't find a way how to change the checkboxes background color...
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( nCtlColor== CTLCOLOR_LISTBOX)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(0,255,0));
hbr = m_brush;
}
return hbr;
}
I want to make edit control's background transparent and change text's color to white. I can do both of them with this function, the only problem is that when you type on edit control, text keeps overlapping, follow the link for an example. I believe the problem is NULL_BRUSH, which makes background transparent, because if I change only the text color it works fine, unfortunately I need both.
How can I do this?
HBRUSH CDlg::OnCtlColor(CDC* pDc, CWnd* pWnd, UINT nCtlColor){
HBRUSH hbr = NULL;
switch (nCtlColor) {
case CTLCOLOR_EDIT:
if (pWnd->GetDlgCtrlID() == editControl){
pDc->SetBkMode(TRANSPARENT);
pDc->SetBkColor(TRANSPARENT);
pDc->SetTextColor(RGB(255, 255, 255));
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
}
break;
default:
hbr = CDialogEx::OnCtlColor(pDc, pWnd, nCtlColor);
}
return hbr;
}
My questions is similar to:
win32 : display editbox with black color in text area on windows mobile 5
However I'm using MFC which doesn't have the same solution available as the one in the above link.
How do I change the background color of the whole background, not just the background behind the text of an edit box?
Below is my code, that only changes the background behind the text, not the whole background of the edit box.
HBRUSH CGadgetStandardDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CStandardDialog::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetBkColor(RGB(255,255,255));
return hbr;
}
In addition to calling SetBkColor you need to return a HBRUSH of the desired background color. So create a brush earlier (say, in the dialog constructor):
m_brBack.CreateSolidBrush(RGB(0, 255, 0));
And then return that brush when called for the control of interest:
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT2)
{
pDC->SetBkColor(RGB(0,255,0));
hbr = m_brBack;
}
Rename your button resource like below.
CButton m_StopButtonto;
to
CMFCButton m_StopButton;
Change some visible features
// Set the background color for the button text.
m_StopButton.SetFaceColor(RGB(255,0,0),true);
m_StopButton.SetTextColor(RGB(0,0,255));
// Set the tooltip of the button.
m_StopButton.SetTooltip(_T("This is my Stop Button!"));
I tried this solution for button and it worked for me. I guess it will work for the other components.
The MFC combobox is really a wierd design.
I use "drop list" type combo box.
HBRUSH CValueInputDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hBrush = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(255, 255, 255));
pDC->SetBkMode(TRANSPARENT);
hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
}
return hBrush;
}
what I do is having all my CStatic and CEdit color WHITE.
but i discover that I also change the combobox's edit to white.
that is what I don't want.
that is what I don't want. and I can't stop it from
pWnd->GetDlgCtrlID() == IDC_COMBO
it is so unfriendly. this combo box.
The Edit box is a child of the combo box. Try this:
pWnd->GetParent()->GetDlgCtrlID() == IDC_COMBO
I have two Cedit and one CCombobox.
you can see the under code, I set the text color to yellow.
then I use a transparent text background, also a transparent background.
It works perfectly fine for Cedit (I don't care the change when I type something in it).
But I notice that the pDC->SetBkMode(TRANSPARENT); do nothing to my CCombobox.
I really don't want to subclass CComboBox to accomplish the transparent background.
or do I need to use drawitem???
HBRUSH CValueInputDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hBrush = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 255, 0));
hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
}
return hBrush;
}
I don't think you're gonna be able to get a good result without subclassing or doing some more work. Transparent controls in MFC are not an easy thing.
Anyway, if you want to get the transparent background on the drop-down list of the combo you need to use CTLCOLOR_LISTBOX instead of CTLCOLOR_EDIT. And CTLCOLOR_BTN for the drow-down button.