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. quick question on color

quick question on color

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 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.
  • J Offline
    J Offline
    jafrazee
    wrote on last edited by
    #1

    i am using onctlcolor message to set the the background color of edit controls. the problem is that the background only changes background color if there is text in the control and only for the length of the text. can the background color be changed regardless of what is in the control for data? thank you

    S J 2 Replies Last reply
    0
    • J jafrazee

      i am using onctlcolor message to set the the background color of edit controls. the problem is that the background only changes background color if there is text in the control and only for the length of the text. can the background color be changed regardless of what is in the control for data? thank you

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #2

      Sounds like you're using SetBgColor(). Instead (or in addition) create a solid color brush and return that._**

      Developers that like shiny objects also dig case mods and scratch-and-sniff stickers.

      **_

      Klaus Probst, The Lounge

      J 1 Reply Last reply
      0
      • J jafrazee

        i am using onctlcolor message to set the the background color of edit controls. the problem is that the background only changes background color if there is text in the control and only for the length of the text. can the background color be changed regardless of what is in the control for data? thank you

        J Offline
        J Offline
        Jeremy Falcon
        wrote on last edited by
        #3

        Don't forget to invalidate the entire region of the controls to force a paint. Jeremy L. Falcon "The One Who Said, 'The One Who Said...'" Homepage : Feature Article : Sonork = 100.16311

        1 Reply Last reply
        0
        • S Shog9 0

          Sounds like you're using SetBgColor(). Instead (or in addition) create a solid color brush and return that._**

          Developers that like shiny objects also dig case mods and scratch-and-sniff stickers.

          **_

          Klaus Probst, The Lounge

          J Offline
          J Offline
          jafrazee
          wrote on last edited by
          #4

          no i am using setbkcolor() this is the onctlcolor it works the ok but it only changes the color behind the text not all the background of the control. if there is no text the color will not change at all. when i want the color to change i call Invalidate() HBRUSH PREFS::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); int nCtrl = pWnd ->GetDlgCtrlID(); switch(nCtrl){ case IDC_BCOLOR: pDC->SetBkColor(RGB(m_red,m_green,m_blue)); break; /////////////////////plus more } return hbr; }

          S 1 Reply Last reply
          0
          • J jafrazee

            no i am using setbkcolor() this is the onctlcolor it works the ok but it only changes the color behind the text not all the background of the control. if there is no text the color will not change at all. when i want the color to change i call Invalidate() HBRUSH PREFS::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); int nCtrl = pWnd ->GetDlgCtrlID(); switch(nCtrl){ case IDC_BCOLOR: pDC->SetBkColor(RGB(m_red,m_green,m_blue)); break; /////////////////////plus more } return hbr; }

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #5

            jafrazee wrote: no i am using setbkcolor() Sorry, typo (yes i am too lazy to bother checking each post against MSDN before submitting ;) ) What you need to do is this (assume you've added a member to your class of type HBRUSH and named it m_hBrush, since you'll need to delete this when done with it):

            HBRUSH PREFS::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
            {
            HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
            int nCtrl = pWnd->GetDlgCtrlID();
            switch (nCtrl)
            {
            case IDC_BCOLOR:
            pDC->SetBkColor(RGB(m_red,m_green,m_blue));
            if ( NULL != m_hBrush )
            ::DeleteObject(m_hBrush);
            m_hBrush = ::CreateSolidBrush(RGB(m_red,m_green,m_blue));
            hbr = m_hBrush;

            break;
            /////////////////////plus more
            }
            return hbr;
            }

            Alternately, you could just keep a handle of a pre-created brush as a class member and return that. Explanation: The background color set using SetBkColor() applies to the text only, as you've noticed. In order to color the rest of the background, you need to return a brush of the appropriate color. This is what the code in bold does above. Windows does not delete the brush when it is done with it, so you must remember to do so after the window is destroyed (or before creating and returning a different brush, as shown)._**

            Developers that like shiny objects also dig case mods and scratch-and-sniff stickers.

            **_

            Klaus Probst, The Lounge

            J 1 Reply Last reply
            0
            • S Shog9 0

              jafrazee wrote: no i am using setbkcolor() Sorry, typo (yes i am too lazy to bother checking each post against MSDN before submitting ;) ) What you need to do is this (assume you've added a member to your class of type HBRUSH and named it m_hBrush, since you'll need to delete this when done with it):

              HBRUSH PREFS::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
              {
              HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
              int nCtrl = pWnd->GetDlgCtrlID();
              switch (nCtrl)
              {
              case IDC_BCOLOR:
              pDC->SetBkColor(RGB(m_red,m_green,m_blue));
              if ( NULL != m_hBrush )
              ::DeleteObject(m_hBrush);
              m_hBrush = ::CreateSolidBrush(RGB(m_red,m_green,m_blue));
              hbr = m_hBrush;

              break;
              /////////////////////plus more
              }
              return hbr;
              }

              Alternately, you could just keep a handle of a pre-created brush as a class member and return that. Explanation: The background color set using SetBkColor() applies to the text only, as you've noticed. In order to color the rest of the background, you need to return a brush of the appropriate color. This is what the code in bold does above. Windows does not delete the brush when it is done with it, so you must remember to do so after the window is destroyed (or before creating and returning a different brush, as shown)._**

              Developers that like shiny objects also dig case mods and scratch-and-sniff stickers.

              **_

              Klaus Probst, The Lounge

              J Offline
              J Offline
              jafrazee
              wrote on last edited by
              #6

              that works great thanks

              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