Link to home
Start Free TrialLog in
Avatar of wygar
wygar

asked on

Another SetTextColor question

I noticed there are several of these but none of them solves my problem.

I am using a combo box and if the user enters some value i want the text to be changed to red.
This is being done in Win32 so here is the code:

            case WM_CTLCOLOREDIT:
                  {
                        HDC hdc = (HDC)wparam;
                        CBaseObject *obj;
                        obj = GetObject( (HWND)lparam );
                        if( obj )
                        {
                              if( obj->m_error )
                              {
                                    ::SetBkMode( hdc, TRANSPARENT );
                                    ::SetTextColor( hdc, RGB(255,0,0) );
                                    return (int)::GetStockObject(WHITE_BRUSH);
                              }
                        }      

                        ::SetBkMode( hdc, TRANSPARENT );      
                        ::SetTextColor( hdc, RGB(0,0,0) );
                        return (int)::GetStockObject(WHITE_BRUSH);
                  }
                  break;

GetObject looks through a list of dynamically created combo boxes and gets the Object associated with it.  It checks if there is an error with the value.

In steping through the code it goes into the portion where it sets the text color to red.
I tried changing the color, then re-setting the text but that didnt work either.

Dustin
Avatar of DanRollins
DanRollins
Flag of United States of America image

The code looks fine (but I did not try it).

If the control is read-only or disabled, the parent will never see the WM_CTLCOLOREDIT message.  You can try looking for the WM_CTLCOLORSTATIC message in that case.

One other thing to try is to call ::InvalidateRect( hwnd, 0, ... )

-- Dan
I assume your combo boxs have unique ID's.
So you could use
      UINT nID = pWnd->GetDlgCtrlID();
and just do your code if the nID is equal to the ID of the control you want to change the colour of.
Avatar of AmitAgarwal
AmitAgarwal

Here is the quick solution.
drive a class from CEdit and override
HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
      
    ::SetBkMode( pDC->m_hDC, TRANSPARENT );
    ::SetTextColor( pDC->m_hDC, RGB(255,0,0) );
     return (HBRUSH)::GetStockObject(WHITE_BRUSH);
}


now you subclass your combo box with this edit control
// create a member in your dlg class
CMyEdit m_edit;
in OnInitDialog

m_edit.SubClassDlgItem (1001, &m_myCombo); // assume you have create a combo box with some resource

this solution is in MFC and I believe you can translate the same in your windows program

amit
Avatar of wygar

ASKER

Well that would work but I am not using MFC so I have no CEdit to derive from.  And my edit boxes change color just fine, it is only the combo box that is not changing right.

I tried the InvalidateRect and it still doesnt display the change.
Did you try my comment with the ID of the combo instead of the nType.

ps - this is the MFC question area
Avatar of wygar

ASKER

Actually they arent unique, I am creating these combo boxes on the fly:
editHandle = CreateWindow( "COMBOBOX", NULL, WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | WS_TABSTOP | WS_VSCROLL,
                      10*(x)+100, 25*(*y)+10, 100, numAdded*25, parent_window, NULL,
                                          (HINSTANCE)GetWindowLongPtr(parent_window, GWL_HINSTANCE), NULL);

I store those up into an array of Handles and then loop over those handles to find the one that is getting painted( seems very inefficient, but i only have a few ).

Yea I know it is the MFC area, I just didnt see a win32 area.  I guess the c++ section would have been a better place for this.
OK.  Could you give all the combos the same ID, a different one to the other controls.
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See also:
    How to subclass combobox in order to change its colors
    https://www.experts-exchange.com/questions/10002772/How-to-subclass-combobox-in-order-to-change-its-colors.html
-- Dan