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. Set edit box text colour

Set edit box text colour

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
4 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    Caoimh
    wrote on last edited by
    #1

    any idea, how to set the colour of text for an edit box. Say from black to blue. Kind Regards Caoimh

    R 1 Reply Last reply
    0
    • C Caoimh

      any idea, how to set the colour of text for an edit box. Say from black to blue. Kind Regards Caoimh

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      You'll need to override the WM_CTLCOLOR message via CWnd::OnCtlColor()'s handler function. You can then set the colour of the edit (or static) controls of your program.HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch(nCtlColor) { case CTLCOLOR_EDIT: **// colour edit controls** { pDC->SetTextColor(RGB(0, 0, 255)); **// set text to red** pDC->SetBkMode(TRANSPARENT); } break; default: break; } return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // call the base functon }


      I Dream of Absolute Zero

      M 1 Reply Last reply
      0
      • R RChin

        You'll need to override the WM_CTLCOLOR message via CWnd::OnCtlColor()'s handler function. You can then set the colour of the edit (or static) controls of your program.HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch(nCtlColor) { case CTLCOLOR_EDIT: **// colour edit controls** { pDC->SetTextColor(RGB(0, 0, 255)); **// set text to red** pDC->SetBkMode(TRANSPARENT); } break; default: break; } return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // call the base functon }


        I Dream of Absolute Zero

        M Offline
        M Offline
        MeterMan
        wrote on last edited by
        #3

        I have a question in regard to your post. I'm sure there is a way I just can't figure it out. How do you set an individual edit box's background color instead of CTLCOLOR_EDIT where it changes all the edit controls? Secondly I can't get the code you posted to actually work. I changed a few things to get it to work. HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(100,190,199)); HBRUSH B = CreateSolidBrush(RGB(2,2,255)); return (HBRUSH) B; } HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr; } Win32newb "Making windows programs worse than they already are"

        R 1 Reply Last reply
        0
        • M MeterMan

          I have a question in regard to your post. I'm sure there is a way I just can't figure it out. How do you set an individual edit box's background color instead of CTLCOLOR_EDIT where it changes all the edit controls? Secondly I can't get the code you posted to actually work. I changed a few things to get it to work. HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(100,190,199)); HBRUSH B = CreateSolidBrush(RGB(2,2,255)); return (HBRUSH) B; } HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr; } Win32newb "Making windows programs worse than they already are"

          R Offline
          R Offline
          RChin
          wrote on last edited by
          #4

          Firstly, looking at the code that you've posted, I suspect the reason why it now suddenly works is because your control is a regular static window (used for read-only text disply etc) and not an edit control, as was assumed. I tend to use a switch statement to distinguish between the control types (there are other types such as CTLCOLOR_DLG and CTLCOLOR_LISTBOX)
          Secondly, you can target a specific control by quering its control id. For Example:if(pWnd->GetDlgCtrlID() == IDC_EDIT_INPUT) { pDC->SetTextColor(RGB(0,190,0)); pDC->SetBkMode(TRANSPARENT); }


          I Dream of Absolute Zero

          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