Add tooltip to a CStatic

寵の児 提交于 2019-12-23 07:05:58

问题


I haven't been able to find a concise chunk of code that allows me to add/display tooltips to a CStatic (and CLed) control. Obviously, the standard code to do so does not apply for this type of control. Can someone post code snippets?


回答1:


I hope this code will solve your problem .One important thing make NOTIFY property of CStatic =TRUE.

if( !m_ToolTip.Create(this))
{
    TRACE0("Unable to create the ToolTip!");
}
else
{
    CWnd* pWnd = GetDlgItem(IDC_STATIC_MASTER_PWD);
    m_ToolTip.AddTool(pWnd,"Ok");
    m_ToolTip.Activate(TRUE);
}

Let me know if any problem.




回答2:


When I add CStatic on Dialog based autocreated mfc application, tooltips don't show until I add RelayEvent in pretranslate dialog message

BOOL CTooltipStaticDlg::PreTranslateMessage(MSG* pMsg)
{
    m_ToolTip.RelayEvent(pMsg); 
    return CDialog::PreTranslateMessage(pMsg);
}



回答3:


I've had success with multiline tooltips using this simple class:

Create a class for ToolTips:

class ToolTip
{
public:
    static HWND CreateToolTip(int toolID, HWND hDlg, UINT id);
};

Next, implement a tooltip creation function:

HWND ToolTip::CreateToolTip(int toolID, HWND hDlg, UINT id)
{
    if (!toolID || !hDlg || !id)
    {
        return FALSE;
    }

    CString strTTText;
    strTTText.LoadString( id );

    // Get the window handle of the control to attach the TT to.
    HWND hwndTool = ::GetDlgItem(hDlg, toolID);

    // Create the tooltip window
    HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                              WS_POPUP |TTS_ALWAYSTIP,// | TTS_BALLOON,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              hDlg, NULL, 
                              AfxGetInstanceHandle() , NULL);

   if (!hwndTool || !hwndTip)
   {
       return (HWND)NULL;
   }                              

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hDlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = (char*)(LPCTSTR)strTTText;
    ::SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
    ::SendMessageA(hwndTip, TTM_SETMAXTIPWIDTH, 0, 40); // force multi-line

    return hwndTip;
}

Call it somewhere in your InitDialog:

CMyDialog::InitDialog()
{
    ToolTip::CreateToolTip( PickAUniqueNumber, m_hWnd, IDS_MY_RESOURCE_STRING );
}



回答4:


I don't know if this is still needed, but here's what I used to solve the problem: just add SS_NOTIFY to dwStyle when creating the static label. (or simply set "Nofity" "True" in the properties). That worked fine for me.




回答5:


I've had on my dialog label with assigned custom ID IDC_PATH. I needed to turn on Notify flag (SS_NOTIFY) of the label and I needed to overload CWnd method OnToolHitTest and handle tooltip hit test like this:

INT_PTR CPath::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
    INT_PTR r = CWnd::OnToolHitTest(point,pTI);

    this->ClientToScreen(&point);
    CRect rcLbl;
    GetDlgItem(IDC_PATH)->GetWindowRect(&rcLbl);
    if( rcLbl.PtInRect(point) )
    {
        pTI->uFlags |= TTF_IDISHWND;
        pTI->uFlags &= ~TTF_NOTBUTTON;
        pTI->uId = (UINT_PTR)GetDlgItem(IDC_PATH)->m_hWnd;
        return IDC_PATH;
    }

    return r;
}

Then my dialog started to receive TTN_NEEDTEXT notification, which I handled and dynamicaly set text for tooltip.

BOOL CPath::OnTtnNeedText(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
   UNREFERENCED_PARAMETER(id);

   TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)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 == IDC_PATH)
      {
         pTTT->lpszText = (LPSTR)(LPCTSTR)m_FullDestPath;
         bRet = TRUE;
      }
   }

   *pResult = 0;

   return bRet;
}


来源:https://stackoverflow.com/questions/13363079/add-tooltip-to-a-cstatic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!