如何更改mfc应用程序的背景属性

时间:2018-05-03 13:10:45

标签: visual-c++ visual-studio-2015 mfc

CURRENT UI

enter image description here

新用户界面

enter image description here

我想更改MFC应用程序中按钮的背景颜色。我已经在MFC中创建了我的用户界面(UI)。我已经添加了工具箱中的每个控件。但问题是我想要更改按钮和窗口的背景和前景属性。如何实现?

请帮我改变MFC中控件的属性。在Windows应用程序中,我们可以直接更改属性窗口中的属性。 但是在MFC应用的情况下是不可能的。

请帮助我..我在MFC应用程序开发方面经验不足..... 提前致谢......................

来自dialer.h的

代码

   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);
};
来自dialer.cpp文件的

代码

#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), &lf);
    lf.lfHeight = 14;
    StringCchCopy(lf.lfFaceName,LF_FACESIZE,_T("Microsoft Sans Serif"));
    m_FontLetters.CreateFontIndirect(&lf);

    DWORD dwStyle = ::GetClassLong(m_hWnd, GCL_STYLE);
    dwStyle &= ~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(&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->hDC);       //Get device context object
    CRect rt;

    rt = lpDrawItemStruct->rcItem;      //Get button rect
    dc.FillSolidRect(rt,dc.GetBkColor());
    dc.SetBkMode(TRANSPARENT);

    CRect rtl = rt;
    UINT state = lpDrawItemStruct->itemState;   //Get state of the button

    if (!m_hTheme) {
        UINT uStyle = DFCS_BUTTONPUSH;
        if ( (state & ODS_SELECTED) ) {
            uStyle |= DFCS_PUSHED;
            rtl.left+=1;
            rtl.top+=1;
        }
        dc.DrawFrameControl(rt, DFC_BUTTON, uStyle);
    } else {
        UINT uStyleTheme = RBS_NORMAL;
        if ( (state & 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 & 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(&rect);
    //255,255,255
    CBrush myBrush(RGB(173, 41, 41));    // dialog background color
    CBrush *pOld = pDC->SelectObject(&myBrush);
    BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
    pDC->SelectObject(pOld);    // restore old brush
    return bRes;                       // CDialog::OnEraseBkgnd(pDC);
}

2 个答案:

答案 0 :(得分:1)

您需要为WM_ERASEBKGND message提供一个消息处理程序并自己绘制背景。

答案 1 :(得分:0)

  1. 在资源编辑器上放置一个普通按钮。
  2. 在您使用表单的.h文件中,声明CMFCButton变量,如m_btnRead
  3. 在表单的DoDataExchange方法中添加一行

    DDX_Control(pDX, IDC_BUTTON_READ, m_btnRead);
    
  4. 在初始化表单的方法(OnInitDialogCreate等)上添加一行

    m_btnRead.SetFaceColor(RGB(0, 255, 255));
    
  5. 现在你已经完成了!