Changing Color of text when color is selected from combo box

185 Views Asked by At

I would like to change the text color of the text present in IDC_EDIT1 box when a new color is selected from the combo box. But it is now working, OnCbnSelchangeComboColorBox :- Event Handler for Combo Box m_Combo_Box :- Variable name for combo box. m_text :- Variable name for IDC_EDIT1.

enter image description here

void CFontFormatter::OnCbnSelchangeComboColorBox()
{
    int comboBoxSelectedItem = m_Combo_Box.GetCurSel();
    if (comboBoxSelectedItem != LB_ERR) {
        CString selectedColorText;
        m_Combo_Box.GetLBText(comboBoxSelectedItem, selectedColorText);
        if (selectedColorText == _T("Red"))
            fontColor = RGB(255, 0, 0);
        else if (selectedColorText == _T("Green"))
            fontColor = RGB(0, 255, 0);
        else if (selectedColorText == _T("Blue"))
            fontColor = RGB(0, 0, 255);
        CWnd* pEditControl = GetDlgItem(IDC_EDIT1);
        if (pEditControl)
        {
            CDC* pDC = m_editText.GetDC();
            pDC->SetTextColor(fontColor);
            pEditControl->Invalidate();
            pEditControl->UpdateWindow();
        }
        m_text.SetString(selectedColorText);
        UpdateData(FALSE);
    }
}

I have Tried through CWnd* and CDC*.

1

There are 1 best solutions below

0
Andrew Truckle On

Here is one resource https://www.equestionanswers.com/vcpp/background-color-edit-static.php). They provide some code:

case WM_CTLCOLOREDIT:
{
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(0,0,255));
    SetBkColor(hdcStatic, RGB(0,230,0));
    return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
}