Solved

Another SetTextColor question

Posted on 2004-09-09
9
728 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 56
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 99
wordcount challenge 11 106
Path of Workbook 3 59
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Send secure, cloud-based, encrypted alerts and maintain HIPAA compliant messaging. Integrates priority & secure messaging into one application. Ensures IT, emergency respondents and healthcare professionals that their critical messages are never mis…

919 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

16 Experts available now in Live!

Get 1:1 Help Now