Edit Boxes & Color (MFC)

This is a discussion on Edit Boxes & Color (MFC) within the Windows Programming forums, part of the Platform Specific Boards category; Is it possible to change the color of text in an edit box, and also change the color of read-only ...

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Edit Boxes & Color (MFC)

    Is it possible to change the color of text in an edit box, and also change the color of read-only (to make it white, instead of the button-color)?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Definitely. One solution is subclassing.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    It would be WM_CTLCOLOREDIT & WM_CTLCOLORSTATIC.

    Something like:

    Code:
       case WM_CTLCOLOREDIT:
          SetTextColor (hdc, txtColor);
          SetBkColor (hdc, bkColor);
    
          return ((LRESULT) editBkBrush);
    But I have one other, yet related question. Has anyone managed to change
    the color for disabled edit or static boxes? Seems like they use GetSysColor(COLOR_GRAYTEXT) for that in original edit proc.

    Should I try with SetSysColor() just before edit box receives WM_PAINT and
    some of its notifications? Can I screw up something else if I play with that?
    Any experience?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Use the same method you just posted.
    A read-only or disabled edit control will use the WM_CTLCOLORSTATIC message.

    gg

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Thanks Delf

    I also found this not to long ago, and it seems to work. I can change the text color and background color of edit boxes and statics.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Quote Originally Posted by Codeplug
    Use the same method you just posted.
    A read-only or disabled edit control will use the WM_CTLCOLORSTATIC message.

    gg

    Are you sure? You mean disabled with EnableWindow(..., FALSE) ?

    I do handle WM_CTLCOLORSTATIC and it doesn't affect disable controls.

    In the CodeProject's article Quantrizi posted above there are these lines:
    Code:
    m_ebCtl.SetReadOnly();      //This makes it so nobody can edit the text.
                                                //If you disable the box it does not let you
                                                //change colors.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    I'd say look at the code on how it's done, and go from there

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Just read the manual: WM_CTLCOLORSTATIC

    gg

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Quote Originally Posted by Codeplug
    Just read the manual: WM_CTLCOLORSTATIC

    gg

    Thanks for being patient with a stubborn person - ME!

    I do handle WM_CTLCOLORSTATIC but it's like this:
    Code:
    LRESULT  StdOnCtlColorStatic (HWND hwnd, WPARAM wParam, LPARAM lParam)  // hwnd of real window!
    {
       // on  WM_CTLCOLORSTATIC:
    
       HDC        hdc = (HDC) wParam;
                 //  hwnd = (HWND) lParam;
              
       // SetTextColor (hdc, RGB(255, 92, 16));
       SetBkColor (hdc, gStatBkColor);
    
       return ((LRESULT)gStatBkBrush);
    }
    I just left SetTextColor() here as a reminder abou something I *could*
    use if I need to. Someone just had to remind me about my reminder! So, thanks!

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Quote Originally Posted by Codeplug
    Just read the manual: WM_CTLCOLORSTATIC

    gg

    For a moment I had a feeling that I'm getting somewhere...

    The problem is - well, there are two problems:

    1. MSDN is wrong,
    2. Disabled edit controls really do use WM_CTLCOLORSTATIC when disabled,
    but only for background. Text color is hardcoded deep into User32.dll.

    I tried it on Win98 and on XP an it's the same. I can make disabled
    edit have background whatever I like, but text is always GRAY.

    Bad luck.

    Then I looked into WINE, just to make sure.

    Code:
      if ( get_app_version() >= 0x40000 &&(
                 !es->bEnableState || (es->style & ES_READONLY)))
              EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
      else
              EDIT_SEND_CTLCOLOR(hwnd, dc);
    
       if (!es->bEnableState)
    		    SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
    EDIT_SEND_CTLCOLORSTATIC and EDIT_SEND_CTLCOLOR are macros, but
    you can guess what they do.

    Seems like MS changed this thing after Win 3.x, but only halfway.
    One day I'll try playing with SetSysColor() and see what happens.
    Last edited by Delf; 08-13-2004 at 05:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 07:46 AM
  2. Edit box(es), and specialized onmouseover
    By Lurker in forum Windows Programming
    Replies: 7
    Last Post: 05-25-2003, 04:13 PM
  3. Edit Boxes
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 02-13-2002, 01:54 PM
  4. please help visual c++ edit control boxes
    By alcoholic in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 01:39 PM
  5. Password Edit Boxes
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2001, 02:40 PM

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