MFC功能区界面在本地化应用程序时给出断言失败

时间:2012-10-17 10:15:19

标签: c++ localization mfc ribbon assert

我有一个使用Office 2007功能区界面的基于MFC的应用程序。 MFC是静态链接的。

我正在尝试添加日语本地化。我在一个单独的DLL中有本地化的资源。我正在InitInstance开头加载资源DLL:

VERIFY(hRes = LoadLibrary(_T("JapaneseLang.dll")));
if(hRes)
    AfxSetResourceHandle(hRes);

这导致CMFCVisualManagerOffice2007::OnUpdateSystemColors

处的断言失败
#if !defined _AFXDLL
        TRACE(_T("\r\nImportant: to enable the Office 2007 look in static link,\r\n"));
        TRACE(_T("include afxribbon.rc from the RC file in your project.\r\n\r\n"));
        ASSERT(FALSE);
#endif

但我确实在DLL和EXE的rc文件中都包含afxribbon.rc


我还发现了similar question asked at tech-archive.net,其中列出了possible solution

  

现在我找到错误位置。这个shoule是新mfc的错误   版本

     

在CMFCVisualManagerOffice2007中,当样式发生变化时,   函数SetStyle的CMFCVisualManagerOffice2007自动调用FreeLibrary   释放DLL,所以错误发生了。

     

现在我从CMFCVisualManagerOffice2007派生了一个类并添加了一个   静态函数通过执行来设置成员变量m_bAutoFreeRes   这个应用程序可以正常运行;见吼叫。

     

类CMFCVisualExtManagerOffice2007:public   CMFCVisualManagerOffice2007 {   DECLARE_DYNCREATE(CMFCVisualExtManagerOffice2007)public:   CMFCVisualExtManagerOffice2007();虚拟   〜CMFCVisualExtManagerOffice2007();

     

static void SetAutoFreeRes(BOOL bAutoFree = FALSE){m_bAutoFreeRes =   bAutoFree; }};


但是我无法理解究竟是什么导致问题,以及此解决方案的工作原理。此外,我不确定这是否是一个正确的解决方案。有谁知道究竟是什么导致了这个问题,以及该解决方案的工作原理?

1 个答案:

答案 0 :(得分:0)

我弄清楚解决方案的工作原理。每次调用m_bAutoFreeRes后,我都需要将CMFCVisualManagerOffice2007::SetStyle设置为false。

class CMFCVisualExtManagerOffice2007 : public CMFCVisualManagerOffice2007
{
    DECLARE_DYNCREATE(CMFCVisualExtManagerOffice2007)
public:
    CMFCVisualExtManagerOffice2007();
    virtual ~CMFCVisualExtManagerOffice2007();

    static void SetAutoFreeRes(BOOL bAutoFree = FALSE)
    {
        m_bAutoFreeRes = bAutoFree;
    }
};

然后在主题之间切换时

    switch (m_nAppLook)
    {
    case ID_VIEW_APPLOOK_OFF_2007_BLUE:
        CMFCVisualManagerOffice2007::SetStyle (CMFCVisualManagerOffice2007::Office2007_LunaBlue);
        CMFCVisualExtManagerOffice2007::SetAutoFreeRes(FALSE);
        break;

    case ID_VIEW_APPLOOK_OFF_2007_BLACK:
        CMFCVisualManagerOffice2007::SetStyle (CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);
        CMFCVisualExtManagerOffice2007::SetAutoFreeRes(FALSE);
        break;

    case ID_VIEW_APPLOOK_OFF_2007_SILVER:
        CMFCVisualManagerOffice2007::SetStyle (CMFCVisualManagerOffice2007::Office2007_Silver);
        CMFCVisualExtManagerOffice2007::SetAutoFreeRes(FALSE);
        break;

    case ID_VIEW_APPLOOK_OFF_2007_AQUA:
        CMFCVisualManagerOffice2007::SetStyle (CMFCVisualManagerOffice2007::Office2007_Aqua);
        CMFCVisualExtManagerOffice2007::SetAutoFreeRes(FALSE);
        break;
    }