2

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
suprit
  • 79
  • 7
  • The combo controls forwards `WM_CTLCOLOREDIT`to its parent but it doesn't adjust the parameters, so the ID won't be the same as the ID of the combo itself (I think the edit control will have ID 1001, but rather than relying on that you should probably check the ID of the its parent). – Jonathan Potter Apr 05 '21 at 00:00
  • 1
    The drop-down list does not seem to have an edit control, you can use [`GetComboBoxInfo`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcomboboxinfo) to get its child windows. Refer to : [How do I select item based on typed letters on owner-draw combobox?](https://stackoverflow.com/questions/66594512/how-do-i-select-item-based-on-typed-letters-on-owner-draw-combobox/66594937#66594937) – Zeus Apr 05 '21 at 01:53
  • 2
    `GetComboBoxInfo` will return `hwndCombo` and `hwndList` when processing the drop-down list. The former should be the handle of the control you mentioned, and the latter is the handle of the drop-down list. You can try to deal with `hwndCombo`. – Zeus Apr 05 '21 at 06:28

1 Answers1

3

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

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
suprit
  • 79
  • 7
  • 1
    I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Zeus Apr 05 '21 at 07:10