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. Button color

Button color

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

    How do you change the foreground color of a button?

    F 1 Reply Last reply
    0
    • R Roger

      How do you change the foreground color of a button?

      F Offline
      F Offline
      Fredrik
      wrote on last edited by
      #2

      I am far from good at C++ but this is what I did after looking around on diffrnt sites, hope it helps/works =) First: I used MFC to make the button in my dialog, then I chose Properties with left mouse button on the button and set the button to ownerdrawn. Then use Classwizard to add the WM_DrawItem (Is it called a listener? Nm...hehe) to your dialog window. Then add the following code, with a few changes: void CAboutDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpdis) // (Change the name CAbotDlg to your Dialogs name (Classwizar should do this for you though)) { CBrush myBrush; CBrush m_brHollow; myBrush.CreateSolidBrush(RGB(178,196,213)); //Change the RGB color to what you want the color of the the button to be m_brHollow.CreateStockObject(NULL_BRUSH); const UINT& nAction = lpdis->itemAction; // Full redraw or selected (up/down) state changed CWnd* pCtl = CWnd::FromHandle(lpdis->hwndItem); CString sText; pCtl->GetWindowText(sText); // button text CRect rc; pCtl->GetWindowRect(&rc); // window rectangle.. pCtl->ScreenToClient(&rc); // ..client rectangle if (lpdis->itemState & ODS_SELECTED) // button is down: rc += CPoint(1,1); // shift southeast CDC* pDC = CDC::FromHandle(lpdis->hDC); // painting the background CBrush* pOldBrush = pDC->SelectObject(&myBrush); pDC->PatBlt(0, 0, rc.Width(), rc.Height(), PATCOPY); pOldBrush = pDC->SelectObject(pOldBrush); if (lpdis->CtlType==ODT_BUTTON) { // Draw button border using COLOR_BTNTEXT CBrush* pOldBrush = pDC->SelectObject(&m_brHollow); CPen pen(PS_SOLID, 2, GetSysColor(COLOR_BTNTEXT)); CPen* pOldPen = pDC->SelectObject(&pen); pDC->Rectangle(&rc); // Draw rectangle // Draw button text pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT)); pDC->DrawText(sText,&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE); pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); } else { // Note: assumes static icon! pDC->DrawIcon(0, 0, AfxGetApp()->LoadIcon(IDR_MAINFRAME)); //I'll be honest to tell you I don't fully know what this line does =) } } Hope that helps, I'll be checking in here later for questions/remarks etc... /Fredrik

      R 1 Reply Last reply
      0
      • F Fredrik

        I am far from good at C++ but this is what I did after looking around on diffrnt sites, hope it helps/works =) First: I used MFC to make the button in my dialog, then I chose Properties with left mouse button on the button and set the button to ownerdrawn. Then use Classwizard to add the WM_DrawItem (Is it called a listener? Nm...hehe) to your dialog window. Then add the following code, with a few changes: void CAboutDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpdis) // (Change the name CAbotDlg to your Dialogs name (Classwizar should do this for you though)) { CBrush myBrush; CBrush m_brHollow; myBrush.CreateSolidBrush(RGB(178,196,213)); //Change the RGB color to what you want the color of the the button to be m_brHollow.CreateStockObject(NULL_BRUSH); const UINT& nAction = lpdis->itemAction; // Full redraw or selected (up/down) state changed CWnd* pCtl = CWnd::FromHandle(lpdis->hwndItem); CString sText; pCtl->GetWindowText(sText); // button text CRect rc; pCtl->GetWindowRect(&rc); // window rectangle.. pCtl->ScreenToClient(&rc); // ..client rectangle if (lpdis->itemState & ODS_SELECTED) // button is down: rc += CPoint(1,1); // shift southeast CDC* pDC = CDC::FromHandle(lpdis->hDC); // painting the background CBrush* pOldBrush = pDC->SelectObject(&myBrush); pDC->PatBlt(0, 0, rc.Width(), rc.Height(), PATCOPY); pOldBrush = pDC->SelectObject(pOldBrush); if (lpdis->CtlType==ODT_BUTTON) { // Draw button border using COLOR_BTNTEXT CBrush* pOldBrush = pDC->SelectObject(&m_brHollow); CPen pen(PS_SOLID, 2, GetSysColor(COLOR_BTNTEXT)); CPen* pOldPen = pDC->SelectObject(&pen); pDC->Rectangle(&rc); // Draw rectangle // Draw button text pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT)); pDC->DrawText(sText,&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE); pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); } else { // Note: assumes static icon! pDC->DrawIcon(0, 0, AfxGetApp()->LoadIcon(IDR_MAINFRAME)); //I'll be honest to tell you I don't fully know what this line does =) } } Hope that helps, I'll be checking in here later for questions/remarks etc... /Fredrik

        R Offline
        R Offline
        Roger
        wrote on last edited by
        #3

        Thanks for the help, I had gotten a solution similar to this from another site, nice to know we all think alike. I am actually going to use the colorbtn class that is posted on here under the button control on the front page for this site. Seems like the easiest way to go. I appreciate your help, let me know if you ever need anything.

        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