由来
在一个项目里,需要随时跟踪鼠标位置的坐标值,当然可以在状态栏里显示,也可以用tooltips的方式来显示,一般在tooltips一般在对话框里用,
这里记录下在单文档视图里的使用
平台
Win10 64bit
VS2015
MFC SDI/MDI
效果
主要原理
从CMFCToolTipCtrl派生出子类CCustomToolTipCtrl自绘
核心代码
class CCustomToolTipCtrl : public CMFCToolTipCtrl
{
// Construction
public:
CCustomToolTipCtrl();
// Attributes
public:
int m_nCurrID;
// Operations
public:
// Overrides
// Implementation
public:
virtual ~CCustomToolTipCtrl();
protected:
CSize OnDrawLabel(CDC* pDC,CRect rect,BOOL bCalcOnly);
};
CCustomToolTipCtrl::CCustomToolTipCtrl()
{
m_nCurrID = 0;
}
CCustomToolTipCtrl::~CCustomToolTipCtrl()
{
}
CSize CCustomToolTipCtrl::OnDrawLabel(CDC* pDC, CRect rect, BOOL bCalcOnly)
{
// TRACE("ENTER CCustomToolTipCtrl::OnDrawLabel bCalcOnly=%d\n", bCalcOnly);
ASSERT_VALID(pDC);
// my special tooltip drawing system for context sensitive tooltips in view (track)
POINT cursor;
GetCursorPos(&cursor);
::GetActiveView()->ScreenToClient(&cursor);
CString labelText, descriptionText;
::GetActiveView()->GetToolTipLabelText(cursor, labelText, descriptionText);
CSize sizeText(0, 0);
BOOL bDrawDescr = m_Params.m_bDrawDescription && !m_strDescription.IsEmpty();
CFont* pOldFont = (CFont*) pDC->SelectObject(m_Params.m_bBoldLabel && bDrawDescr ? &afxGlobalData.fontBold : &afxGlobalData.fontTooltip);
if (labelText.Find(_T('\n')) >= 0) // Multi-line text
{
UINT nFormat = DT_NOPREFIX;
if (bCalcOnly)
{
nFormat |= DT_CALCRECT;
}
if (m_pRibbonButton != NULL)
{
nFormat |= DT_NOPREFIX;
}
int nHeight = pDC->DrawText(labelText, rect, nFormat);
sizeText = CSize(rect.Width(), nHeight);
}
else
{
if (bCalcOnly)
{
sizeText = pDC->GetTextExtent(labelText);
}
else
{
UINT nFormat = DT_LEFT | DT_NOCLIP | DT_SINGLELINE;
if (!bDrawDescr)
{
nFormat |= DT_VCENTER;
}
if (m_pRibbonButton != NULL)
{
nFormat |= DT_NOPREFIX;
}
sizeText.cy = pDC->DrawText(labelText, rect, nFormat);
sizeText.cx = rect.Width();
}
}
pDC->SelectObject(pOldFont);
SetDescription (descriptionText);
return sizeText;
}
参考文献
How to Use CMFCToolTipCtrl Tooltips in a View for Dynamic Context Sensitive Tips
工程下载
本站上传出错,临时用360网盘https://yunpan.cn/cYxcqHxV53Tvb (提取码:53e2)