[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

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
0
wygar
Asked:
wygar
  • 3
  • 3
  • 2
  • +1
1 Solution
 
DanRollinsCommented:
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
0
 
AndyAinscowCommented:
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.
0
 
AmitAgarwalCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wygarAuthor Commented:
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.
0
 
AndyAinscowCommented:
Did you try my comment with the ID of the combo instead of the nType.

ps - this is the MFC question area
0
 
wygarAuthor Commented:
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.
0
 
AndyAinscowCommented:
OK.  Could you give all the combos the same ID, a different one to the other controls.
0
 
DanRollinsCommented:
I think that the problem is that you are getting the HWND and HDC of the combobox and not the internal edit box that is it's child.

This is utterly trival to handle in MFC, but trickeir to handle in raw Win32 programming.  I think you will need to subclass the combobox and handle WM_CTLCOLOR messages there (they will apply to its child, the edit box)

I have forgotten how to subclass in Win32, but it should not be difficult for someone who is fresh with these techniques.  You can obtain the HWND of the internal edit box by using code like:

      HWND hwndEdit= ::GetWindow( hWnd, GW_CHILD );  // there is only one child

(where hWnd is the hwnd of the combo box).  Then in your subclassing handler, process the WM_COLOREDIT as you are currently doing.

Here is some relevant info to help you on your way:
     How To Subclass CListBox and CEdit Inside of CComboBox
     http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B174667

It is about how to do it with MFC, but the principles are the same.

-- Dan
0
 
DanRollinsCommented:
See also:
    How to subclass combobox in order to change its colors
    http://www.experts-exchange.com/Win_Prog/Q_10002772.html
-- Dan
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now