强制组合框“下拉"上面而不是下面 [英] Forcing a combobox to "dropdown" above instead of below

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

问题描述

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

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天全站免登陆