Click here to Skip to main content
14,975,219 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a source file called RoundBtn.cpp with its header file RoundBtn.h

these two files are responsable for coloring the entier buttons on my Dialog. What i need to do is to give one of my button a special color. Some of the code for coloring all buttons is given below.

void RoundBtn::DrawItem(LPDRAWITEMSTRUCT lp)
	CRect rc = lp->rcItem;
	CDC dc;
	CBrush br;
	if (is_pressed) // when the button is pressed
		CPen pen(PS_SOLID, 2, RGB(0,0,0));
		CBrush *pBrush=new CBrush(RGB(220,100,220));
		dc.RoundRect(0, 0, rc.Width(), rc.Height(), rc.Height()/1, rc.Height()/1);  // Round the Buttons 
		dc.SetTextColor(RGB(0, 0, 0));

What I have tried:

What sould i change on my main file or what sould i call on my main file to color the button. the button ID Adress is
and its function is

void CVCDlg::Apply1()


Updated 5-Jul-18 21:12pm

1 solution

The color used to construct the *pBrush is the one that needs to be adjusted. You could make a color variable (COLORREF) a member of the RoundBtn class and set it to 220,100,220 in the class constructor. Then add a method to set the color member and you can have each button be a different color if you wanted to.
Baderd94 7-Jul-18 22:31pm
I added this to my RoundBtn.cpp file

void RoundBtn::SetColor(COLORREF text, COLORREF bkgnd)
m_TextColor = text;
m_BkgndColor = bkgnd;

if(m_hWnd != NULL)

then I added this to my RoundBth.h

COLORREF m_TextColor;
COLORREF m_BkgndColor;

and this

void SetColor(COLORREF text, COLORREF bkgnd);

then i added this to my main file


where Loadbt1 is the button that i want to color

it shows no error but it still does not work
Rick York 7-Jul-18 22:55pm
You forgot some changes in DrawItem - instead of using RGB(220,100,220) :

CBrush *pBrush=new CBrush( m_BkgndColor );

and change the SetTextColor call also to :

dc.SetTextColor( m_TextColor );

BTW, white is probably not the best color to use for both the text and the background.
Baderd94 7-Jul-18 23:44pm
Thank you for your quick reply

i have changed it as you mentioned and it made sense to me tbh but it still does not work, it gives uncolored now, any idea because i feel im to close to getting the right method
Baderd94 8-Jul-18 11:34am
Thank you soo much your method worked perfectly, The problem was the function that i needed to put the code in.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900