【问题标题】:Manually set MFC CComboBox Dropdown height with Horizontal Scrollbar使用水平滚动条手动设置 MFC CComboBox 下拉高度
【发布时间】:2012-06-06 19:23:35
【问题描述】:

我有一个 C++ MFC CComboBox (VS 2010),用户可以输入文本并单击“保存”按钮,将文本插入下拉列表以供以后调用/使用。当文本对于框来说太长时,我需要一个滚动条,所以我在资源文件中设置了 WS_HSCROLL 并使用 m_Combo.SetHorizo​​ntalExtent(x),效果很好。

我遇到的问题是,在有水平滚动的地方,一条线被它覆盖,并且垂直滚动条似乎滚动到该项目。我试过了

m_Combo.MoveWindow(&rctDropDown) //rctDropDown was first pulled out and modified
::SetWindowPos() //called after modifying values from ::GetWindowRect()
r.OffsetRect() //where r is from m_Combo.GetDroppedControlRect(&r)

过去几天可能更多,但似乎没有什么可以覆盖不考虑水平滚动的下拉菜单的自动调整大小。我是 MFC 的新手,在绝望的 Google 搜索过程中在网上找到了这些建议。

简而言之,有没有办法覆盖自动高度或扩展它?我知道如何在资源编辑器中调整它的大小,但我想在运行时调整代码大小,一切似乎都被忽略了。这是我在重现错误的测试项目中的函数:

void CtestDlg::StoreClicked()
{
    CString l;
    m_Combo.GetWindowText(l);
    m_Combo.InsertString(0, l);
    m_Combo.SetCurSel(0);
    UpdateList();
}

void CtestDlg::UpdateList()
{
    // Find the longest string in the list box.
    CString     str;
    CSize       sz;
    TEXTMETRIC  tm;
    CDC*        pDC = m_Combo.GetDC();
    CFont*      pFont = m_Combo.GetFont();

    int         x = 0;
    int         y = 0;

    // Select the listbox font, save the old font
    CFont* pOldFont = pDC->SelectObject(pFont);
    // Get the text metrics for avg char width
    pDC->GetTextMetrics(&tm); 

    for(int i = 0; i < m_Combo.GetCount(); i++)
    {
        m_Combo.GetLBText(i, str);
        sz = pDC->GetTextExtent(str);

        // Add the avg width to prevent clipping
        sz.cx += tm.tmMaxCharWidth;

        m_Combo.SetItemHeight(i, sz.cy);

        if (sz.cx > x)
            x = sz.cx;

        y += sz.cy;
    }
    // Select the old font back into the DC
    pDC->SelectObject(pOldFont);
    m_Combo.ReleaseDC(pDC);
    m_Combo.SetHorizontalExtent(x);

    ////////////////////////////////
    //manually change height here?//
    ////////////////////////////////
}

【问题讨论】:

  • 你好 mwilliams,你有没有按照你的意愿让它工作?
  • 不,我认为这是 MFC 的限制。

标签: c++ drop-down-menu mfc height ccombobox


【解决方案1】:

如果拖放的列表框不够宽,则无需添加水平滚动条并允许滚动,您可以相应地设置拖放列表框的宽度。

替换

m_Combo.SetHorizontalExtent(x);

m_Combo.SetDroppedWidth(x);

【讨论】:

  • 谢谢,我知道这个解决方法。如果我不能让它工作,那是我的备份,但这是一个非常小的显示器,滚动更实用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多