MFC中控制Tips的显示

from:  http://www.cnblogs.com/lingyun1120/archive/2011/11/09/2243279.html

    MFC中为ToolTips生成了一个类CToolTipCtrl,一般使用步骤如下:

        1、 添加CToolTipCtrl成员变量

        2、在父窗口中调用EnableToolTips(TRUE);

        3、在窗口的OnCreate或者Dlg的OnInitialDlg中向ToolTip中添加需要显示Tip的子窗口,并同时指定相应的显示字串

            CToolTipCtrl::AddTool()

        4、 重载父窗口的 BOOL PreTranslateMessage(MSG* pMsg) ,在函数中调用CToolTipCtrl成员变量的RelayEvent()

     正常情况下,经过上面的四步,当鼠标移动到窗口上时自动显示tips信息。如果想控制tips在满足一定条件下显示,该如何处理呢?可以通过以下方式实现:

            m_toolTipsCtrl.UpdateTipText(_T("大写锁定打开可能造成密码设置错误"), pFocusWnd);
            CToolInfo       sTinfo;                // 提示信息
            m_toolTipsCtrl.GetToolInfo(sTinfo, pFocusWnd);
            sTinfo.uFlags = TTF_TRACK;     // 显示方式设置
            m_toolTipsCtrl.SetToolInfo(&sTinfo);

            // 下面是关键两步
            m_toolTipsCtrl.SendMessage(TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(rect.left, rect.bottom));
            m_toolTipsCtrl.SendMessage(TTM_TRACKACTIVATE, TRUE, (LPARAM)&sTinfo ); // tips显示

     另外,通过Activate控制tips的有效性,这样就可以完全控制tips的显示,比如大写提示信息等。

列子:

case   WM_RBUTTONDOWN: 
GetCursorPos(&currentposition); 
//   Prepare   TOOLINFO   structure   for   use   as   tracking   ToolTip. 
ti.cbSize   =   sizeof(TOOLINFO); 
ti.uFlags   =   TTF_IDISHWND   |   TTF_TRACK   |   TTF_ABSOLUTE; 
ti.hwnd       =   m_thiswnd; 
ti.uId         =   (UINT)m_thiswnd; 
ti.hinst     =   ghinst; 
ti.lpszText     =   "右健 "; 
ti.rect.left   =   ti.rect.top   =   ti.rect.bottom   =   ti.rect.right   =   0;   

//   Add   the   tool   to   the   control,   displaying   an   error   if   needed. 
SendMessage(ToolTipWnd,TTM_ADDTOOL,0,(LPARAM)&ti); 
SendMessage(ToolTipWnd,TTM_TRACKPOSITION,0, 
                                        (LPARAM)MAKELPARAM(currentposition.x+15,currentposition.y+15)); 
SendMessage(ToolTipWnd,TTM_UPDATETIPTEXT,0,(LPARAM)&ti); 
SendMessage(ToolTipWnd,TTM_TRACKACTIVATE,(WPARAM)TRUE,(LPARAM)&ti); 
SendMessage(ToolTipWnd,TTM_SETDELAYTIME,TTDT_AUTOPOP,   (LPARAM)(INT)   MAKELONG(1000,0));

  

如果问题解决起来不妥或者有更好的解决办法,麻烦请告知,帮助曾经和你一样的入门者,谢谢。
原文地址:https://www.cnblogs.com/ourran/p/5323205.html