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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Change bg color of editbox.

Change bg color of editbox.

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 7 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.
  • A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #1

    Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

    C M V D 5 Replies Last reply
    0
    • A acerunner316

      Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Not really. An edit box is really nasty, as it paints itself outside the normal WM_PAINT method, so any owner drawing tends to get erased as you type. It can be done, but not without flicker, and not as trivially as, say, a button. To change the color of a button, you can derive a class from the button class, and draw the background yourself. No bitmap needed.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      A 1 Reply Last reply
      0
      • C Christian Graus

        Not really. An edit box is really nasty, as it paints itself outside the normal WM_PAINT method, so any owner drawing tends to get erased as you type. It can be done, but not without flicker, and not as trivially as, say, a button. To change the color of a button, you can derive a class from the button class, and draw the background yourself. No bitmap needed.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        A Offline
        A Offline
        acerunner316
        wrote on last edited by
        #3

        "To change the color of a button, you can derive a class from the button class, and draw the background yourself. No bitmap needed." Can you explain furthur please?

        1 Reply Last reply
        0
        • A acerunner316

          Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          acerunner316 wrote:

          Is there a simple way to change the BG color (and maybe text color also) of an editbox?

          Check out the WM_CTLCOLOREDIT message.

          realJSOPR 1 Reply Last reply
          0
          • A acerunner316

            Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Here's a Christmas edit box (adjust colors for your region/season/religion/etc): (This is MFC code but API calls are similar)

            CBrush RedBrush; //class member in MyDlg.h
            ...
            // in CMyDlg consructor
            RedBrush.CreateSolidBrush(RGB(0xFF,0x00,0x00));
            ...
            HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
            {
            HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

            if (nCtlColor == CTLCOLOR\_EDIT)
            {
            	pDC->SetTextColor(RGB(0x00,0xFF,0x00));
            	pDC->SetBkColor(RGB(0xFF,0x00,0x00));
            	hbr = RedBrush;
            }
            
            return hbr;
            

            }

            -- modified at 11:25 Tuesday 5th December, 2006

            T 1 Reply Last reply
            0
            • M Mark Salsbery

              Here's a Christmas edit box (adjust colors for your region/season/religion/etc): (This is MFC code but API calls are similar)

              CBrush RedBrush; //class member in MyDlg.h
              ...
              // in CMyDlg consructor
              RedBrush.CreateSolidBrush(RGB(0xFF,0x00,0x00));
              ...
              HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
              {
              HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

              if (nCtlColor == CTLCOLOR\_EDIT)
              {
              	pDC->SetTextColor(RGB(0x00,0xFF,0x00));
              	pDC->SetBkColor(RGB(0xFF,0x00,0x00));
              	hbr = RedBrush;
              }
              
              return hbr;
              

              }

              -- modified at 11:25 Tuesday 5th December, 2006

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              Mark Salsbery wrote: Christmas edit box he he he

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you

              M 1 Reply Last reply
              0
              • A acerunner316

                Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

                V Offline
                V Offline
                vikas amin
                wrote on last edited by
                #7

                No way " Nothing is easy , u have to work hard "

                Vikas Amin EATON PUNE

                1 Reply Last reply
                0
                • M Mark Salsbery

                  acerunner316 wrote:

                  Is there a simple way to change the BG color (and maybe text color also) of an editbox?

                  Check out the WM_CTLCOLOREDIT message.

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  This doesn't work when you've also changed the background color of the dialog box itself.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  M 1 Reply Last reply
                  0
                  • T ThatsAlok

                    Mark Salsbery wrote: Christmas edit box he he he

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    No no no... That's "Ho Ho Ho" :)

                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      This doesn't work when you've also changed the background color of the dialog box itself.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      John Simmons / outlaw programmer wrote:

                      This doesn't work when you've also changed the background color of the dialog box itself.

                      ummm. no :) Maybe you are thinking of WM_ERASEBKND? Try it. I posted an MFC example below.

                      1 Reply Last reply
                      0
                      • A acerunner316

                        Is there a simple way to change the BG color (and maybe text color also) of an editbox? I only need two colors to be toggled by clicking a button. I wanted to toggle the color of the button, but found out that it can only be done using images as buttons, which seemed too complicated for me.

                        D Offline
                        D Offline
                        digitalmythology
                        wrote on last edited by
                        #11

                        As soon as I can, I'll get you a working, practical piece of real-worl code. For now though, try this from MSDN : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/editcontrols/editcontrolreference/editcontrolmessages/wm_ctlcoloredit.asp[^]

                        -digitalmythology -dm

                        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