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. OnCtrlColor - how do I set a button fore color?

OnCtrlColor - how do I set a button fore color?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
5 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    michael thomas
    wrote on last edited by
    #1

    Hi, I'm about to spontaneously combust if I can't manage to set the color of one single little button in a dialog. It seems that with OnCtlColor() I can set the color of EVERYTHING EXCEPT my button's foreground. Please help me before I blow! :confused: I know I am missing something probably very simple. Do I need to use OnDrawItem()? Thank you for any suggestions. I am relatively new (1 year) to MFC so be kind and if possible detailed in your suggestions. Thank you so much. Here's one of several bits of code I have tried :- //CBrush is set within OnInitDialog() m_brush.CreateSolidBrush(RGB(0,0,255)); // Blue brush .... HBRUSH CTry_button_colorsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //this works ok - my editbox ends up a nice blue color if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_MSGBOX ) { pDC->SelectObject(m_brush); hbr = this->m_brush; } //THIS DOES NOT WORK if (nCtlColor == CTLCOLOR_BTN) { hbr = this->m_brush; pDC->SetBkColor(RGB(0,0,255)); } //THIS DOESN'T WORK EITHER if( pWnd->GetDlgCtrlID() == IDC_MYBUTTON) { pDC->SetBkColor(RGB(0,0,255)); pDC->SelectStockObject(WHITE_BRUSH); } return hbr; } Michael

    R H 2 Replies Last reply
    0
    • M michael thomas

      Hi, I'm about to spontaneously combust if I can't manage to set the color of one single little button in a dialog. It seems that with OnCtlColor() I can set the color of EVERYTHING EXCEPT my button's foreground. Please help me before I blow! :confused: I know I am missing something probably very simple. Do I need to use OnDrawItem()? Thank you for any suggestions. I am relatively new (1 year) to MFC so be kind and if possible detailed in your suggestions. Thank you so much. Here's one of several bits of code I have tried :- //CBrush is set within OnInitDialog() m_brush.CreateSolidBrush(RGB(0,0,255)); // Blue brush .... HBRUSH CTry_button_colorsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //this works ok - my editbox ends up a nice blue color if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_MSGBOX ) { pDC->SelectObject(m_brush); hbr = this->m_brush; } //THIS DOES NOT WORK if (nCtlColor == CTLCOLOR_BTN) { hbr = this->m_brush; pDC->SetBkColor(RGB(0,0,255)); } //THIS DOESN'T WORK EITHER if( pWnd->GetDlgCtrlID() == IDC_MYBUTTON) { pDC->SetBkColor(RGB(0,0,255)); pDC->SelectStockObject(WHITE_BRUSH); } return hbr; } Michael

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      You can't change the button colour using OnCtlColor(). You'll have to create an owner-draw button to do this. michael thomas wrote: I'm about to spontaneously combust Don't spend too much time in the sun then... ;P

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      M 1 Reply Last reply
      0
      • M michael thomas

        Hi, I'm about to spontaneously combust if I can't manage to set the color of one single little button in a dialog. It seems that with OnCtlColor() I can set the color of EVERYTHING EXCEPT my button's foreground. Please help me before I blow! :confused: I know I am missing something probably very simple. Do I need to use OnDrawItem()? Thank you for any suggestions. I am relatively new (1 year) to MFC so be kind and if possible detailed in your suggestions. Thank you so much. Here's one of several bits of code I have tried :- //CBrush is set within OnInitDialog() m_brush.CreateSolidBrush(RGB(0,0,255)); // Blue brush .... HBRUSH CTry_button_colorsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //this works ok - my editbox ends up a nice blue color if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_MSGBOX ) { pDC->SelectObject(m_brush); hbr = this->m_brush; } //THIS DOES NOT WORK if (nCtlColor == CTLCOLOR_BTN) { hbr = this->m_brush; pDC->SetBkColor(RGB(0,0,255)); } //THIS DOESN'T WORK EITHER if( pWnd->GetDlgCtrlID() == IDC_MYBUTTON) { pDC->SetBkColor(RGB(0,0,255)); pDC->SelectStockObject(WHITE_BRUSH); } return hbr; } Michael

        H Offline
        H Offline
        HPSI
        wrote on last edited by
        #3

        If you want to change the color of a dialog button, you have to use owner-draw button. (you can use bitmap buttons) Changing the color through OnCtlColor() will not work for buttons. The following Knowledge Base articles may be of help to you: ID: Q32685 TITLE: Using the WM_CTLCOLOR Message ID: Q64328 SAMPLE: Owner-Draw: 3-D Push Button Made from Bitmaps with Text HPS HwndSpy - GUI developer's aid to visually locate and inspect windows. For the month of August only, use coupon code CP-81239 for 30% off.

        M 1 Reply Last reply
        0
        • R Ryan Binns

          You can't change the button colour using OnCtlColor(). You'll have to create an owner-draw button to do this. michael thomas wrote: I'm about to spontaneously combust Don't spend too much time in the sun then... ;P

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          M Offline
          M Offline
          michael thomas
          wrote on last edited by
          #4

          Thanks Ryan.

          1 Reply Last reply
          0
          • H HPSI

            If you want to change the color of a dialog button, you have to use owner-draw button. (you can use bitmap buttons) Changing the color through OnCtlColor() will not work for buttons. The following Knowledge Base articles may be of help to you: ID: Q32685 TITLE: Using the WM_CTLCOLOR Message ID: Q64328 SAMPLE: Owner-Draw: 3-D Push Button Made from Bitmaps with Text HPS HwndSpy - GUI developer's aid to visually locate and inspect windows. For the month of August only, use coupon code CP-81239 for 30% off.

            M Offline
            M Offline
            michael thomas
            wrote on last edited by
            #5

            Thanks for the help. Michael

            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