Solved

Another SetTextColor question

Posted on 2004-09-09
9
735 Views
Last Modified: 2013-11-20
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
Comment
Question by:wygar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 12023121
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12024060
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
 
LVL 4

Expert Comment

by:AmitAgarwal
ID: 12024296
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wygar
ID: 12028666
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12028946
Did you try my comment with the ID of the combo instead of the nType.

ps - this is the MFC question area
0
 

Author Comment

by:wygar
ID: 12029456
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12029571
OK.  Could you give all the combos the same ID, a different one to the other controls.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 125 total points
ID: 12031977
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 12035612
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question