Click here to Skip to main content
15,897,273 members
Articles / Desktop Programming / MFC

Create In-Place ToolTips on yourself control

Rate me:
Please Sign up or sign in to vote.
3.52/5 (8 votes)
22 Mar 2007 42.8K   1.5K   21  
It is very easy to create In-Place ToolTips on yourself control like treeview control.
// ToolTipsView.cpp : implementation of the CToolTipsView class
//
/////////////////////////////////////////////////////////////////////////////
/*  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

 * Copyright (C) 2003-2007 Auto Debug System http://www.autodebug.com.
 */
 
#include "stdafx.h"
#include "resource.h"

#include "ToolTipsView.h"

BOOL CToolTipsView::PreTranslateMessage(MSG* pMsg)
{
	pMsg;
	return FALSE;
}

LRESULT CToolTipsView::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	CPaintDC dc(m_hWnd);

	//TODO: Add your drawing code here
	HFONT pOldFont = (HFONT)dc.SelectFont(m_font);
	CDemoItemList::iterator it;

	CDemoItem* pItem = NULL;
	for(it = m_items.begin(); it != m_items.end(); it++)
	{
		pItem = (*it);
		dc.TextOut(pItem->m_x, pItem->m_y, pItem->m_str);
	}	
	return 0;
}

CDemoItem* CToolTipsView::AddItem(int x, int y, LPCTSTR pszText)
{
	CDemoItem* pItem = new CDemoItem();
	pItem->m_x = x;
	pItem->m_y = y;
	pItem->m_str = pszText;
	
	m_items.push_back(pItem);
	
	HDC pDC = GetDC();
	HFONT pOldFont = (HFONT)SelectObject(pDC, m_font);
	CSize sz;
	GetTextExtentPoint32(pDC, pItem->m_str, pItem->m_str.GetLength(), &sz);
	SetRect(&pItem->m_rect, x, y, x+sz.cx, y+sz.cy);
	SelectObject(pDC, pOldFont);
	return pItem;	
}

CDemoItem* CToolTipsView::GetItemAtPt(POINT pt)
{
	CDemoItemList::iterator it;
	CDemoItem* pItem = NULL;
	for(it = m_items.begin(); it != m_items.end(); it++)
	{
		pItem = (*it);
		if(PtInRect(&pItem->m_rect, pt))
			return pItem;
	}	
	return NULL;

}

void CToolTipsView::GetItemRect(CDemoItem *pItem, RECT *pRect)
{
	SetRect(pRect, pItem->m_rect.left, pItem->m_rect.top, pItem->m_rect.right, pItem->m_rect.bottom);
}

LRESULT CToolTipsView::OnMouseLeave(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	m_toolTip.TrackActivate(&m_ti, FALSE);
	return 0;
}

LRESULT CToolTipsView::OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	POINT pt;
	
	pt.x = GET_X_LPARAM(lParam);
	pt.y = GET_Y_LPARAM(lParam);
	TRACKMOUSEEVENT tk;
	
	CDemoItem* pItem = GetItemAtPt(pt);
	if(pItem == NULL)
	{
		m_toolTip.TrackActivate(&m_ti, FALSE);
		return 0;
	}
	RECT rect;
	GetItemRect(pItem, &rect);
	pt.x = rect.left - 3;
	pt.y = rect.top - 2;
	tk.cbSize = sizeof(tk);
	tk.dwFlags = TME_LEAVE;//|TME_HOVER|TME_CANCEL;
	tk.dwHoverTime = 0;
	tk.hwndTrack = m_hWnd;
	_TrackMouseEvent(&tk);
	ClientToScreen(&pt);
	m_toolTip.TrackPosition(pt.x, pt.y);
	m_toolTip.TrackActivate(&m_ti, TRUE);
	return 0;
}

LRESULT CToolTipsView::OnNeedTextNotify(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
	LPNMTTDISPINFO pInfo = (LPNMTTDISPINFO)pnmh;
	POINT pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	CDemoItem* pItem = GetItemAtPt(pt);
	if(pItem)
		pInfo->lpszText = (LPTSTR)(LPCTSTR)pItem->m_str;
	return 0;
}

LRESULT CToolTipsView::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	m_font = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
	
	m_toolTip.Create(m_hWnd);
	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.AddTool(&m_ti);
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions