树ComboBox控件

介绍 最近,我需要一 个树控件在一个组合框内,在我搜索谷歌期间,我发现了一些东西,但它比我需要的稍微复杂一点。我开始自己起草,并与您分享结果。 使用的代码 一切都基于CTreeComboBox,它是从标准的CComboBox派生出来的。这里我有一个CComboTreeCtrl变量成员和树控件的方法特征。在CTreeComboBox::PreSubclassWindow中,我创建和初始化m_Tree并在combobox下拉列表上拉伸。

void CTreeComboBox::PreSubclassWindow() 
{
    // TODO: Add your specialized code here and/or call the base class

    CComboBox::PreSubclassWindow();

    CRect rect(0, 0, 0, 0);
    DWORD dwStyle =  WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | 
    TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | 
    TVS_CHECKBOXES;
    m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
    m_Tree.Init(this);

    GetClientRect(rect);
    SetDroppedWidth(rect.Width());
    SetDroppedHeight(m_droppedHeight);

    dwStyle = 0x03 & GetStyle();
    ASSERT(dwStyle == CBS_DROPDOWNLIST);
}

首先点击,或按下箭头键,树窗口将打开。

void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default

    m_bTree = ! m_bTree;
    if(m_bTree)DisplayTree();

//    CComboBox::OnLButtonDown(nFlags, point);
}

和:

BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class

    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
    {
        DisplayTree();
        return TRUE;
    }

    return CComboBox::PreTranslateMessage(pMsg);
}

但树窗口应该关闭在第二次点击,escape键,Enter键,或失去焦点。我尝试了几种方法,但都失败了,所以我的解决方案是发送给父(CTreeComboBox)一个消息来关闭。

LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
{
    TreeCtrlDone();
    m_Tree.ShowWindow(SW_HIDE);
    m_bTree = FALSE;

    return 1;
}

ComboBox必须用CBS_DROPDOWNLIST设置,否则将断言:) 为了使用这个控件,您需要#include“TreeComboBox”。h”,并进一步将combobox处理为树形控件(您可以在示例项目中看到如何处理)。 享受它! 历史 2011年4月26日:初始版本。2012年2月20日:将CTreeCtrl和GetTreeCtrl()方法添加到CTreeComboBox类中,因此现在它具有对树控件的完全访问权。2013年5月10日:更新控制档案。2014年10月16日:按屏幕位置下降(下降或上升);下拉键不能被Alt+F4关闭。2014年11月14日:为了在更新版本的Visual Studio(例如VS2008)上使用,我更新了代码。2014年11月18日:我增加了combobox可以通过F4下降的可能性。我添加了CComboTreeCtrlExt类,以便自定义树控件的combobox没有混乱的CComboTreeCtrl类。 本文转载于:http://www.diyabc.com/frontweb/news374.html

原文地址:https://www.cnblogs.com/Dincat/p/13443924.html