Dynamically Created CButton
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
Re: Dynamically Created CButton
..could you post the whole OnPaint-function ? Perhaps I can help you.
Re: Dynamically Created CButton
It in the original post....
Thanks
-LHoffman
Re: Dynamically Created CButton
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
}
Re: Dynamically Created CButton
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
Re: Dynamically Created CButton
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