如何在CTreeCtrl中显示自定义工具提示? [英] How do I display custom tooltips in a CTreeCtrl?

查看:295
本文介绍了如何在CTreeCtrl中显示自定义工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从CTreeCtrl派生的类.在OnCreate()中,我用自定义对象替换了默认的CToolTipCtrl对象:

I have a class derived from CTreeCtrl. In OnCreate() I replace the default CToolTipCtrl object with a custom one:

int CMyTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Replace tool tip with our own which will
    // ask us for the text to display with a TTN_NEEDTEXT message
    CTooltipManager::CreateToolTip(m_pToolTip, this, AFX_TOOLTIP_TYPE_DEFAULT);
    m_pToolTip->AddTool(this, LPSTR_TEXTCALLBACK);
    SetToolTips(m_pToolTip);

    // Update: Added these two lines, which don't help either
    m_pToolTip->Activate(TRUE);
    EnableToolTips(TRUE);

    return 0;
}

我的消息处理程序如下:

My message handler looks like this:

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, &CMyTreeCtrl::OnTtnNeedText)

但是我从未收到TTN_NEEDTEXT消息.我看过Spy ++,看起来好像永远都不会发送此消息.

However I never receive a TTN_NEEDTEXT message. I had a look with Spy++ and it also looks like this message never gets sent.

这里可能是什么问题?

我不确定这是否相关:CTreeCtrl的父窗口的类型为CDockablePane.要进行这项工作,还需要一些额外的工作吗?

I'm not sure whether this is relevant: The CTreeCtrl's parent window is of type CDockablePane. Could there be some extra work needed for this to work?

推荐答案

最后!我(部分)解决了它:

Finally! I (partially) solved it:

看起来CDockablePane父窗口确实导致了此问题...

It looks like the CDockablePane parent window indeed caused this problem...

首先,我从CTreeCtrl派生的类中删除了所有特定于工具提示的代码.一切都在父窗格窗口中完成.

First I removed all the tooltip-specific code from the CTreeCtrl-derived class. Everything is done in the parent pane window.

然后我编辑了父窗口的OnCreate()方法:

Then I edited the parent window's OnCreate() method:

int CMyPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDockablePane::OnCreate(lpCreateStruct) == -1)
        return -1;

const DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
    TVS_CHECKBOXES | TVS_DISABLEDRAGDROP | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT |
    TVS_INFOTIP | TVS_NOHSCROLL | TVS_SHOWSELALWAYS;

// TREECTRL_ID is a custom member constant, set to 1
if(!m_tree.Create(dwStyle, m_treeRect, this, TREECTRL_ID ) )
{
    TRACE0("Failed to create trace tree list control.\n");
    return -1;
}

// m_pToolTip is a protected member of CDockablePane
m_pToolTip->AddTool(&m_tree, LPSTR_TEXTCALLBACK, &m_treeRect, TREECTRL_ID);
m_tree.SetToolTips(m_pToolTip);


return 0;

}

不幸的是,我们不能简单地用更少的参数调用AddTool(),因为如果没有设置工具ID,基类将以ASSERT的形式抱怨uFlag成员. 并且由于我们需要设置ID,因此我们还需要设置一个矩形.我创建了一个CRect成员,并将其设置为CTor中的(0, 0, 10000, 10000).我还没有找到改变工具的矩形尺寸的可行方法,因此这是我非常难看的解决方法.这也是为什么我将此解决方案称为部分解决方案的原因. 更新:我问了一个与此相关的问题.

Unforunately we cannot simply call AddTool() with less parameters because the base class will complain in the form of an ASSERT about a uFlag member if there is no tool ID set. And since we need to set the ID, we also need to set a rectangle. I created a CRect member and set it to (0, 0, 10000, 10000) in the CTor. I have not yet found a working way to change the tool's rect size so this is my very ugly workaround. This is also why I call this solution partial. Update: I asked a question regarding this.

最后有一个处理程序来获取工具提示信息:

Finally there is the handler to get the tooltip info:

// Message map entry
ON_NOTIFY(TVN_GETINFOTIP, TREECTRL_ID, &CMobileCatalogPane::OnTvnGetInfoTip)


// Handler
void CMyPane::OnTvnGetInfoTip(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMTVGETINFOTIP>(pNMHDR);

    // This is a CString member
    m_toolTipText.ReleaseBuffer();
    m_toolTipText.Empty();

    // Set your text here...

    pGetInfoTip->pszText = m_toolTipText.GetBuffer();

    *pResult = 0;
}

这篇关于如何在CTreeCtrl中显示自定义工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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