Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with SetTextColor

Problem with SetTextColor

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aviv Halperin
    wrote on last edited by
    #1

    Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal

    B I F 3 Replies Last reply
    0
    • A Aviv Halperin

      Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal

      B Offline
      B Offline
      Brian D
      wrote on last edited by
      #2

      How about surrounding the call with: if(pWnd->GetDlgCtrlID() == IDC_EDIT1) or switch the results out. BD


      "You know "that look" women get when they want sex? Me neither." --Steve Martin

      1 Reply Last reply
      0
      • A Aviv Halperin

        Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal

        I Offline
        I Offline
        includeh10
        wrote on last edited by
        #3

        it is not correct. you must call SetTextColor() inside of OnPaint() of your control includeh10

        1 Reply Last reply
        0
        • A Aviv Halperin

          Hi, I have a dialog with a few edit controls. I want to set the text of one control to red and the rest should not change. Now if I call the pDC->SetTextColor(RGB(255,0,0)) in the OnCtlColor message handler of the dialog this changes the text color for all the edit controls in the dialog. If I call the following code: CDC *pDC = EditToChangeTextColor.GetDC(); pDC->SetTextColor(RGB(255,0,0)); nothing changes. Does anyone know if there is a way to change the text collor only for one control in the dialog? Thanks. avivhal

          F Offline
          F Offline
          f64
          wrote on last edited by
          #4

          Hi Check the MSDN fot CWnd::OnCtlColor, it even has a example of what you are asking. You just have to verify the message is for the control you want to change, otherwise do nothing. Extracted form the MSDN

          // This OnCtlColor handler will change the color of a static control
          // with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
          // class has an initialized and created CBrush member named m_brush.
          // The control will be painted with red text and a background
          // color of m_brush.

          HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
          {
          // Call the base class implementation first! Otherwise, it may
          // undo what we're trying to accomplish here.
          HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

          // Are we painting the IDC_MYSTATIC control? We can use
          // CWnd::GetDlgCtrlID() to perform the most efficient test.

          if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
          {
          // Set the text color to red
          pDC->SetTextColor(RGB(255, 0, 0));

            // Set the background mode for text to transparent 
            // so background will show thru.
            pDC->SetBkMode(TRANSPARENT);
          
            // Return handle to our CBrush object
            hbr = m\_brush;
          

          }

          return hbr;
          }

          Fabian

          A 1 Reply Last reply
          0
          • F f64

            Hi Check the MSDN fot CWnd::OnCtlColor, it even has a example of what you are asking. You just have to verify the message is for the control you want to change, otherwise do nothing. Extracted form the MSDN

            // This OnCtlColor handler will change the color of a static control
            // with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
            // class has an initialized and created CBrush member named m_brush.
            // The control will be painted with red text and a background
            // color of m_brush.

            HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
            {
            // Call the base class implementation first! Otherwise, it may
            // undo what we're trying to accomplish here.
            HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

            // Are we painting the IDC_MYSTATIC control? We can use
            // CWnd::GetDlgCtrlID() to perform the most efficient test.

            if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
            {
            // Set the text color to red
            pDC->SetTextColor(RGB(255, 0, 0));

              // Set the background mode for text to transparent 
              // so background will show thru.
              pDC->SetBkMode(TRANSPARENT);
            
              // Return handle to our CBrush object
              hbr = m\_brush;
            

            }

            return hbr;
            }

            Fabian

            A Offline
            A Offline
            Aviv Halperin
            wrote on last edited by
            #5

            Thanks now its working fine, I dont know how I missed this example in the MSDN (should have thought of it myself anyway), I only found a very general one with no reference as to the caller. Is it possible maybe to call SetTextColor from outside OnCtlColor? I tried it in various places and handlers but it did not seem to work(including in the OnPaint handler). Maybe the attributes set for the DC fonts color are blocked when called from outside this segment of code... Thanks again. avivhal

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups