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. Need a simple way to change Back Color of CEdit & CStatic controls

Need a simple way to change Back Color of CEdit & CStatic controls

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
7 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.
  • V Offline
    V Offline
    vikas amin
    wrote on last edited by
    #1

    I have a small problem were i need to change the back color of the CEdit & CStatic control , is there any simple code for the same. Yeh and how can i change the font color in CEdit. thank u Vikas Amin Embin Technology Bombay vikas.amin@embin.com

    P 1 Reply Last reply
    0
    • V vikas amin

      I have a small problem were i need to change the back color of the CEdit & CStatic control , is there any simple code for the same. Yeh and how can i change the font color in CEdit. thank u Vikas Amin Embin Technology Bombay vikas.amin@embin.com

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      use ON_WM_CTLCOLOR message

      L V 2 Replies Last reply
      0
      • P prasad_som

        use ON_WM_CTLCOLOR message

        L Offline
        L Offline
        Laffis
        wrote on last edited by
        #3

        Cool! Learn something new everyday! I suppose this is faster (performance wise) than use a derived class from CEdit etc and doing DrawItem ect. And much simpler.

        V 1 Reply Last reply
        0
        • P prasad_som

          use ON_WM_CTLCOLOR message

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

          first of all Thank u for your reply :-D I am using a MFC application were i am displaying a Dialogbox were i have a CEdit control . Which is suppose to change the colour when a user enters invalid entry (non-numeric) I dont wont to touch the base class (CEdit) or nor want to derive new class from it . ON_WM_CTLCOLOR I need some solution i have tried to get the "Device Context "of the edit box but it could not print inside rather it printed outside the Edit box . :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

          L 1 Reply Last reply
          0
          • L Laffis

            Cool! Learn something new everyday! I suppose this is faster (performance wise) than use a derived class from CEdit etc and doing DrawItem ect. And much simpler.

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

            first of all Thank u for your reply I am using a MFC application were i am displaying a Dialogbox were i have a CEdit control . Which is suppose to change the colour when a user enters invalid entry (non-numeric) I dont wont to touch the base class (CEdit) or nor want to derive new class from it . ON_WM_CTLCOLOR I need some solution i have tried to get the "Device Context "of the edit box but it could not print inside rather it printed outside the Edit box . Vikas Amin Embin Technology Bombay vikas.amin@embin.com

            1 Reply Last reply
            0
            • V vikas amin

              first of all Thank u for your reply :-D I am using a MFC application were i am displaying a Dialogbox were i have a CEdit control . Which is suppose to change the colour when a user enters invalid entry (non-numeric) I dont wont to touch the base class (CEdit) or nor want to derive new class from it . ON_WM_CTLCOLOR I need some solution i have tried to get the "Device Context "of the edit box but it could not print inside rather it printed outside the Edit box . :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

              L Offline
              L Offline
              Laffis
              wrote on last edited by
              #6

              Use that message and handle it, amin, it is very easy. Do a search on the web you will get an example in msdn. I just spent 5 min doing that and get a test case working. As to the number thing, you should be able to config the edit ctrl to accept number only. In the code, you may be able to use something like this: DDV_MinMaxLong(...) <-- check reference how to use it. It's been a while since I last use them. What u will get is if user inputs an invalid entry, a messagebox will come up telling him so. You do not even need to change the CEdit color to let him know.

              V 1 Reply Last reply
              0
              • L Laffis

                Use that message and handle it, amin, it is very easy. Do a search on the web you will get an example in msdn. I just spent 5 min doing that and get a test case working. As to the number thing, you should be able to config the edit ctrl to accept number only. In the code, you may be able to use something like this: DDV_MinMaxLong(...) <-- check reference how to use it. It's been a while since I last use them. What u will get is if user inputs an invalid entry, a messagebox will come up telling him so. You do not even need to change the CEdit color to let him know.

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

                ;) Thank you very much i have got the code working now The client requirement was to change the colour of the dialog box so i had to do that . just pasted the code for other programers Example // This OnCtlColor handler will change the color of a static control // with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog // class has an initialized and created CBrush member named m_brush. // The control will be painted with red text and a background // color of m_brush. HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { // Call the base class implementation first! Otherwise, it may // undo what we're trying to accomplish here. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use // CWnd::GetDlgCtrlID() to perform the most efficient test. if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC) { // Set the text color to red pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent // so background will show thru. pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object hbr = m_brush; } return hbr; } Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                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