Thread: Updating Static Control Color Real-Time :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001

    Updating Static Control Color Real-Time :: MFC


    What is the best way to update the background color of one or more static/edit control in a dialog box? For example, I have a static control in a dialog box that I use to depict font color. If the user sets the font to have green color, than the user will see a static control that is green. Initially, I use WM_CTLCOLOR to update the color of the static control. The problem is it only works when the dialog first initializes. Once it has been initialized, I cannot figure out how to update the static control after the user have selected a different color. I try PostMessage(WM_CTLCOLOR, 0, 0), however, the program crashes. It seems ON_WM_CTLCOLOR only works when the dialog is initialized.

    Here is the code.

    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd->GetDlgCtrlID() == IDC_STATICCOLOR)
    // m_pBrush is a pointer to a brush object I use containing the
    // font color I the user wants

    if (m_pBrush != NULL)
    delete m_pBrush;
    m_pBrush = NULL;

    m_pBrush = new CBrush(m_Color);
    hbr = static_cast<HBRUSH>(m_pBrush->GetSafeHandle());

    Please post if you have any idea on a possible solution.


  2. #2
    Join Date
    Jan 2002
    The msg is only sent when windows needs to repaint the control's background so try Invalidating the control ( or its parent) with InvalidateRect ( you want to erase the background):





  3. #3

    The solution is calling Invalidate(). It repaints the dialog box and update all static controls.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static control color change
    By Devil Panther in forum Windows Programming
    Replies: 6
    Last Post: 08-10-2005, 09:36 AM
  2. Few Questions (Styles, Static Control)
    By Zeusbwr in forum Windows Programming
    Replies: 11
    Last Post: 04-15-2005, 04:13 AM
  3. STATIC text label or LABEL in Windows APP - NO MFC!
    By triste in forum Windows Programming
    Replies: 5
    Last Post: 10-25-2004, 11:14 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM