0

In a standard C++/MFC MDI doc/view project, I want to implement a tracking tooltip in the view (the tabbed view windows which generally occupy most of the main frame window). So, in class MyAppView, I have a member CToolTipCtrl tooltip. Function MyAppView::OnInitialUpdate() contains the initialization

BOOL ok0 = tooltip.Create(this, TTS_ALWAYSTIP);
CRect clientRect; GetClientRect(&clientRect);
BOOL ok2 = tooltip.AddTool(this, LPSTR_TEXTCALLBACK, &clientRect, 1234/*tool ID*/);
tooltip.Activate(TRUE);

to make the entire client area of the view be the "tool". The message map contains an entry

ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnNeedToolTipText)

and the function OnNeedToolTipText is defined as

BOOL MyAppView::OnNeedToolTipText(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
    UNREFERENCED_PARAMETER(id);

    NMTTDISPINFO *pTTT = (NMTTDISPINFO *)pNMHDR;
    UINT_PTR nID = pNMHDR->idFrom;
    BOOL bRet = FALSE;
    if(nID == 1234)
    {
        // Come here when text is needed for tracking tooltip
    }
    if(pTTT->uFlags & TTF_IDISHWND)
    {
        // idFrom is actually the HWND of the tool
        nID = ::GetDlgCtrlID((HWND)nID);
        if(nID)
        {
            _stprintf_s(pTTT->szText, sizeof(pTTT->szText) / sizeof(TCHAR),
              _T("Control ID = %d"), nID);
            pTTT->hinst = AfxGetResourceHandle();
            bRet = TRUE;
        }
    }

    *pResult = 0;

    return bRet;
}

What happens is that only placing the mouse on the menu items (File, Edit, View, Window, Help) causes the code to enter OnNeedToolTipText, with an ID of 0-5. Moving the mouse into the client area (the view) does nothing.

How can I get the tooltip to appear in the client area of the view only?

Visual Studio 2017; C++; 64-bit Windows 7

4

4 回答 4

0

所以我回去看看我可能会错过什么。我在 10 多年前写了这些东西。我还覆盖了一个 CWnd 成员

virtual INT_PTR OnToolHitTest( CPoint point, TOOLINFO* pTI ) const;

和:

INT_PTR HERichView::OnToolHitTest( CPoint point, TOOLINFO* pTI ) const
{
    pTI->hwnd = m_hWnd;
    pTI->uId = point.x + ( point.y << 16 );
    CRect rect;
    GetClientRect( rect );
    pTI->rect= rect;
    pTI->lpszText= LPSTR_TEXTCALLBACK;
    return pTI->uId;
}

我查了一下,没有这个就不行。所以你的:

ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTip )

如果您添加上述内容,应该会被调用。而且只有EnableToolTips( );应该需要。

于 2017-08-09T22:57:11.587 回答
0

如果您想在视图中显示跟踪工具提示,请按照以下步骤操作:

创建工具提示并添加工具。

void CToolTipDemoView::OnInitialUpdate()
{
    // ...

    m_toolTip.Create(this, TTS_ALWAYSTIP | TTS_NOANIMATE);
    m_toolTip.AddTool(this, _T("Doesn't matter"));
}

处理 WM_MOUSEMOVE 消息。首先,调用 _TrackMouseEvent 以进一步接收 WM_MOUSELEAVE 并激活工具提示。其次,更新工具提示文本,并在鼠标指针坐标处显示。

void CToolTipDemoView::OnMouseMove(UINT nFlags, CPoint point)
{
    if (!m_bTrackingMouseLeave)
    {
        TRACKMOUSEEVENT tme = { 0 };
        tme.cbSize = sizeof(TRACKMOUSEEVENT);
        tme.dwFlags = TME_LEAVE;
        tme.hwndTrack = m_hWnd;
        ::_TrackMouseEvent(&tme);

        m_toolTip.Activate(TRUE);
        m_bTrackingMouseLeave = TRUE;
    }

    if (m_pointLastMousePos != point)
    {
        CString strText;
        strText.Format(_T("x = %d y = %d"), point.x, point.y);
        m_toolTip.UpdateTipText(strText, this);
        m_toolTip.Popup();
        m_pointLastMousePos = point;
    }

    CScrollView::OnMouseMove(nFlags, point);
}

