如何在垂直停靠的 CMFCToolBar 中显示整个 CMFCToolBarComboBoxButton不是图标

问题描述

我有一个 MFC 应用程序(具有从 CWinApp 派生的应用程序类),并且有一个从 CToolBar 派生的工具栏类。当此工具栏停靠在左侧或右侧时,它仍会显示嵌入其中的整个组合框(不是图标)。

enter image description here

由于以下 CMyToolBar 类,这种行为是可能的:

class CMyToolBar : public CToolBar
{
public:
    CMyToolBar();

protected:
    afx_msg void OnPaint();
    afx_msg void OnMove(int x,int y);
    afx_msg LONG OnCheckUpdate(UINT uParam,LONG lParam);
    DECLARE_MESSAGE_MAP()
};

const int COMBOBOX_POS = 8;
const int COMBOBOX_WIDTH = 180;

BEGIN_MESSAGE_MAP(CMyToolBar,CToolBar)
    ON_WM_PAINT()
    ON_WM_MOVE()
    ON_MESSAGE(WM_USER + 3,OnCheckUpdate)
END_MESSAGE_MAP()

CMyToolBar::CMyToolBar() {}

void CMyToolBar::OnPaint()
{
    Default();

    CMainFrame* pMainFrame = dynamic_cast<CMainFrame*>(AfxGetMainWnd());
    if (pMainFrame != NULL && ::IsWindow(pMainFrame->m_comboBox.m_hWnd))
    {
        PostMessage(WM_USER + 3,0);
    }
}

LONG CMyToolBar::OnCheckUpdate(UINT uParam,LONG lParam)
{
    CMainFrame* pMainFrame = dynamic_cast<CMainFrame*>(AfxGetMainWnd());

    if (pMainFrame != NULL && ::IsWindow(pMainFrame->m_comboBox.m_hWnd))
    {
        CRect toolBarRect,comboBoxRect;
        GetClientRect(&toolBarRect);
        GetItemRect(COMBOBOX_POS,&comboBoxRect);

        bool bChanged = false;
        if (toolBarRect.Height() > comboBoxRect.Height() * 2)
        {
            if (comboBoxRect.Width() != COMBOBOX_WIDTH)
            {
                SetButtonInfo(COMBOBOX_POS,ID_COMBOBOX,TBBS_SEPARATOR,COMBOBOX_WIDTH);

                CalcDynamicLayout(0,LM_HORZ | LM_MRUWIDTH | LM_COMMIT);
                bChanged = true;
            }
        }
        else
        {
            if (comboBoxRect.Width() != COMBOBOX_WIDTH)
            {
                SetButtonInfo(COMBOBOX_POS,COMBOBOX_WIDTH);
                CalcDynamicLayout(0,LM_HORZ | LM_HORZDOCK | LM_COMMIT);
                bChanged = true;
            }
        }

        if (bChanged || uParam == 1)
        {
            CRect newComboBoxRect;
            GetItemRect(COMBOBOX_POS,&newComboBoxRect);
            pMainFrame->m_comboBox.MoveWindow(newComboBoxRect,FALSE);
            pMainFrame->ShowControlBar(this,FALSE,TRUE,FALSE);
        }
    }

    return 0L;
}

void CMyToolBar::OnMove(int x,int y)
{
    CToolBar::OnMove(x,y);

    CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

    if (pMainFrame != NULL && ::IsWindow(pMainFrame->m_comboBox.m_hWnd))
    {
        PostMessage(WM_USER + 3,1);
    }
}

使用 CMFCToolBar 实现此类代码的所有尝试都不起作用(创建了带有 CWinAppEx 的应用程序,重写了 OnResetToolBar,从CMFCToolBar 已实施(具有相同的(或多或少)OnPaintOnMoveCheckUpdate,其所有CalcFixedLayout()AdjustSize() 等等),但这并没有带来积极的结果)。

如何实现 CMFCToolBar 派生类来实现所需的行为? (或者可能有必要做一些其他的事情?)


环境
操作系统 Windows 10 x64
Visual Studio Microsoft Visual Studio 社区 2019(版本 16.8.3)
Windows SDK 版本 10.0
平台工具集 Visual Studio 2019 (v142)

这里是项目的链接,方便复现(附复现步骤):
A project with CToolBar
A project with CMFCToolBar


附注 以下决定不是我需要的。

MFC: How do you get CMFCToolBarComboBoxButton to show on a CMFCToolBar?

How to SetOriginalState of a dynamically created CMFCToolbar?

How to SetOriginalState of a dynamically created CMFCToolbar?

Adding a check combobox to an CMFCToolbar

How to make CMFCToolBarComboBoxButton able to works in vertical mode?

How to change CMFCToolBar runtime

CMFCStatusBar change height when the icon is added

How to change CComboBox edit control's height?

How to set size and Transparent / Clear CMFCToolBar Button and Icon in mfc?

CMFCToolBar & CMFCMenuBar don't show dynamic items by default

CMFCToolBarDateTimeCtrl - appearance

How to add custom control in MFCtoolbar

How to change button position in CMFCToolBar?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...