Why isn't the dropdown arrow drawn for an CMFC

2020-07-18 03:37发布

问题:

I ran into this issue when trying to add a CMFCMenuButton to an existing MFC application. It worked properly, and even resized the button to accommodate the dropdown arrow. But it didn't draw the dropdown arrow, and when I hovered over the button, I saw the following debug output:

> Can't load bitmap: 42b8.GetLastError() = 716
> CMenuImages. Can't load menu images 3f01

It turns out that even with Visual Studio 2010 RTM, when you create a brand new MFC Dialog based application, the CMFCMenuButton doesn't draw the arrow and shows the same errors. Initially I assumed that I didn't have something installed or registered correctly. However, the NewControls example from the MFC Feature Pack showed the dropdown arrow perfectly.

What is missing?

回答1:

The reason I posted this question is because I couldn't find any answers via Google. The closest I came when researching it was a couple hacks that didn't seem to be the real solution. After pouring over the NewControls example, I finally found the culprit.

At the bottom of the default .rc file for a project, there is the following code:

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#include "res\YOUR_PROJECT_NAME.rc2"  // non-Microsoft Visual C++ edited resources
#include "afxres.rc"      // Standard components
#endif

The NewControls example's .rc file looks like this:

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#include "res\NewControls.rc2"  // non-Microsoft Visual C++ edited resources
#include "afxres.rc"      // Standard components
#ifndef _AFXDLL
#include "afxribbon.rc"      // Ribbon and control bars
#endif
#endif

Adding the afxribbon.rc enables the bitmap resources needed for the controls in the MFC Feature pack update. Now you can't just simply add the missing code to the bottom of the .rc file. If you do that, every time you edit the resource file using the visual designer, your added code will be removed. The solution to the problem is to add this to the bottom of the YOUR_PROJECT_NAME.rc2 file:

#ifndef _AFXDLL
#include "afxribbon.rc"      // Ribbon and control bars
#endif

Make sure you have an empty line at the bottom of the file, or the resource compiler will complain. I'm not sure what setting needs to be adjusted in order for the visual designer to automatically include afxribbon.rc like it does in the NewControls example project. But adding it to the .rc2 seems to fix the problem.


Update

Keep in mind that you can use the IDE to modify your RC file:

  • Right-Click the RC file and select Resource Includes...:

  • Paste the new code into the Compile-time directives area:



回答2:

I solve this problem for myself in such way: I add a clause to CMyApp::InitInstance:

BOOL CMyApp::InitInstance()
{
    CWinAppEx::InitInstance();

    InitCommonControls();

    //This! 
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

    //...
    return TRUE;
}