suprit
suprit

Reputation: 89

CCombobox : how to set text color of Editable area text when combobox style is dropdown in win32

I am using CComboBox with style DropDown wherein user can enter data in edit area in case expected option is not available in the drop down options. I am trying to set color of text present in Editable area using OnCtlColor but it sets color to only drop down items inserted and not the editable area.

HBRUSH CUserInfoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    int iCtrlID;

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    iCtrlID = pWnd->GetDlgCtrlID();

    if (CTLCOLOR_STATIC == nCtlColor &&
        (IDC_CMB_CITY == iCtrlID)
        )
    {
        pDC->SetTextColor(RGB(255, 0, 0));
        pDC->SetBkMode(TRANSPARENT);
        hbr = (HBRUSH) GetStockObject(WHITE_BRUSH);
    }

    if (CTLCOLOR_EDIT == nCtlColor &&
        (IDC_CMB_CITY == iCtrlID)
        )
    {
        pDC->SetTextColor(RGB(255, 0, 0));
        pDC->SetBkMode(TRANSPARENT);
    }
 return hbr;
}
 

where IDC_CMB_CITY is resource ID of CComboBox control.

Upvotes: 2

Views: 495

Answers (1)

suprit
suprit

Reputation: 89

Found the answer with help of comments:

HBRUSH CUserInfoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    TCHAR szText[MAX_PATH];
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    if (pWnd->m_hWnd == m_CityComboBoxInfo.hwndItem)
    {
        pDC->SetTextColor(RGB(255, 0, 0));
        pDC->SetBkMode(TRANSPARENT);
        hbr = (HBRUSH) GetStockObject(WHITE_BRUSH);

        int iSel = m_Cmb_City.GetCurSel();
        if (CB_ERR != iSel)
        {
            m_Cmb_City.GetLBText(iSel, szText);
            if (0 != _tcsicmp(szText, L"some_default_text_initially_shown_on_Dropdown"))
            {
                pDC->SetTextColor(RGB(0, 0, 0));
            }
        }
   }
}

where CComboBox m_Cmb_City; and m_CityComboBoxInfo is fetched using m_Cmb_City.GetComboBoxInfo(&m_CityComboBoxInfo); Above piece of code sets text color as initially red. When user makes selection from menu it changes text color to black

Upvotes: 4

Related Questions