起初我想使用CToolTipCtrl::AddTool的第三个参数lpRectTool来实现这个功能,但没有成功。后来,我采用了自立更生的解决方案,创建了一个可重用窗口类:
CPopupText 类定义和实现 ////////////////////////////////////////////////////////////////// PupText.h // #pragma once// Get NONCLIENTMETRICS info: ctor calls SystemParametersInfo.//class CNonClientMetrics : public NONCLIENTMETRICS {public: CNonClientMetrics() { cbSize = sizeof(NONCLIENTMETRICS); SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,this,0); }};// Popup text window, like tooltip.// Can be right or left justified relative to creation point.//class CPopupText : public CWnd {public: CSize m_szMargins; // extra space around text: change if you like enum {JUSTIFYLEFT=0, JUSTIFYRIGHT}; CPopupText(); virtual ~CPopupText(); BOOL Create(CPoint pt, CWnd* pParentWnd, UINT nStyle=0, UINT nID=0); void ShowDelayed(UINT msec); void Cancel();protected: CFont m_font; // font to use (same as tooltips) UINT m_nStyle; // style (see enum below) virtual void PostNcDestroy(); 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();};PupText.cpp ////////////////////////////////////////////////////////////////// VCKBASE -- September 2000 // Visual C++ 6.0 环境编译, Windows 98 和 NT 环境运行.// #include "stdafx.h"#include "puptext.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifIMPLEMENT_DYNAMIC(CPopupText,CWnd)BEGIN_MESSAGE_MAP(CPopupText,CWnd) ON_WM_PAINT() ON_MESSAGE(WM_SETTEXT, OnSetText) ON_WM_TIMER()END_MESSAGE_MAP()CPopupText::CPopupText(){ m_szMargins = CSize(4,4); // create font ?use system tooltip font CNonClientMetrics ncm; m_font.CreateFontIndirect(&ncm.lfStatusFont);}CPopupText::~CPopupText(){}// Create window. pt is upper-left or upper-right corner depending on // nStyle.//CPopupText::Create(CPoint pt, CWnd* pParentWnd, UINT nStyle, UINT nID){ m_nStyle = nStyle; return CreateEx(0, NULL, NULL, WS_POPUP|WS_VISIBLE, CRect(pt,CSize(0,0)), pParentWnd, nID);}// Someone changed the text: resize to fit new text//LRESULT CPopupText::OnSetText(WPARAM wp, LPARAM lp){ CClientDC dc = this; CFont* pOldFont = dc.SelectObject(&m_font); CRect rc; GetWindowRect(&rc); int x = (m_nStyle & JUSTIFYRIGHT) ? rc.right : rc.left; int y = rc.top; dc.DrawText(CString((LPCTSTR)lp), &rc, DT_CALCRECT); rc.InflateRect(m_szMargins); if (m_nStyle & JUSTIFYRIGHT) x -= rc.Width(); SetWindowPos(NULL,x,y,rc.Width(),rc.Height(), SWP_NOZORDER|SWP_NOACTIVATE); return Default();}// Paint the text. Use system colors//void CPopupText::OnPaint(){ CPaintDC dc(this); CRect rc; GetClientRect(&rc); CString s; GetWindowText(s); CBrush b(GetSysColor(COLOR_INFOBK)); // use tooltip bg color dc.FillRect(&rc, &b); // draw text dc.SetBkMode(TRANSPARENT); CFont* pOldFont = dc.SelectObject(&m_font); dc.SetTextColor(GetSysColor(COLOR_INFOTEXT)); // tooltip text color dc.DrawText(s, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER); dc.SelectObject(pOldFont);}// Register class if needed//BOOL CPopupText::PreCreateWindow(CREATESTRUCT& cs)