Hi!
Who know how change background color of CDateTimeCtrl (in edit-part of control)?
Thanks!
Printable View
Hi!
Who know how change background color of CDateTimeCtrl (in edit-part of control)?
Thanks!
Create a new class CMyDateTimeCtrl deriving it from CDateTimeCtrl using class wizard and add a handler for the WM_ERASEBKGND message
Add the following to the .h file:
Code:COLORREF SetBackgroundColor(BOOL bSysColor, COLORREF cr);
COLORREF m_bkgd_color;
CBrush* m_bkgd_brush;
Add this to the .cpp file:
Code:COLORREF CMyDateTimeCtrl::SetBackgroundColor(BOOL bSysColor, COLORREF cr)
{
COLORREF ret_color = m_bkgd_color; // return old color
m_bkgd_color = bSysColor ? ::GetSysColor(COLOR_WINDOW) : cr;
if (ret_color != m_bkgd_color) // color changed?
{
delete m_bkgd_brush; // throw away old brush
m_bkgd_brush = new CBrush(m_bkgd_color); // buy a new one ;-)
Invalidate(); // repaint
}
return ret_color;
}
BOOL CMyDateTimeCtrl::OnEraseBkgnd(CDC* pDC)
{
CBrush * old_brush = pDC->SelectObject(m_bkgd_brush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(old_brush);
return TRUE;
}
Your constructor and destructor should be changed to this:
Code:CMyDateTimeCtrl::CMyDateTimeCtrl()
: CDateTimeCtrl(), m_bkgd_color(::GetSysColor(COLOR_WINDOW))
{
m_bkgd_brush = new CBrush(::GetSysColor(COLOR_WINDOW));
}
CMyDateTimeCtrl::~CMyDateTimeCtrl()
{
delete m_bkgd_brush;
}
HTH
Jens C.
Oops, I did it again!! The call to Invalidate() does not belong to the comment. Please insert CR/LF in front of it. Sorry for the GBF (General Brain Failure).
Jens C.
And another one... Please replace COLOR_BACKGROUND by COLOR_WINDOW. Sorry again.
Live long and prosper
JensC
And also add BOOL OnEraseBkgnd(CDC* pDC); to the header file too ;)
And replace those HTML > codes with > symbol ;)
error C2084: function '__thiscall CMyDateTimeCtrl::~ CMyDateTimeCtrl(void)' already has a body
Manually derived from CDateTimeCtrl with no wizard....
btw, does this paint the box (of the drop down bit)?
No, this box is not belong to the *background*.
Not bad for an eight year old thread!
Some things never die..... ;)
I truly hope, at least the OP is still alive. :D
This is a very nice solution, but it unfortunately doesn't appear to work on Windows 7.
Anybody know a solution under Windows 7?