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. CEdit's Text Color when Disabled

CEdit's Text Color when Disabled

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 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.
  • V Offline
    V Offline
    Vladimir Georgiev
    wrote on last edited by
    #1

    Hi, Does anyone know what is the way to change a CEdit's text color when the control is disabled? I already implemented the reflected OnCtlColor, and it successfully changes the color when the control is enabled, but when disabled, the color becomes gray... Thank you in advance. "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell

    D U 2 Replies Last reply
    0
    • V Vladimir Georgiev

      Hi, Does anyone know what is the way to change a CEdit's text color when the control is disabled? I already implemented the reflected OnCtlColor, and it successfully changes the color when the control is enabled, but when disabled, the color becomes gray... Thank you in advance. "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Vladimir Georgiev wrote: ...but when disabled, the color becomes gray... This is normal (and expected) behavior. If you changed it to some other color, would the user know that the control was disabled, or would they try to interact with it and become frustrated when they couldn't?


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      1 Reply Last reply
      0
      • V Vladimir Georgiev

        Hi, Does anyone know what is the way to change a CEdit's text color when the control is disabled? I already implemented the reflected OnCtlColor, and it successfully changes the color when the control is enabled, but when disabled, the color becomes gray... Thank you in advance. "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell

        U Offline
        U Offline
        Ulric Auger
        wrote on last edited by
        #3

        I found a way to change the text color of a disabled CEdit. 1- You must derive a class from CEdit 2- Overwrite WindowProc and do the following:

        LRESULT CMyEditBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
        {
        if (message == WM_PAINT)
        {
        DWORD dwStyle = GetStyle();
        if (dwStyle & WS_DISABLED)
        {
        EnableWindow(TRUE); //Trick paint by temporarily enabling CEdit
        SetReadOnly(TRUE); //Temporarily making CEdit read only

              LRESULT res = CEdit::WindowProc(message, wParam, lParam);
        
              SetRedraw(FALSE);
        
              if ((dwStyle&ES\_READONLY) == 0) SetReadOnly(FALSE); //Restore READ ONLY style
              EnableWindow(FALSE); //Restore DISABLED style
        
              SetRedraw(TRUE);
        
              CRect rcClient;
              GetWindowRect(rcClient);
              ScreenToClient(rcClient);
              ValidateRect(rcClient);
        
              return res;
          }
        

        }

        return CEdit::WindowProc(message, wParam, lParam);
        }

        3- Overwrite CtlColor and change color when control is in read-only state (of course you can use a member variable instead of the read-only style to set disabled text color)

        HBRUSH CMyEditBox::CtlColor(CDC* pDC, UINT nCtlColor)
        {
        if (GetStyle() & ES_READONLY)
        {
        pDC->SetBkColor(GetSysColor(COLOR_3DFACE));
        pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
        return GetSysColorBrush(COLOR_3DFACE);
        }

        return NULL;
        }

        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