首页 > 解决方案 > 使用 LoadToolbarEx 解决异常以及正常与大图像计数之间的差异

问题描述

我刚刚尝试CDialog在调试中运行我的应用程序,但出现异常。它与我创建 myu 工具栏有关。

我把这个提到OnInitDialog

void CMeetingScheduleAssistantDlg::CreateToolbar()
{
    DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
    DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;
    if (m_ToolBar.CreateEx(this, dwCtrlStyle,
        dwStyle, CRect(1, 1, 1, 1), IDR_TOOLBAR))
    {
        dwStyle = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC;
        m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle() | dwStyle);

        CMFCToolBarInfo info;

        info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
        info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
        info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
        info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;

        m_ToolBar.LoadToolBarEx(IDR_TOOLBAR, info, FALSE);

        CSize   sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
        m_ToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
            SWP_NOACTIVATE | SWP_NOZORDER);

        // Move all controls down
        CPoint ptOffset(0, sizeToolBar.cy);

        CRect  rcChild;
        CWnd* pwndChild = GetWindow(GW_CHILD);
        while (pwndChild)
        {
            if (pwndChild->GetSafeHwnd() != m_ToolBar.GetSafeHwnd())
            {
                pwndChild->GetWindowRect(rcChild);
                ScreenToClient(rcChild);
                rcChild.OffsetRect(ptOffset);
                pwndChild->MoveWindow(rcChild, FALSE);
            }
            pwndChild = pwndChild->GetNextWindow();
        }

        // Resize the window
        CRect rcWindow;
        GetWindowRect(rcWindow);
        rcWindow.bottom += sizeToolBar.cy;
        MoveWindow(rcWindow, FALSE);

    }
}

例外是在这一行:

LoadToolBarEx

当我跟踪代码时,我最终得到了这个ASSERT

// Load large images:
if (params.m_uiLargeHotResID != 0)
{
    if (!m_LargeImages.Load(params.m_uiLargeHotResID, NULL, TRUE))
    {
        return FALSE;
    }

    ASSERT(m_Images.GetCount() == m_LargeImages.GetCount());
}

当我查看这些变量时:

这是调用堆栈:

>   Meeting Schedule Assistant.exe!CMFCToolBar::LoadBitmapEx(CMFCToolBarInfo & params, int bLocked) Line 781    C++
    Meeting Schedule Assistant.exe!CMFCToolBar::LoadToolBarEx(unsigned int uiToolbarResID, CMFCToolBarInfo & params, int bLocked) Line 872  C++
    Meeting Schedule Assistant.exe!CMeetingScheduleAssistantDlg::CreateToolbar() Line 2288  C++
    Meeting Schedule Assistant.exe!CMeetingScheduleAssistantDlg::OnInitDialog() Line 246    C++
    Meeting Schedule Assistant.exe!AfxDlgProc(HWND__ * hWnd, unsigned int message, unsigned __int64 __formal, __int64 __formal) Line 28 C++
    [External Code] 
    Meeting Schedule Assistant.exe!CWnd::DefWindowProcW(unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 1100   C++
    Meeting Schedule Assistant.exe!CWnd::Default() Line 299 C++
    Meeting Schedule Assistant.exe!CDialog::HandleInitDialog(unsigned __int64 __formal, __int64 __formal) Line 721  C++
    Meeting Schedule Assistant.exe!CWnd::OnWndMsg(unsigned int message, unsigned __int64 wParam, __int64 lParam, __int64 * pResult) Line 2441   C++
    Meeting Schedule Assistant.exe!CWnd::WindowProc(unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 2099    C++
    Meeting Schedule Assistant.exe!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 265  C++
    Meeting Schedule Assistant.exe!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 418   C++
    [External Code] 
    Meeting Schedule Assistant.exe!IsolationAwareCreateDialogIndirectParamW(HINSTANCE__ * hInstance, const DLGTEMPLATE * lpTemplate, HWND__ * hWndParent, __int64(*)(HWND__ *, unsigned int, unsigned __int64, __int64) lpDialogFunc, __int64 dwInitParam) Line 569 C++
    Meeting Schedule Assistant.exe!CWnd::CreateDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 358   C++
    Meeting Schedule Assistant.exe!CWnd::CreateRunDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 460    C++
    Meeting Schedule Assistant.exe!CDialog::DoModal() Line 633  C++
    Meeting Schedule Assistant.exe!CMeetingScheduleAssistantApp::InitInstance() Line 251    C++
    Meeting Schedule Assistant.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 37  C++
    Meeting Schedule Assistant.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 26    C++
    [External Code] 

我的主要 PNG 工具栏资源有 10 张图片。然而我指定了这段代码:

info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;

我的工具栏图像是 320 x 32。

我该如何处理这个异常?

标签: c++mfctoolbar

解决方案


我想我在这里找到了答案。

CMFCToolBar::m_dblLargeImageRatio

指定大图像的尺寸(高度或宽度)与常规图像的尺寸之间的比率。

备注 默认比率为 2。您可以更改此值以使较大的工具栏图像变大或变小。

当您不指定一组大图像时,框架会使用此数据成员。例如,如果您只提供大小为 16x16 的小图像集,并且希望大图像的大小为 24x24,则将此数据成员设置为 1.5。

CMFCToolBar::m_dblLargeImageRatio = 1.0;

推荐阅读