处理 WM_MOUSELEAVE 并停用工具提示。

void CCToolTipDemoView::OnMouseLeave()
{
    m_bTrackingMouseLeave = FALSE;

    // mouse pointer leaves the window so deactivate the tooltip
    m_toolTip.Activate(FALSE);

    CScrollView::OnMouseLeave();
}

笔记:

  • 不再需要处理 TTN_NEEDTEXT。
  • 此外,不再需要覆盖 PreTranslateMessage
于 2017-08-09T09:18:09.760 回答
0

为了解决问题,您需要执行以下操作:

BOOL CMyAppView::PreTranslateMessage(MSG* pMsg)
{
    switch (pMsg->message)
    {
    case WM_KEYDOWN:
    case WM_SYSKEYDOWN:
    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_LBUTTONUP:
    case WM_RBUTTONUP:
    case WM_MBUTTONUP:
    case WM_MOUSEMOVE:
        if (m_pToolTip->GetSafeHwnd () != NULL)
        {
            m_pToolTip->RelayEvent(pMsg);
        }
        break;
    }

    return CScrollView::PreTranslateMessage(pMsg);
}
于 2017-08-09T08:37:30.120 回答
0

我没有成功让跟踪工具提示在 MFC 中工作。我来的最近的是

在消息映射中:ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnNeedToolTipText)

OnInitialUpdateBOOL ok1 = EnableTrackingToolTips(TRUE);

在覆盖虚函数时OnToolHitTest

    pTI->hwnd = m_hWnd;
    pTI->uId = (UINT_PTR)m_hWnd;
    pTI->uFlags = TTF_IDISHWND | TTF_ALWAYSTIP | TTF_TRACK | TTF_NOTBUTTON | TTF_ABSOLUTE | TTF_SUBCLASS;
    pTI->lpszText = LPSTR_TEXTCALLBACK;
    return pTI->uId;

OnNeedToolTipText

NMTTDISPINFO *pTTT = (NMTTDISPINFO *)pNMHDR;
UINT_PTR nID = pNMHDR->idFrom;
BOOL bRet = FALSE;
if(pTTT->uFlags & TTF_IDISHWND)
{
    // idFrom is actually the HWND of the tool
    nID = ::GetDlgCtrlID((HWND)nID);
    if(nID)
    {
        CURSORINFO ci; ci.cbSize = sizeof(CURSORINFO); // get something interesting to display
        GetCursorInfo(&ci);
        _stprintf_s(pTTT->szText, sizeof(pTTT->szText) / sizeof(TCHAR),
            _T("Control ID = %lld at (%d, %d)"), nID, ci.ptScreenPos.x, ci.ptScreenPos.y);
        pTTT->hinst = AfxGetResourceHandle();
        bRet = TRUE;
    }
}
*pResult = 0;
return bRet;

这会产生以下特殊行为。当我启动应用程序并将鼠标光标移动到 CScrollView 的客户区域时,光标旁边会出现一个工具提示。

如果我小心(平稳地)移动鼠标,工具提示会正确跟踪。但是,过了一会儿,它消失了,并且没有进一步的鼠标动作,包括离开 CScrollView 窗口并返回,使它重新出现。

我认为正在发生的事情是,当鼠标光标移动到工具提示窗口上时,工具提示会永久关闭。这种消失似乎与时间无关(例如,由于自动弹出);如果鼠标保持不动,则工具提示将无限期保留。

于 2017-08-13T18:32:49.647 回答