简体   繁体   English

使用水平滚动条手动设置MFC CComboBox下拉高度

[英]Manually set MFC CComboBox Dropdown height with Horizontal Scrollbar

I have a C++ MFC CComboBox (VS 2010) which users can enter text and click a "save" button, which inserts their text into the dropdown list for later recall/use. 我有一个C ++ MFC CComboBox(VS 2010),用户可以输入文本并单击“保存”按钮,该按钮会将其文本插入到下拉列表中,以供以后调用/使用。 When text is too long for the box I need to have a scrollbar, so I set WS_HSCROLL in the resource file and use m_Combo.SetHorizontalExtent(x), which works just fine. 当文本对于该框而言太长时,我需要具有一个滚动条,因此我在资源文件中设置了WS_HSCROLL并使用m_Combo.SetHorizo​​ntalExtent(x),效果很好。

The problem I have is that where there's a horizontal scroll, one line is covered by it and a vertical scroll bar appears to scroll to that one item. 我的问题是,在水平滚动的地方,一行覆盖着它,而垂直滚动条似乎可以滚动到该项目。 I have tried 我努力了

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)

and probably more the past few days but nothing seems to override the automatic sizing of the dropdown which doesn't account for the horizontal scroll. 并且可能在过去的几天中会更多,但似乎没有什么能覆盖下拉菜单的自动调整大小,而不会自动调整水平滚动。 I'm new to MFC and found these suggested online during desperate Google searches. 我是MFC的新手,在绝望的Google搜索中发现了建议的这些在线内容。

In short, is there a way to override the auto-height or extend it? 简而言之,是否有一种方法可以覆盖自动高度或将其延长? I know how to resize it in the resource editor but I want to resize in code during runtime and everything seems to be ignored. 我知道如何在资源编辑器中调整大小,但是我想在运行时调整代码大小,一切似乎都被忽略了。 Here are my functions from a test project that reproduced the error: 这是我的测试项目中再现错误的函数:

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?//
    ////////////////////////////////
}

Instead of adding a horizontal scrollbar and allowing scrolling if the dropped listbox is not wide enough, you can rather just set the width of the dropped listbox accordingly. 如果放置的列表框不够宽,则不必添加水平滚动条并允许滚动,而可以相应地设置放置的列表框的宽度。

Replace 更换

m_Combo.SetHorizontalExtent(x);

with

m_Combo.SetDroppedWidth(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM