Thread: MFC: Change the color of static text with button press?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    72

    MFC: Change the color of static text with button press?

    I've got some text that I want to change from red to green when the user pushes a button. Currently, I'm coloring the text like so:

    // in OnInitDialog:
    Code:
            m_brush.CreateSolidBrush(RGB(50, 50, 50));
    Code:
    HBRUSH CCineFrameDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
      if (pWnd->GetDlgCtrlID() == 0x200 ||
          pWnd->GetDlgCtrlID() == IDC_STATIC_COLOR)
        {
          pDC->SetTextColor(RGB(200,0,0));
          pDC->SetBkMode(TRANSPARENT);
          hbr = m_brush;
        }
    
      return hbr;
    }
    Two questions -
    1. how go I make it change with the button press?
    2. How do I get it to actually look transparent (as opposed to the attached with the dark background behind the text)? Is there a way to get the background color of the window & set the brush to the color as a default?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    OnCtlColor() is called right before a control is to be drawn.
    The returned brush will be used to draw the control's background.
    Use pDC to set the foreground and background text color and background mode.

    1. Store the text color as a member variable and change it on a button press.
    2. Don't return your own brush, return what CDialog::OnCtlColor() returns.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Got it working - thanks!

    The only thing I'll add for anyone else that attempts this, is that you need to do the Invalidate(); UpdateWindow(); after you change the color in the buttonclick() function.

    Last edited by BrianK; 06-16-2004 at 11:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM
  5. changing static text
    By codec in forum Windows Programming
    Replies: 2
    Last Post: 03-23-2002, 09:45 PM