CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2001
    Location
    Russia
    Posts
    14

    CDateTimeCtrl - background color

    Hi!
    Who know how change background color of CDateTimeCtrl (in edit-part of control)?
    Thanks!


  2. #2
    Join Date
    Oct 2001
    Location
    North-Rhine-Westfalia, Germany
    Posts
    5

    Re: CDateTimeCtrl - background color

    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.
    Last edited by JensC; April 29th, 2009 at 06:35 AM.

  3. #3
    Join Date
    Oct 2001
    Location
    North-Rhine-Westfalia, Germany
    Posts
    5

    Re: CDateTimeCtrl - background color

    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.


  4. #4
    Join Date
    Oct 2001
    Location
    North-Rhine-Westfalia, Germany
    Posts
    5

    Re: CDateTimeCtrl - background color

    And another one... Please replace COLOR_BACKGROUND by COLOR_WINDOW. Sorry again.

    Live long and prosper

    JensC


  5. #5
    Join Date
    Mar 2009
    Posts
    15

    Re: CDateTimeCtrl - background color

    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
    Last edited by andwan0; April 29th, 2009 at 06:13 AM.

  6. #6
    Join Date
    Oct 2001
    Location
    North-Rhine-Westfalia, Germany
    Posts
    5

    Re: CDateTimeCtrl - background color

    Quote Originally Posted by andwan0 View Post
    And also add BOOL OnEraseBkgnd(CDC* pDC); to the header file too
    This is done by the wizard. Maybe you didn't follow the instructions above!?

  7. #7
    Join Date
    Mar 2009
    Posts
    15

    Re: CDateTimeCtrl - background color

    Manually derived from CDateTimeCtrl with no wizard....

    btw, does this paint the box (of the drop down bit)?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CDateTimeCtrl - background color

    No, this box is not belong to the *background*.
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: CDateTimeCtrl - background color

    Not bad for an eight year old thread!
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CDateTimeCtrl - background color

    Quote Originally Posted by krmed View Post
    Not bad for an eight year old thread!
    Good point!
    And I only looked at the last post date!
    Victor Nijegorodov

  11. #11
    Join Date
    Oct 2001
    Location
    North-Rhine-Westfalia, Germany
    Posts
    5

    Re: CDateTimeCtrl - background color

    Some things never die.....

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CDateTimeCtrl - background color

    Quote Originally Posted by JensC View Post
    Some things never die.....
    Well "some thins" is the "Old love":
    Old love never dies!
    Victor Nijegorodov

  13. #13
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: CDateTimeCtrl - background color

    I truly hope, at least the OP is still alive.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  14. #14
    Join Date
    May 2001
    Location
    St Louis, MO
    Posts
    5

    Re: CDateTimeCtrl - background color

    This is a very nice solution, but it unfortunately doesn't appear to work on Windows 7.

    Anybody know a solution under Windows 7?

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: CDateTimeCtrl - background color

    Quote Originally Posted by wbrouhaha View Post
    This is a very nice solution, but it unfortunately doesn't appear to work on Windows 7.

    Anybody know a solution under Windows 7?
    And probably it doesn't appear to work under Windows 8 and further Windows...
    Please, if you have a new problem, open a new thread and don't wake up very old ones!

    [ Thread closed ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured