Click to See Complete Forum and Search --> : Dynamically Created CButton


LHoffman
September 13th, 1999, 12:31 PM
I derived a new class from CButton. I also catch the ONPaint() method with in this new class. In another clsss I dynamically created a radio CButton. What I want to do is change the background color of this radio CButton to the system color. Which I can do with the following routine:

Here is how I create the radio button:


OP1CButton *theRadioButton = new OP1CButton;
theRadioButton->Create(theScriptXQuestion->cLabel, WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, theCEditRect, this, NULL);




Here is the ONPaint method:


void OP1CButton::OnPaint()
{
CPaintDC dc(this); // device context for painting

CRect rect;
GetClientRect(rect);

//Get system color.
DWORD systemColor = GetSysColor(COLOR_MENU);

dc.SetBkColor(systemColor);
}




Now what happens is I get a control tat I can't see until I click in the area of the control. Even the text doesn't show up......

What in the heck am I doing wrong?????

Thanks
-LHoffman

martho
September 13th, 1999, 04:10 PM
..could you post the whole OnPaint-function ? Perhaps I can help you.

LHoffman
September 14th, 1999, 06:41 AM
It in the original post....

Thanks
-LHoffman

Thomas Ascher
September 14th, 1999, 07:32 AM
To change the colors of controls use the message WM_CTLCOLOR_REFLECT. If you overwrite OnPaint you must paint the whole control yourself, that's why you can't see it. This sample uses a CEdit but it should work with CButton too:

class CColorEdit : public CEdit
{
// usual stuff here
protected:
//{{AFX_MSG(CColorEdit)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
//{{AFX_MSG_MAP(CColorEdit)
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAPEND_MESSAGE_MAP()

HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor(RGB(255, 255, 255)); // textcolor: white
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(BLACK_BRUSH); // backgroundcolor: black
}

LHoffman
September 14th, 1999, 08:42 AM
Hey Thomas,

I tried what you suggested and it didn't seen to work. I also tried to implement the ON_WM_CTLCOLOR_REFLECT() I get an error at compile time. This code doesn't even seem to be executing when the CButtons are created. Any other suggestion??? I am am ll ears!!

Thanks
-LHoffman

LHoffman
September 14th, 1999, 09:16 AM
Oops.... I got it working using your code example.. But I still need to change the color of the background to the menu system color.

Thanks
-LHoffman

LHoffman
September 14th, 1999, 09:28 AM
Thomas,

Thanks for the help..... I got it working using your suggestions. Then I found GetSysColorBrush() method and passed in the parameter COLOR_MENU. This gave me what I needed... Once again thanks.

-LHoffman

Thomas Ascher
September 14th, 1999, 09:40 AM
Changing the standard behaviour of derived controls is often quite tricky. Always use the reflected messages if you want to do so. I'm happy that i could help you! :-)

LHoffman
September 14th, 1999, 09:45 AM
Thomas,

once again..... thanks.

p.s. you bet I will remember to use the reflected messages for future controls.....

LHoffman
Email: larry@opone.com