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. 880410 - CtlColor

880410 - CtlColor

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

    hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:

    HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    if (d_chars==decimalChars) // if mode==emDecimal
    {
    CString s;
    GetWindowText(s);
    USES_CONVERSION;
    int val=atoi(T2A(s));
    if (val<d_minVal || val>d_maxVal)
    {
    pDC->SetTextColor(RED);
    pDC->SetBkColor(YELLOW);
    }
    }
    return NULL;
    }

    i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx

    _ C 2 Replies Last reply
    0
    • I ilostmyid2

      hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:

      HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
      {
      if (d_chars==decimalChars) // if mode==emDecimal
      {
      CString s;
      GetWindowText(s);
      USES_CONVERSION;
      int val=atoi(T2A(s));
      if (val<d_minVal || val>d_maxVal)
      {
      pDC->SetTextColor(RED);
      pDC->SetBkColor(YELLOW);
      }
      }
      return NULL;
      }

      i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      ilostmyid2 wrote:

      derived a class from CEdit for this purpose

      This is not correct. WM_CTLCOLOR is sent to the parent window. So you should be handling OnCtlColor of the parent dialog. In that, check if the message is for your particular edit control and then do pDC->SetTextColor.

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • I ilostmyid2

        hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:

        HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
        {
        if (d_chars==decimalChars) // if mode==emDecimal
        {
        CString s;
        GetWindowText(s);
        USES_CONVERSION;
        int val=atoi(T2A(s));
        if (val<d_minVal || val>d_maxVal)
        {
        pDC->SetTextColor(RED);
        pDC->SetBkColor(YELLOW);
        }
        }
        return NULL;
        }

        i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx

        C Offline
        C Offline
        Chuck OToole
        wrote on last edited by
        #3

        MyEditBox::MyEditBox()
        {
        m_brush = NULL;
        itemData = NULL;
        }

        MyEditBox::~MyEditBox()
        {
        delete m_brush;
        }

        BEGIN_MESSAGE_MAP(MyEditBox, CEdit)
        //{{AFX_MSG_MAP(MyEditBox)
        ON_WM_CTLCOLOR_REFLECT()
        //}}AFX_MSG_MAP
        END_MESSAGE_MAP()

        bool MyEditBox::SetColorScheme(COLORREF brushColor, COLORREF bgTextColor, COLORREF fgTextColor)
        {
        delete m_brush;
        m_brush = new CBrush(brushColor);
        bgc = bgTextColor;
        fgc = fgTextColor;
        Invalidate();
        UpdateWindow();
        return (m_brush != NULL);
        }

        void MyEditBox::ResetColorScheme()
        {
        delete m_brush;
        m_brush = NULL;
        Invalidate();
        UpdateWindow();
        }
        /////////////////////////////////////////////////////////////////////////////
        // MyEditBox message handlers
        /////////////////////////////////////////////////////////////////////////////

        HBRUSH MyEditBox::CtlColor(CDC* pDC, UINT nCtlColor)
        {
        if (m_brush == NULL)
        return NULL;
        //note - if brush and text background color are different, anomalies will occur
        pDC->SetTextColor(fgc);
        pDC->SetBkColor(bgc);
        return (HBRUSH)m_brush->GetSafeHandle();
        }

        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