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(WHIT E_BRUSH);
}
}
::SetBkMode( hdc, TRANSPARENT );
::SetTextColor( hdc, RGB(0,0,0) );
return (int)::GetStockObject(WHIT E_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
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(WHIT
}
}
::SetBkMode( hdc, TRANSPARENT );
::SetTextColor( hdc, RGB(0,0,0) );
return (int)::GetStockObject(WHIT
}
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
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.
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.
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(W HITE_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
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(W
}
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
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.
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
ps - this is the MFC question area
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)GetWindowLongPt r(parent_w indow, 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.
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)GetWindowLongPt
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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