📄 tooltipex.cpp
字号:
// ToolTipEx.cpp : implementation file
//
#include "stdafx.h"
#include "multiboard.h"
#include "ToolTipEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
ToolTipInfo::ToolTipInfo()
{
}
ToolTipInfo::~ToolTipInfo()
{
}
CellTipInfo::CellTipInfo()
{
m_bShowing = FALSE;
}
CellTipInfo::~CellTipInfo()
{
}
/////////////////////////////////////////////////////////////////////////////
// CToolTipEx
CToolTipEx::CToolTipEx()
{
m_nHeight = 0;
m_nWidth = 0;
m_nFontSize = 14; // original size
m_currentControlID = 0;
m_currentCellID = -1;
m_pParentWnd = NULL;
m_aControlInfo.RemoveAll();
m_ayCellInfo.RemoveAll();
}
CToolTipEx::~CToolTipEx()
{
int i = 0,nSize = 0;
ToolTipInfo *pInfo = NULL;
nSize = m_aControlInfo.GetSize();
for( i = 0; i < nSize; i++ )
{
pInfo = (ToolTipInfo*)m_aControlInfo.GetAt( i );
delete pInfo;
}
m_aControlInfo.RemoveAll();
nSize = m_ayCellInfo.GetSize();
CellTipInfo *pTipInfo = NULL;
for(i = 0 ;i < nSize ; i++)
{
pTipInfo = (CellTipInfo*)m_ayCellInfo.GetAt(i);
delete pTipInfo;
}
m_ayCellInfo.RemoveAll();
}
BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
//{{AFX_MSG_MAP(CToolTipEx)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CToolTipEx message handlers
void CToolTipEx::Create(CWnd *pWnd)
{
m_pParentWnd = pWnd;
}
void CToolTipEx::SetFontSize(int size)
{
m_nFontSize = size;
}
void CToolTipEx::DisplayInfo(ToolTipInfo *pToolTip)
{
if( pToolTip->nInfoSize <= 0 )
return;
ASSERT(m_pParentWnd);
CDC* pDC = m_pParentWnd->GetDC();
CRect oInfoRect;
CBrush oBrush, *pOldBrush, oBorderBrush;
int nX, nY;
TEXTMETRIC TM;
int nTextHigh;
CFont oFont, *pOldFont;
CWnd* pWnd = NULL;
pDC->SetBkMode(TRANSPARENT);
oBrush.CreateSolidBrush( pToolTip->nBackColor );
pOldBrush = pDC->SelectObject( &oBrush );
pDC->SetTextColor( pToolTip->nTextColor );
//calculate the width and height of the box dynamically
CalculateHeightAndWidth( pToolTip->nControlInfo );
CalculateInfoBoxRect( pToolTip->nControlID, &oInfoRect );
oFont.CreateFont(m_nFontSize, 0, 0, 0, FW_REGULAR, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
pOldFont = pDC->SelectObject(&oFont);
pDC->FillRect(&oInfoRect, &oBrush);
pDC->SelectObject(pOldBrush);
oBrush.DeleteObject();
oBorderBrush.CreateSolidBrush( pToolTip->nTextColor );
pOldBrush = pDC->SelectObject(&oBorderBrush);
DrawEdge(pDC->m_hDC, &oInfoRect, BDR_RAISEDINNER | BDR_SUNKENOUTER, BF_RECT);
pDC->SetTextAlign(TA_LEFT);
pDC->GetTextMetrics(&TM);
nTextHigh = TM.tmHeight + TM.tmExternalLeading - 2;
nX = oInfoRect.left + 8;
nY = oInfoRect.top + 5;
for( register UINT i = 0; i < pToolTip->nInfoSize; i++)
{
pDC->TextOut(nX, nY, pToolTip->nControlInfo[i]);
nY += m_nFontSize - 1;
}
pDC->SelectObject(pOldBrush);
oBorderBrush.DeleteObject();
m_pParentWnd->ReleaseDC(pDC);
}
void CToolTipEx::CalculateInfoBoxRect(UINT nControlID, CRect *pInfoRect)
{
ASSERT(m_pParentWnd);
CRect oRect(0,0,0,0);
CRect oParentWindowRect(0,0,0,0);
m_pParentWnd->GetWindowRect(&oParentWindowRect);
m_pParentWnd->ScreenToClient(&oParentWindowRect);
CWnd* pWnd = m_pParentWnd->GetDlgItem(nControlID);
ASSERT(pWnd);
pWnd->GetWindowRect(&oRect);
m_pParentWnd->ScreenToClient(&oRect);
//check if the box fit in the parent dialog
SetFontSize( 14 );
int nBottom = oRect.bottom - ((oRect.bottom - oRect.top)/2) + m_nHeight;
if(nBottom <= oParentWindowRect.bottom)
{
pInfoRect->top = oRect.bottom - (oRect.bottom - oRect.top)/2;
pInfoRect->bottom = pInfoRect->top + m_nHeight;
}
else
{
// change the size of the font as well as the info box if all
// info data cannot be viewed
if( m_nHeight > (oParentWindowRect.bottom - oParentWindowRect.top) )
{
SetFontSize( 12 );
m_nHeight = 12 + m_nTotalLine * (m_nFontSize - 1); //m_nFontSize is the font size
m_nWidth = 10 + (int)(7 * m_maxCharInLine);
}
// end
pInfoRect->bottom = oParentWindowRect.bottom - 1;
pInfoRect->top = pInfoRect->bottom - m_nHeight;
}
//check if the box fit in the parent dialog
int nRight = (oRect.left + oRect.right)/2 + m_nWidth;
if(nRight <= oParentWindowRect.right)
{
pInfoRect->left = (oRect.left + oRect.right)/2;
pInfoRect->right = pInfoRect->left + m_nWidth;
}
else
{
int nLeft = (oRect.left + oRect.right)/2 - m_nWidth;
if(nLeft <= oParentWindowRect.left)
{
pInfoRect->left = oParentWindowRect.left + 1;
pInfoRect->right = pInfoRect->left + m_nWidth;
}
else
{
pInfoRect->right = (oRect.left + oRect.right)/2;
pInfoRect->left = pInfoRect->right - m_nWidth;
}
}
ASSERT(pInfoRect->top <= pInfoRect->bottom);
ASSERT(pInfoRect->left <= pInfoRect->right);
}
void CToolTipEx::CalculateHeightAndWidth(CStringArray &straInfos)
{
//This assertion fails because you did not call Create() first.
//m_pParentWnd was not initialized.
ASSERT(m_pParentWnd);
int nMaxLength = 0;
int nLength;
int nLine = straInfos.GetSize();
m_nTotalLine = nLine; // holder for maximum line of information
for(int i=0; i<nLine; i++)
{
nLength = (straInfos[i]).GetLength();
if(nLength > nMaxLength)
nMaxLength = nLength;
}
m_maxCharInLine = nMaxLength; // holder for longest char info
m_nHeight = 12 + nLine * (m_nFontSize - 1); //m_nFontSize is the font size
m_nWidth = 10 + (int)(7.5 * nMaxLength); //aproximately 5 pixel per char
}
ToolTipInfo* CToolTipEx::IsControlIDExisting(UINT controlID)
{
ToolTipInfo *pToolTip = NULL;
int nSize = m_aControlInfo.GetSize();
for( register int i = 0; i < nSize; i++ )
{
pToolTip = (ToolTipInfo*)m_aControlInfo.GetAt( i );
if( pToolTip->nControlID == controlID ) // if found!
{
m_currentControlID = controlID;
return pToolTip;
}
}
m_currentControlID = 0; // if not found, reset the current control ID to refresh the display
return NULL;
}
void CToolTipEx::ErasePreviousToolTipDisplay(UINT nControlID)
{
//This assertion fails because you did not call Create() first.
//m_pParentWnd was not initialized.
ASSERT(m_pParentWnd);
//if erase already, do not do it again
if((m_nHeight == 0) || (m_nWidth == 0))
return;
CRect oInfoRect(0,0,0,0);
CalculateInfoBoxRect(nControlID, &oInfoRect);
m_pParentWnd->InvalidateRect(&oInfoRect);
m_pParentWnd->UpdateWindow();
m_nHeight = 0;
m_nWidth = 0;
}
void CToolTipEx::ShowToolTip(UINT nControlID)
{
ToolTipInfo *pToolTip = IsControlIDExisting( nControlID );
if( pToolTip == NULL )
return;
DisplayInfo( pToolTip );
}
void CToolTipEx::ShowToolTip(CPoint &point)
{
CWnd* pWnd = m_pParentWnd->ChildWindowFromPoint(point);
if( pWnd )
{
UINT nControlID = (UINT)pWnd->GetDlgCtrlID();
if( m_currentControlID != nControlID )
{
ErasePreviousToolTipDisplay( m_currentControlID );
ShowToolTip( nControlID );
}
}
}
BOOL CToolTipEx::AddControlInfo(UINT contolID, CStringArray &straInfo, COLORREF back, COLORREF text)
{
ToolTipInfo *pToolTip = new ToolTipInfo;
ASSERT( pToolTip != NULL );
int nSize = straInfo.GetSize();
if( pToolTip <= 0 ) // no information, don't add to the list and return FALSE
{
delete pToolTip;
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -