irisz
asked on
set a color of a push button
Hi,
I tried to use CtlColor event on a class derived from CButton, but it didnt help:
HBRUSH CMyButton::CtlColor(CDC* pDC, UINT nCtlColor)
{
COLORREF aColor = RGB(255,0,0);
pDC->SetTextColor(aColor);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}
can someone help ?
Iris.
I tried to use CtlColor event on a class derived from CButton, but it didnt help:
HBRUSH CMyButton::CtlColor(CDC* pDC, UINT nCtlColor)
{
COLORREF aColor = RGB(255,0,0);
pDC->SetTextColor(aColor);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}
can someone help ?
Iris.
instead of CTLCOLOR_BUTTON it should be CTLCOLOR_BTN
ASKER
I tried your way but I still get black text...
Besides, I would prefer to color the text in the responsibility of the button and not the dialog.
Iris.
Besides, I would prefer to color the text in the responsibility of the button and not the dialog.
Iris.
Hi irisz,
This may be a little overkill for what you are wanting to achieve. The benefits of this method are that you can at anytime totally change the appearance of your buttons by modifying a few lines of code. What I'm talking about is using an ownerdrawn button. Please try the code I've provided below. Note the areas that I have commented that will change the text color, and the background color of your CButton derived buttons. First you must override "DrawItem" from within your derived button class.
Make certain to choose from within your resource editor that the button<s> you wish to use this method will have the OwnerDraw property selected.
Paste the following code into your "DrawItem" method.
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItem Struct->hD C);
CPen *pOldPen;
BOOL bIsPressed = (lpDrawItemStruct->itemSta te &
ODS_SELECTED);
BOOL bIsFocused = (lpDrawItemStruct->itemSta te &
ODS_FOCUS);
BOOL bIsDisabled = (lpDrawItemStruct->itemSta te &
ODS_DISABLED);
CRect itemRect = lpDrawItemStruct->rcItem;
pDC->SetBkMode(TRANSPARENT );
//Prepare the button to be drawn upon. (This is
//required to get the proper effect)
CBrush br(RGB(255, 255, 255));
pDC->FillRect(&itemRect, &br);
//Draw pressed button
if(bIsPressed)
{
//draw the 3D shadow around the button frame
CBrush brBtnShadow(GetSysColor(CO LOR_BTNSHA DOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
else //draw non pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor
(COLOR_BTNHILIGHT)); //White
CPen pen3DLight(PS_SOLID, 0, GetSysColor
(COLOR_3DDKSHADOW)); //Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor
(COLOR_BTNSHADOW)); //Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor
(COLOR_3DDKSHADOW)); //Black
//White line
pOldPen = pDC->SelectObject(&penBtnH iLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right , itemRect.top);
//Light gray line
pDC->SelectObject(pen3DLig ht);
pDC->MoveTo(itemRect.left+ 1, itemRect.bottom-1);
pDC->LineTo(itemRect.left+ 1, itemRect.top+1);
pDC->LineTo(itemRect.right , itemRect.top+1);
//Black line
pDC->SelectObject(pen3DDKS hadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right -1, itemRect.bottom-1);
pDC->LineTo(itemRect.right -1, itemRect.top-1);
//Dark gray line
pDC->SelectObject(penBtnSh adow);
pDC->MoveTo(itemRect.left+ 1, itemRect.bottom-2);
pDC->LineTo(itemRect.right -2, itemRect.bottom-2);
pDC->LineTo(itemRect.right -2, itemRect.top);
pDC->SelectObject(pOldPen) ;
}
//Get the buttons title
CString sTitle;
GetWindowText(sTitle);
CRect captionRect = lpDrawItemStruct->rcItem;
//Set the button text
if(sTitle.IsEmpty() == FALSE)
{
if(bIsPressed)
captionRect.OffsetRect(1, 1);
//Set text color to BLUE and Background to WHITE
pDC->SetTextColor(RGB(0, 0, 255));
pDC->SetBkColor(RGB(255, 255, 255));
//Center text on button
CRect centerRect = captionRect;
pDC->DrawText(sTitle, -1, captionRect,
DT_SINGLELINE | DT_CALCRECT);
captionRect.OffsetRect((ce nterRect.W idth() -
captionRect.Width())/2,
(centerRect.Height() -
captionRect.Height())/2);
pDC->SetBkMode(TRANSPARENT );
pDC->DrawState(captionRect .TopLeft() ,
captionRect.Size(), (LPCTSTR)
sTitle, (bIsDisabled ?
DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
}
}
Hopes this helps in your Button color quest...
Have fun!
Newton1
This may be a little overkill for what you are wanting to achieve. The benefits of this method are that you can at anytime totally change the appearance of your buttons by modifying a few lines of code. What I'm talking about is using an ownerdrawn button. Please try the code I've provided below. Note the areas that I have commented that will change the text color, and the background color of your CButton derived buttons. First you must override "DrawItem" from within your derived button class.
Make certain to choose from within your resource editor that the button<s> you wish to use this method will have the OwnerDraw property selected.
Paste the following code into your "DrawItem" method.
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItem
CPen *pOldPen;
BOOL bIsPressed = (lpDrawItemStruct->itemSta
ODS_SELECTED);
BOOL bIsFocused = (lpDrawItemStruct->itemSta
ODS_FOCUS);
BOOL bIsDisabled = (lpDrawItemStruct->itemSta
ODS_DISABLED);
CRect itemRect = lpDrawItemStruct->rcItem;
pDC->SetBkMode(TRANSPARENT
//Prepare the button to be drawn upon. (This is
//required to get the proper effect)
CBrush br(RGB(255, 255, 255));
pDC->FillRect(&itemRect, &br);
//Draw pressed button
if(bIsPressed)
{
//draw the 3D shadow around the button frame
CBrush brBtnShadow(GetSysColor(CO
pDC->FrameRect(&itemRect, &brBtnShadow);
}
else //draw non pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor
(COLOR_BTNHILIGHT)); //White
CPen pen3DLight(PS_SOLID, 0, GetSysColor
(COLOR_3DDKSHADOW)); //Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor
(COLOR_BTNSHADOW)); //Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor
(COLOR_3DDKSHADOW)); //Black
//White line
pOldPen = pDC->SelectObject(&penBtnH
pDC->MoveTo(itemRect.left,
pDC->LineTo(itemRect.left,
pDC->LineTo(itemRect.right
//Light gray line
pDC->SelectObject(pen3DLig
pDC->MoveTo(itemRect.left+
pDC->LineTo(itemRect.left+
pDC->LineTo(itemRect.right
//Black line
pDC->SelectObject(pen3DDKS
pDC->MoveTo(itemRect.left,
pDC->LineTo(itemRect.right
pDC->LineTo(itemRect.right
//Dark gray line
pDC->SelectObject(penBtnSh
pDC->MoveTo(itemRect.left+
pDC->LineTo(itemRect.right
pDC->LineTo(itemRect.right
pDC->SelectObject(pOldPen)
}
//Get the buttons title
CString sTitle;
GetWindowText(sTitle);
CRect captionRect = lpDrawItemStruct->rcItem;
//Set the button text
if(sTitle.IsEmpty() == FALSE)
{
if(bIsPressed)
captionRect.OffsetRect(1, 1);
//Set text color to BLUE and Background to WHITE
pDC->SetTextColor(RGB(0, 0, 255));
pDC->SetBkColor(RGB(255, 255, 255));
//Center text on button
CRect centerRect = captionRect;
pDC->DrawText(sTitle, -1, captionRect,
DT_SINGLELINE | DT_CALCRECT);
captionRect.OffsetRect((ce
captionRect.Width())/2,
(centerRect.Height() -
captionRect.Height())/2);
pDC->SetBkMode(TRANSPARENT
pDC->DrawState(captionRect
captionRect.Size(), (LPCTSTR)
sTitle, (bIsDisabled ?
DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
}
}
Hopes this helps in your Button color quest...
Have fun!
Newton1
ASKER
Dear newton1,
All I wanted is to change the color of the text of a push button. Without making it owner-drawn and draw the text myself.
Is this to much to ask ??
thank,
Iris.
All I wanted is to change the color of the text of a push button. Without making it owner-drawn and draw the text myself.
Is this to much to ask ??
thank,
Iris.
First CTLCOLOR_BTN is Type of message that You need to catch. Second thing is that You need to return HBRUSH of Your background for Button. You may get HBRUSH value with function GetSysColor().
ASKER
Obviously, I caught the message...
I dont think the returning of the brush should effect the color of the text...NeverTheLess, I tried to return a brush and it did not help.
I dont think the returning of the brush should effect the color of the text...NeverTheLess, I tried to return a brush and it did not help.
In Control constructor call:
m_clrText = RGB( 0xFF, 0, 0 );
m_clrBkgnd = GetSysColor(COLOR_BTNFACE) ;
m_brBkgnd.CreateSolidBrush ( GetSysColor(COLOR_BTNFACE) );
(add m_clrText, m_clrBkgnd and m_brBkgnd as class members).
In OnCtlColor call:
if( pWnd->GetDlgCtrlID() == IDC_MY_BUTTON )
{
pDC->SetTextColor( m_clrText ); // text
pDC->SetBkColor( m_clrBkgnd ); // text bkgnd
return m_brBkgnd; // ctl bkgnd
}
return(NULL);
m_clrText = RGB( 0xFF, 0, 0 );
m_clrBkgnd = GetSysColor(COLOR_BTNFACE)
m_brBkgnd.CreateSolidBrush
(add m_clrText, m_clrBkgnd and m_brBkgnd as class members).
In OnCtlColor call:
if( pWnd->GetDlgCtrlID() == IDC_MY_BUTTON )
{
pDC->SetTextColor( m_clrText ); // text
pDC->SetBkColor( m_clrBkgnd ); // text bkgnd
return m_brBkgnd; // ctl bkgnd
}
return(NULL);
irisz, did u try my suggestion?
i'm using this code every day and it works fine.
i needed to change the text color of a static in order to simulate link to some web site, here's the code:
HBRUSH CContactInfoDlg::OnCtlColo r(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( CTLCOLOR_STATIC == nCtlColor )
{
if ( GetDlgItem( IDC_Z3OFFICIALWEBSITE )->GetSafeHwnd() == pWnd->GetSafeHwnd() )
pDC->SetTextColor( RGB( 0, 0, 255 ) );
}
return hbr;
}
the only things u need 2 change are:
1. CTLCOLOR_STATIC should be CTLCOLOR_BTN //for button ctrl
2. IDC_Z3OFFICIALWEBSITE should be your button resource ID
if it doesn't work something wrong with your program or something....
i'm using this code every day and it works fine.
i needed to change the text color of a static in order to simulate link to some web site, here's the code:
HBRUSH CContactInfoDlg::OnCtlColo
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( CTLCOLOR_STATIC == nCtlColor )
{
if ( GetDlgItem( IDC_Z3OFFICIALWEBSITE )->GetSafeHwnd() == pWnd->GetSafeHwnd() )
pDC->SetTextColor( RGB( 0, 0, 255 ) );
}
return hbr;
}
the only things u need 2 change are:
1. CTLCOLOR_STATIC should be CTLCOLOR_BTN //for button ctrl
2. IDC_Z3OFFICIALWEBSITE should be your button resource ID
if it doesn't work something wrong with your program or something....
i can send u a sample application if u post your e-mail address...
g'day
g'day
ASKER
sedgwick,
here is my mail:
irisz@cimatron.co.il
i would appritiate if you send me a sample code.
Iris.
here is my mail:
irisz@cimatron.co.il
i would appritiate if you send me a sample code.
Iris.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( CTLCOLOR_BUTTON == nCtlColor )
{
if ( GetDlgItem( IDC_MYBUTTON)->GetSafeHwnd
pDC->SetTextColor( RGB( 255, 0, 0 ) );
}
good luck mate