简体   繁体   中英

MFC VC++: CMFCButton::SetFaceColor does not change background(face) color

I have a CMFCButton on a CFormView. I want it to have a particular background color. I came across CMFCButton::SetFaceColor which I thought would do that. But unfortunately it didnt work. May be I'm misunderstanding the "Face" terminology here. Is that the case?

Here's my code:

m_btnCopy.SetFaceColor(RGB(255,0,0),true);
m_btnCopy.SetTextColor(RGB(0,0,255));

and output:

在此处输入图片说明

This setting is quite archaic and is ignored by modern theme engines. Most likely your application was generated by Visual Studio wizard and contains something like:

CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

You can try to remove this line to disable custom MFC themes but the button will not look good anyway, after SetFaceColor it will be drawn in classic theme.

almost same method but slightly better look.

  m_btnCopy.EnableWindowsTheming(FALSE);
  m_btnCopy.m_nFlatStyle = CMFCButton::BUTTONSTYLE_FLAT;
  m_btnCopy.m_bTransparent = false;
  m_btnCopy.SetFaceColor(RGB(255,0,0),true);
  m_btnCopy.SetTextColor(RGB(0,0,255));

Other people suggest to use m_bDontUseWinXPTheme = TRUE. It works due to the following code in afxbutton.cpp:

void CMFCButton::DrawBorder(CDC* pDC, CRect& rectClient, UINT uiState)
{
   <snip>

   // Draw 3d border:
   if (m_nFlatStyle != BUTTONSTYLE_NOBORDERS)
   {
       if (m_bWinXPTheme && !m_bDontUseWinXPTheme && CMFCVisualManager::GetInstance()->DrawPushButtonWinXP(pDC, rectClient, this, uiState))

The 'DrawPushButtonWinXP' isn't invoked anymore when m_bDontUseWinXPTheme is set. You get though an ugly old style button.

试试这些代码。

mCMFCBtn.m_bGrayDisabled = FALSE;

mCMFCBtn.m_bDontUseWinXPTheme = TRUE;

mCMFCBtn.m_nFlatStyle = CMFCButton::BUTTONSTYLE_FLAT;

mCMFCBtn.SetFaceColor(RGB(255, 0, 0), TRUE);

mCMFCBtn.SetTextColor(RGB(0, 0, 0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM