Fixing Unreadable Window Titles in Maximized MFC Ribbon Bar Applications

If you use MFC to create an MFC ribbon bar application, you might notice that when you maximize your application, that the text in the window title bar has a different color compared to a non-maximized window. For example, here is a screenshot of a non-maximized default MFC ribbon bar application created by the Visual Studio project wizard:

When you maximize the window, the result could be as follows:

The text in the title bar becomes white, while it was black in the non-maximized window.
This is a long-standing bug in MFC. Luckily, there is an easy workaround for it. I’ll be using the Windows 7 MFC theme, but similar workarounds can be implemented for the other themes.

First, you need to derive your own class from CMFCVisualManagerWindows7 and override two methods: DrawNcText() and DrawTextOnGlass(). Here is the code:

#pragma once

class CMyVisualManager7 : public CMFCVisualManagerWindows7
{
	DECLARE_DYNCREATE(CMyVisualManager7)

	virtual void DrawNcText(CDC* pDC, CRect& rect, const CString&
		strTitle, BOOL bActive, BOOL bIsRTL, BOOL bTextCenter, BOOL bGlass = FALSE,
		int nGlassGlowSize = 0, COLORREF clrGlassText = (COLORREF)-1) override;

	virtual BOOL DrawTextOnGlass(CDC* pDC, CString strText, CRect rect,
		DWORD dwFlags, int nGlowSize = 0, COLORREF clrText = (COLORREF)-1) override;
};

And here is the implementation:

#include "stdafx.h"
#include "MyVisualManager7.h"

IMPLEMENT_DYNCREATE(CMyVisualManager7, CMFCVisualManagerWindows7)

void CMyVisualManager7::DrawNcText(CDC* pDC, CRect& rect, const CString&
	strTitle, BOOL bActive, BOOL bIsRTL, BOOL bTextCenter, BOOL bGlass /*= FALSE*/,
	int nGlassGlowSize /*= 0*/, COLORREF clrGlassText /*= (COLORREF)-1*/)
{
	if (bGlass && afxGlobalData.bIsWindows7)
	{
		// If we're drawing text on the DWM caption and Windows is Win7 or later, always use default text color
		// (in WinVista, the maximized "aero" caption was black):
		clrGlassText = (COLORREF)-1;
		nGlassGlowSize = 10;
	}

	__super::DrawNcText(pDC, rect, strTitle, bActive, bIsRTL,
		bTextCenter, bGlass, nGlassGlowSize, clrGlassText);
}

BOOL CMyVisualManager7::DrawTextOnGlass(CDC* pDC, CString strText, CRect rect,
	DWORD dwFlags, int nGlowSize /*= 0*/, COLORREF clrText /*= (COLORREF)-1*/)

{
	if (afxGlobalData.bIsWindows7)
	{
		// If we're on Windows 7 or later, always use default color.
		clrText = (COLORREF)-1;
		nGlowSize = 10;
	}

	return __super::DrawTextOnGlass(pDC, strText, rect, dwFlags,
		nGlowSize, clrText);
}

The overridden DrawNcText() method makes sure that the text for the window title is correct, while the overriden DrawTextOnGlass() method makes sure that Quick Access Toolbar controls with
text labels have the correct color.

Once you have your derived class, you have to use it. Your application probably contains something as follows:

CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows7));

Just replace CMFCVisualManagerWindows7 with your CMyVisualManager7:

CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMyVisualManager7));

That’s it.
Just compile and run your application. When you now maximize your window, the color in the title bar will be correct.
For example, here is the previous window maximized:

Looks much better 🙂

Share

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: