title: How to change the background properties of an mfc application
body: <p>CURRENT UI</p>
  
  <p><a href="https://i.stack.imgur.com/GjhQh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GjhQh.png" alt="enter image description here"></a></p>
  
  <p>NEW UI</p>
  
  <p><a href="https://i.stack.imgur.com/ftvnf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ftvnf.png" alt="enter image description here"></a></p>
  
  <p>I want to change the background color of a button in MFC application. I have  created my user interface(UI) in MFC .I have added every controls from the toolbox.But the problem is that i want to change the background and foreground properties of a button and window.How it is possible?</p>
  
  <p>please help me to change the properties of controls in MFC.In windows application we can directly change the properties in the property window.
  But in the case of MFC application that is not possible.</p>
  
  <p>please help me..i have not enough experience in MFC application development.....
  Thanks in advance...................... </p>
  
  <h2>code from dialer.h</h2>
  
  <pre><code>   class CButtonDialer : public CButton
  {
  // Construction
  public:
      CButtonDialer();
  
  // Attributes
  public:
      CButton m_button;
  
  //  CButton IDC_KEY_1;
      CBrush m_brush;
  
  // Operations
  public:
  
  // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CButtonDialer)
      public:
      virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  
  
  // Implementation
  public:
      virtual ~CButtonDialer();
  // Generated message map functions
  protected:
      //{{AFX_MSG(CButtonDialer)
          // NOTE - the ClassWizard will add and remove member functions here.
      //}}AFX_MSG
      afx_msg BOOL OnEraseBkgnd(CDC* pDC);
      CFont m_FontLetters;
      CMapStringToString m_map;
      HTHEME m_hTheme; 
  
  
      void OpenTheme() { m_hTheme = OpenThemeData(m_hWnd, L"Button"); }
      void CloseTheme() {
          if (m_hTheme) { CloseThemeData(m_hTheme); m_hTheme = NULL; }
      }
      DECLARE_MESSAGE_MAP()
  
      virtual void PreSubclassWindow();
      afx_msg LRESULT OnThemeChanged();
      afx_msg void OnMouseMove(UINT,CPoint);
      afx_msg void OnSize(UINT type, int w, int h);
  };
  </code></pre>
  
  <h2>code from dialer.cpp file</h2>
  
  <pre><code>#include "stdafx.h"
  #include "ButtonDialer.h"
  #include "Strsafe.h"
  #include "const.h"
  
  /////////////////////////////////////////////////////////////////////////////
  // CButtonDialer
  
  CButtonDialer::CButtonDialer()
  {
      //255,255,255
      m_brush.CreateSolidBrush(
          (173, 41, 41));
      m_map.SetAt(_T("1"),_T(""));
      m_map.SetAt(_T("2"),_T("ABC"));
      m_map.SetAt(_T("3"),_T("DEF"));
      m_map.SetAt(_T("4"),_T("GHI"));
      m_map.SetAt(_T("5"),_T("JKL"));
      m_map.SetAt(_T("6"),_T("MNO"));
      m_map.SetAt(_T("7"),_T("PQRS"));
      m_map.SetAt(_T("8"),_T("TUV"));
      m_map.SetAt(_T("9"),_T("WXYZ"));
      m_map.SetAt(_T("0"),_T(""));
      m_map.SetAt(_T("*"),_T(""));
      m_map.SetAt(_T("#"),_T(""));
  }
  
  CButtonDialer::~CButtonDialer()
  {
      CloseTheme();
  }
  
  
  BEGIN_MESSAGE_MAP(CButtonDialer, CButton)
      ON_WM_THEMECHANGED()
      ON_WM_MOUSEMOVE()
      ON_WM_SIZE()
  END_MESSAGE_MAP()
  
  /////////////////////////////////////////////////////////////////////////////
  // CButtonDialer message handlers
  
  void CButtonDialer::PreSubclassWindow()
  {
      OpenTheme();
  
      HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
      LOGFONT lf;
      GetObject(hFont, sizeof(LOGFONT), &amp;lf);
      lf.lfHeight = 14;
      StringCchCopy(lf.lfFaceName,LF_FACESIZE,_T("Microsoft Sans Serif"));
      m_FontLetters.CreateFontIndirect(&amp;lf);
  
      DWORD dwStyle = ::GetClassLong(m_hWnd, GCL_STYLE);
      dwStyle &amp;= ~CS_DBLCLKS;
      ::SetClassLong(m_hWnd, GCL_STYLE, dwStyle);
  }
  
  LRESULT CButtonDialer::OnThemeChanged() 
  {
      CloseTheme();
      OpenTheme();
      return 0L;
  }
  
  void CButtonDialer::OnSize(UINT type, int w, int h)
  {
      CButton::OnSize(type, w, h);
  }
  
  void CButtonDialer::OnMouseMove(UINT nFlags,CPoint point)
  {
      CRect rect;
      GetClientRect(&amp;rect);
      if (rect.PtInRect(point)) {
          if (GetCapture() != this) {
              SetCapture();
              Invalidate();
          }
      }
      else {
          ReleaseCapture();
          Invalidate();
      }
      CButton::OnMouseMove(nFlags, point);
  }
  
  void CButtonDialer::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  {
      CDC dc;
      dc.Attach(lpDrawItemStruct-&gt;hDC);       //Get device context object
      CRect rt;
  
      rt = lpDrawItemStruct-&gt;rcItem;      //Get button rect
      dc.FillSolidRect(rt,dc.GetBkColor());
      dc.SetBkMode(TRANSPARENT);
  
      CRect rtl = rt;
      UINT state = lpDrawItemStruct-&gt;itemState;   //Get state of the button
  
      if (!m_hTheme) {
          UINT uStyle = DFCS_BUTTONPUSH;
          if ( (state &amp; ODS_SELECTED) ) {
              uStyle |= DFCS_PUSHED;
              rtl.left+=1;
              rtl.top+=1;
          }
          dc.DrawFrameControl(rt, DFC_BUTTON, uStyle);
      } else {
          UINT uStyleTheme = RBS_NORMAL;
          if ( (state &amp; ODS_SELECTED) ) {
              uStyleTheme = PBS_PRESSED;
          } else if (GetCapture()==this) {
              uStyleTheme = PBS_HOT;
          }
          DrawThemeBackground(m_hTheme, dc.m_hDC,
              BP_PUSHBUTTON, uStyleTheme,
              rt, NULL);
      }
  
      CString strTemp;
      GetWindowText(strTemp);     // Get the caption which have been set
  
      rtl.top += 4;
      CString letters;
      COLORREF crOldColor;
      if (m_map.Lookup(strTemp,letters)) {
          rtl.left+=15;
          dc.DrawText(strTemp,rtl,DT_LEFT|DT_TOP|DT_SINGLELINE);      // Draw out the caption
          HFONT hOldFont = (HFONT)SelectObject(dc.m_hDC, m_FontLetters);
          // Do your text drawing
          rtl.left += 13;
          rtl.top += 4;
          rtl.right -=4;
          crOldColor = dc.SetTextColor(RGB(148, 167, 70));
          dc.DrawText(letters,rtl,DT_LEFT | DT_TOP | DT_SINGLELINE);
          dc.SetTextColor(crOldColor);
          // Always select the old font back into the DC
          SelectObject(dc.m_hDC, hOldFont);
      } else {
          //127,127,127
          crOldColor = dc.SetTextColor(RGB(148, 167, 70));
          dc.DrawText(strTemp,rtl,DT_CENTER|DT_TOP|DT_SINGLELINE);        // Draw out the caption
          dc.SetTextColor(crOldColor);
      }
  
      if ( (state &amp; ODS_FOCUS ) )       // If the button is focused
      {
          int iChange = 3;
          rt.top += iChange;
          rt.left += iChange;
          rt.right -= iChange;
          rt.bottom -= iChange;
          dc.DrawFocusRect(rt);
      }
      dc.Detach();
  }
  
  BOOL CButtonDialer::OnEraseBkgnd(CDC* pDC)
  {
      CRect rect;
      GetClientRect(&amp;rect);
      //255,255,255
      CBrush myBrush(RGB(173, 41, 41));    // dialog background color
      CBrush *pOld = pDC-&gt;SelectObject(&amp;myBrush);
      BOOL bRes = pDC-&gt;PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
      pDC-&gt;SelectObject(pOld);    // restore old brush
      return bRes;                       // CDialog::OnEraseBkgnd(pDC);
  }
  </code></pre>