CToolTipCtrl的使用实例-MSDN

如何操纵CToolTipCtrl来给自己的控件添加tool tip呢?MSDN给出了答案。

 

创建并操纵一个CToolTipCtrl

=======================

  1. 创建一个CToolTipCtrl的对象.

  2. 调用Create函数来创建windows通用提示控件并使之与CToolTipCtrl对象产生关联。

  3. 调用AddTool函数来把tool tip control注册到一个tool上,这样存储在tool tip中的信息就能在光标悬浮在这个tool上的时候显示出来。

  4. 调用SetToolInfo来设置tool tip为这个tool所保留的信息。

  5. 调用SetToolRect来设置该tol的一个新的范围矩形。

  6. 调用HitTest函数来判断一个点是否在某个给定tool的范围矩形之内,如果是的话,就返回这个tool的信息。

  7. 调用GetToolCount来得到一个tool tip所关联到的tool的个数。

// Create and associate a tooltip control to the tab control of 
// CMyPropertySheet.  CMyPropertySheet is a CPropertySheet-derived
// class.
BOOL CMyPropertySheet::OnInitDialog() 
...{
    BOOL bResult = CPropertySheet::OnInitDialog();

    // Create a tooltip control.  m_ToolTipCtrl is a member variable
    // of type CToolTipCtrl* in CMyPropertySheet class.  It is 
    // initialized to NULL in the constructor, and destroyed in the 
    // destructor of CMyPropertySheet class.
    m_ToolTipCtrl = new CToolTipCtrl;// 第一步,创建对象
    if (!m_ToolTipCtrl->Create(this)) //第二步,调用Create函数
        ...{
            TRACE("Unable To create ToolTip ");           
            return bResult;
    }

    // Associate the tooltip control to the tab control
    // of CMyPropertySheet.
    CTabCtrl* tab = GetTabControl();
    tab->SetToolTips(m_ToolTipCtrl);
    // Get the bounding rectangle of each tab in the tab control of the
    // property sheet. Use this rectangle when registering a tool with 
    // the tool tip control.  IDS_FIRST_TOOLTIP is the first ID string 
    // resource that contains the text for the tool.
    int count = tab->GetItemCount();
    int id = IDS_FIRST_TOOLTIP;
    for (int i = 0; i < count; i++)
        ...{
            id += i;
            CRect r;
            tab->GetItemRect(i, &r);
            VERIFY(m_ToolTipCtrl->AddTool(tab, id, &r, id));
    }

    // Activate the tooltip control.
    m_ToolTipCtrl->Activate(TRUE);

    return bResult;
}

// Override PreTranslateMessage() so RelayEvent() can be 
// called to pass a mouse message to CMyPropertySheet's 
// tooltip control for processing.
BOOL CMyPropertySheet::PreTranslateMessage(MSG* pMsg) 
...{
    if (NULL != m_ToolTipCtrl)            
        m_ToolTipCtrl->RelayEvent(pMsg);

    return CPropertySheet::PreTranslateMessage(pMsg);
}

posted on 2009-11-05 23:20  中道学友  阅读(5612)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上