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