Updating Static Control Color Real-Time :: MFC

This is a discussion on Updating Static Control Color Real-Time :: MFC within the Windows Programming forums, part of the Platform Specific Boards category; Hi. What is the best way to update the background color of one or more static/edit control in a dialog ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Updating Static Control Color Real-Time :: MFC

    Hi.

    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.

    Thanks,
    Kuphryn

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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):

    ie:

    CWnd_Derived_Control_object->InvalidateRect(0);

    or

    CWnd_Derived_Control_object.InvalidateRect(0);

  3. #3
    Unregistered
    Guest
    Yeah.

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

    Thanks,
    Kuphryn

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, 10:36 AM
  2. Few Questions (Styles, Static Control)
    By Zeusbwr in forum Windows Programming
    Replies: 11
    Last Post: 04-15-2005, 05:13 AM
  3. STATIC text label or LABEL in Windows APP - NO MFC!
    By triste in forum Windows Programming
    Replies: 5
    Last Post: 10-26-2004, 12:14 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21