TIP: Create In-Place ToolTips on Your Own Controls

It is very easy to create in-place toolTips on your own control. For example, you can place one on a treeview control.

Step 1

In the window's header file, you must add a CToolTipCtrl instance and a TOOLINFO member. Also, you need to add a message map to handle the mouse messages.

protected:
   afx_msg void OnMouseMove(UINT nFlags, CPoint point);
   afx_msg void OnMouseLeave(WPARAM wPawam, LPARAM lParam);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
private:
   CToolTipCtrl m_toolTip;
   TOOLINFO   m_ti;

Step 2

In the window's implementation file, add the following to the end of Create to set the ToolTips.

m_toolTip.Create(this);
m_ti.cbSize = sizeof(m_ti);
m_ti.uFlags = TTF_IDISHWND|TTF_TRACK|TTF_ABSOLUTE |TTF_TRANSPARENT ;
m_ti.hwnd = m_hWnd;
m_ti.hinst = NULL;
m_ti.uId = (UINT)m_hWnd;
m_ti.lpszText = LPSTR_TEXTCALLBACK;
m_ti.rect.left = 0;
m_ti.rect.top = 0;
m_ti.rect.right = 0;
m_ti.rect.bottom = 0;

m_toolTip.SendMessage(TTM_ADDTOOL, 0, (LPARAM)&m_ti);

At the bottom of the message map, add the following:

   ON_WM_MOUSEMOVE()
   ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

Finally, add the message handler method as entered in the message map:

void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
   CDemoItem* pItem = GetItemAtPt(point);
   if(pItem == NULL)
   {
      m_toolTip.SendMessage(TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ti);
      return;
   }
   RECT rect;
   GetItemRect(pItem, &rect);
   POINT pt;

   TRACKMOUSEEVENT tk;
   tk.cbSize = sizeof(tk);
   tk.dwFlags = TME_LEAVE;
   tk.dwHoverTime = 0;
   tk.hwndTrack = m_hWnd;
   _TrackMouseEvent(&tk);
   pt.x = rect.left - 3;
   pt.y = rect.top - 2;
   ClientToScreen(&pt);
   m_toolTip.SendMessage(TTM_TRACKPOSITION, 0,
                         (LPARAM)MAKELPARAM(pt.x, pt.y));
   m_toolTip.SendMessage(TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ti);
}

void CChildView::OnMouseLeave(WPARAM wPawam, LPARAM lParam)
{
   m_toolTip.SendMessage(TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ti);
}


About the Author

Unknown Unknown

Best API Monitor tool. automatic monitor any dll all api's input and output parameter. http://www.autodebug.com

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds