强制组合框“下拉"到列表框.高于而不是低于 [英] Forcing a combobox to "dropdown" above instead of below

查看:106
本文介绍了强制组合框“下拉"到列表框.高于而不是低于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您单击组合框的下拉"按钮时,下拉列表框将显示在组合框的下方,除非下面没有足够的空间,在这种情况下,列表框将显示在上方 .

When you click on the "dropdown" button of a combobox, the dropped down listbox appears below the combobox, unless there is not enough space below, in which case the listbox appears above.

现在,我想知道是否有可能迫使lisbox出现在组合框的上方,即使下面有足够的空间.

Now I wonder if there is a possibility to force the lisbox to appear above the combobox, even if there is enough space below.

插图

当我单击组合框时,我希望下拉"列表框始终显示在左侧屏幕副本的上方.

When I click on the combo box, I'd like the "drop down" list box appear always above as on the left screen copy.

推荐答案

一切皆有可能,您无需从头开始"实现控件.

Everything is possible, and you don't need to implement the control "from scratch".

首先,您可以对ComboBox的ListBox部分进行子类化,以完全控制它,如 MSDN .您可以使用类向导创建从CListBox派生的类.您只需要在其中实现WM_WINPOSITIONCHANGING处理程序:

First, you can subclass the ListBox part of your ComboBox to get complete control over it, as explained in MSDN. You can create a class, derived from CListBox, using the Class Wizard. You only need to implement WM_WINPOSITIONCHANGING handler in it:

void CTopListBox::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
    CListBox::OnWindowPosChanging(lpwndpos);
    if ((lpwndpos->flags & SWP_NOMOVE) == 0)
    {
        lpwndpos->y -= lpwndpos->cy + 30;
    }
}

在这里,为简单起见,我将盒子向上移动(高度+30).您可以获取ComboBox的高度,而不是我的30.

Here, for simplicity, I am moving the box up by the (heights+30). You can get the height of your ComboBox instead of my 30.

然后在对话框类中声明一个成员变量:

Then you declare a member variable in your dialog class:

CTopListBox m_listbox;

并像这样子类化:

HBRUSH CMFCDlgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_LISTBOX)
    {
        if (m_listbox.GetSafeHwnd() == NULL)
        {
            m_listbox.SubclassWindow(pWnd->GetSafeHwnd());
            CRect r;
            m_listbox.GetWindowRect(r);
            m_listbox.MoveWindow(r);
        }
    }

    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
    return hbr;
}

请注意,我在那里打电话给m_listbox.MoveWindow(r);之所以需要它,是因为该列表框的第一条WM_CONTROLCOLOR消息是在放置后出现的,因此它第一次出现时是下拉而不是向上.

Note that I am calling m_listbox.MoveWindow(r) there; it is needed because first WM_CONTROLCOLOR message for that list box comes after it is positioned, so the very first time it would drop down instead of up.

免责声明:这不是一个很干净的解决方案,因为如果启用了Windows动画,您会看到列表从上到下展开.

Disclaimer: this is not a very clean solution, as, if you have windows animation enabled, you'd see that the list unrolls from top to bottom.

或者,您应该能够欺骗"组合框,使其过于靠近屏幕底部;然后它会自行下降.我把它留给读者练习:)

Alternatively, you should be able to "fool" the combobox that it is too close to the bottom of the screen; then it will drop up by itself. I leave it as an exercise for the readers :)

这篇关于强制组合框“下拉"到列表框.高于而不是低于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