我应该如何更改 MFC 应用程序工具栏中按钮的图像(与类型无关)?

How should I change the IMAGE(doesn't matter the type) of the button in the toolbar in MFC applications?

我知道这是个愚蠢的问题,但在过去的 4 天里我一直坚持这个问题。为什么只修改MFC应用程序中的工具栏就这么复杂?

我创建新 Visual studio MFC 应用程序 基于对话框。我创建了新的 工具栏资源 。然后我应该如何设置图像(png、位图、jpeg...)或任何要在我的工具栏中使用的类型?

我已将大小设置为 w50 和 h50,我可以在按钮内部绘制。但是我找不到使用图像的方法。

我想使用 8 个位图格式和 png 格式的图像,而不是我刚刚尝试过的这 2 个按钮。我在某处读到 MFC 应用程序不支持 PNG,所以我转换为位图。

我在 OnInitDialog() 方法中像这样在对话框应用程序中加载我的工具栏:

    DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
    DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;
    CMFCToolBar::m_dblLargeImageRatio = 1;

    if (m_ToolBar.CreateEx(this, dwCtrlStyle, dwStyle, CRect(1, 1, 1, 1), IDR_TOOLBAR1))
    {
        dwStyle = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC;
        m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle() | dwStyle);
        CMFCToolBarInfo info;
        m_ToolBar.LoadToolBarEx(IDR_TOOLBAR1, info, FALSE);
        CSize   sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
        m_ToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy, SWP_NOACTIVATE | 
        SWP_NOZORDER);
        CPoint ptOffset(0, sizeToolBar.cy);
    }

如果有人能帮助我,我将不胜感激。图像格式无关紧要。我只想把图片放在工具栏里。

这是我目前的应用程序:

更新: 我试过这种方法。 ID_BUTTON_1 如果工具栏中的第一个按钮我试图更改它的图像。但是有了这个,第一个按钮所在的工具栏中就没有按钮了。我做错了什么?

VERIFY(m_toolbar.LoadBitmap(IDB_BITMAP1));
CMFCToolBarButton mbutton;
mbutton.SetImage(m_toolbar.GetImages()->GetCount() - 1);
m_toolbar.ReplaceButton(ID_BUTTON_1, CMFCToolBarButton(ID_BUTTON_1, 0));

我最近回答了这个问题,但找不到我的答案了。您可以为工具栏使用 PNG 图像。在引擎盖下,您仍然使用资源编辑器的 BMP 版本来创建事件处理程序等。但是您可以将 PNG 添加为资源,然后将其加载到对话框中。

例如,我在 OnInitDialog 函数中这样调用:

void CMeetingScheduleAssistantDlg::CreateToolbar()
{
    DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
    DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;

    CMFCToolBar::m_dblLargeImageRatio = 1; // AJT v20.1.7 Bug fix
    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);

    }
}

我不知道我之前的回答去了哪里,否则我会把它标记为重复的。 #confused.