// PopupText.h #pragma once /* Popup text window, like a Tool Tip: If this is a member of a CWnd derived class: CTrackPopup PopupText; call this in OnMouseMove: PopupText.SetTipTextAndPos("Hello World", x,y, this); it starts a timer and when the mouse stops moving for a moment, draws the Popup Text. and this to finish: PopupText.Hide(); */ class CPopupText : public CWnd { public: CSize Margins; // extra space around text: change if you like enum Justify {Left, Right}; CPopupText(); virtual ~CPopupText(); BOOL Create(CPoint pt, CWnd* pParentWnd, UINT nStyle=0, UINT nID=0); void Show(UINT msec); void Hide(); protected: CFont Font; // font to use (same as tooltips) Justify Style; // style // CPopupText is intended to be used on the stack, not heap, so don't auto-delete. virtual void PostNcDestroy() {/* don't delete this*/} virtual BOOL PreCreateWindow(CREATESTRUCT& cs); afx_msg void OnPaint(); afx_msg void OnTimer(UINT nIDEvent); afx_msg LRESULT OnSetText(WPARAM wp, LPARAM lp); DECLARE_DYNAMIC(CPopupText); DECLARE_MESSAGE_MAP(); }; class CTrackPopup : public CPopupText { int m_cursor_height; int m_xp; int m_yp; public: CTrackPopup() : m_xp(0), m_yp(0), m_cursor_height(GetSystemMetrics(SM_CYCURSOR)) {Create(CPoint(0,0),0,0);} virtual ~CTrackPopup() {} // Set text and position (in client coords of the parent) void SetTipTextAndPos(const CString& str, int xp, int yp, CWnd* parent, UINT Delay=0) { if(abs(xp - m_xp) < 2 && abs(yp - m_yp) < 2) return; m_xp = xp; m_yp = yp; Hide(); // don't show it while moving if(!str.IsEmpty()) { // Position tip window: CPoint Point(xp, yp + (m_cursor_height * 2) / 3); parent->ClientToScreen(&Point); SetWindowPos(NULL, Point.x, Point.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); SetWindowText(str); // Do this here to adjust the position in case the tip is off-screen. Show(Delay); // show pop-up text delayed (milliseconds) } } };