Solved

Another SetTextColor question

Posted on 2004-09-09
9
726 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 47
Strange date stored 22 100
how do i create updater to My Activex application? 3 74
zeroMAx challenge 20 76
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now