Click here to Skip to main content
15,893,668 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.h : interface 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.
 */
 
#if !defined(AFX_TOOLTIPSVIEW_H__D8DD3C97_AE13_4282_B95E_6EC9834ECBE6__INCLUDED_)
#define AFX_TOOLTIPSVIEW_H__D8DD3C97_AE13_4282_B95E_6EC9834ECBE6__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include <list>

using namespace std;

class CDemoItem
{
public:
	CDemoItem(){}
	virtual ~CDemoItem(){}
	int m_x;
	int m_y;
	CString m_str;
	RECT m_rect;
};

typedef list<CDemoItem*, allocator<CDemoItem*> > CDemoItemList;

class CToolTipsView : public CWindowImpl<CToolTipsView>
{
public:
	CDemoItem* AddItem(int x, int y, LPCTSTR pszText);
	DECLARE_WND_CLASS(NULL)

	BOOL PreTranslateMessage(MSG* pMsg);

	BEGIN_MSG_MAP(CToolTipsView)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave)
		MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		NOTIFY_HANDLER((UINT)m_hWnd, TTN_NEEDTEXT, OnNeedTextNotify)
	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnMouseLeave(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnNeedTextNotify(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/);
// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

protected:
	void GetItemRect(CDemoItem *pItem, RECT *pRect);
	CDemoItem* GetItemAtPt(POINT pt);

private:
	CDemoItemList m_items;
	HFONT m_font;
	CToolTipCtrl m_toolTip;
	TOOLINFO	m_ti;

};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TOOLTIPSVIEW_H__D8DD3C97_AE13_4282_B95E_6EC9834ECBE6__INCLUDED_)

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