📄 tooltipex.cpp
字号:
}
pToolTip->nControlInfo.RemoveAll();
pToolTip->nInfoSize = nSize;
pToolTip->nControlID = contolID;
for( register int i = 0; i < nSize; i++ )
{
pToolTip->nControlInfo.Add( straInfo[i] );
}
// initialize colors, use default if not supplied
pToolTip->nBackColor = back;
pToolTip->nTextColor = text;
// add to the list
m_aControlInfo.Add( pToolTip );
return TRUE;
}
BOOL CToolTipEx::RemoveControlInfo(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!
{
if( m_currentControlID == controlID )
ErasePreviousToolTipDisplay( m_currentControlID );
m_aControlInfo.RemoveAt( i ); // remove the entry
delete pToolTip;
break; // break from the loop
}
}
return TRUE;
}
/////////////////////////////////////////////////////////////
////////////////// Cell(add by baoshenghua /////
/////////////////////////////////////////////////////////////
BOOL CToolTipEx::AddCellInfo(UINT nCellID, CStringArray &strayInfo, COLORREF crBack, COLORREF crText)
{
CellTipInfo *pTipInfo = new CellTipInfo;
ASSERT( pTipInfo != NULL );
int nSize = strayInfo.GetSize();
if( pTipInfo <= 0 ) // no information, don't add to the list and return FALSE
{
delete pTipInfo;
return FALSE;
}
pTipInfo->m_ayCellInfo.RemoveAll();
pTipInfo->m_iCellID = nCellID;
for( register int i = 0; i < nSize; i++ )
{
pTipInfo->m_ayCellInfo.Add( strayInfo[i] );
}
// initialize colors, use default if not supplied
pTipInfo->m_crBackColor = crBack;
pTipInfo->m_crTextColor = crText;
// add to the list
m_ayCellInfo.Add( pTipInfo );
return TRUE;
}
BOOL CToolTipEx::AddCellInfo(UINT uCellID, LPCTSTR strInfo, COLORREF crBack, COLORREF crText)
{
CellTipInfo *pTipInfo = new CellTipInfo;
ASSERT( pTipInfo != NULL );
if( pTipInfo <= 0 ) // no information, don't add to the list and return FALSE
{
delete pTipInfo;
return FALSE;
}
pTipInfo->m_ayCellInfo.Add( strInfo);
// initialize colors, use default if not supplied
pTipInfo->m_crBackColor = crBack;
pTipInfo->m_crTextColor = crText;
// add to the list
m_ayCellInfo.Add( pTipInfo );
return TRUE;
}
void CToolTipEx::CalculateCellInfoBoxRect(CellTipInfo *pTipInfo,CPoint &pt)
{
ASSERT(m_pParentWnd);
int nMaxLength = 0;
int nLength = 0;
int nHeight = 0,nWidth = 0;
int nLine = pTipInfo->m_ayCellInfo.GetSize();
m_nTotalLine = nLine; // holder for maximum line of information
for(int i=0; i<nLine; i++)
{
nLength = (pTipInfo->m_ayCellInfo[i]).GetLength();
if(nLength > nMaxLength)
nMaxLength = nLength;
}
nHeight = 12 + nLine * (m_nFontSize - 1); //m_nFontSize is the font size
nWidth = 10 + (int)(7.5 * nMaxLength);
CRect rectParent(0,0,0,0);
CRect rectInfo(0,0,0,0);
m_pParentWnd->GetClientRect(&rectParent);
int nBottom = pt.y + nHeight;
if(nBottom <= rectParent.bottom)
{
rectInfo.top = pt.y,rectInfo.bottom = nBottom;
}
else
{
if(rectParent.Height() >= nHeight){
rectInfo.bottom = rectParent.bottom-2,rectInfo.top = rectInfo.bottom - nHeight;
}
else{
int nTop = (rectParent.bottom + rectParent.top)/2;
rectInfo.top = nTop,rectInfo.bottom = rectInfo.top + nHeight;
}
}
int nRight = pt.x + nWidth;
if(nRight <= rectParent.right)
{
rectInfo.left = pt.x,rectInfo.bottom = nRight;
}
{
if(rectParent.Width() >= nWidth){
rectInfo.right = rectParent.right -2,rectInfo.left = rectInfo.right - nWidth;
}
else{
int nLeft = (rectParent.right + rectParent.left)/2;
rectInfo.left = nLeft,rectInfo.right = rectInfo.left + nWidth;
}
}
pTipInfo->m_rectShow = rectInfo;
ASSERT(rectInfo.top <= rectInfo.bottom);
ASSERT(rectInfo.left <= rectInfo.right);
}
void CToolTipEx::DisplayCellInfo(CellTipInfo *pTipInfo,CPoint &pt)
{
if( pTipInfo->m_ayCellInfo.GetSize() <= 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( pTipInfo->m_crBackColor );
pOldBrush = pDC->SelectObject( &oBrush );
pDC->SetTextColor( pTipInfo->m_crTextColor );
//calculate the width and height of the box dynamically
CalculateCellInfoBoxRect( pTipInfo,pt);
pTipInfo->m_bShowing = TRUE;
oInfoRect = pTipInfo->m_rectShow;
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( pTipInfo->m_crTextColor );
pOldBrush = pDC->SelectObject(&oBorderBrush);
// DrawEdge(pDC->m_hDC, &oInfoRect, BDR_RAISEDINNER | BDR_SUNKENOUTER, BF_RECT);
CPen penBlack;
penBlack.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&penBlack);
// pDC->Rectangle(oInfoRect);
pDC->MoveTo(oInfoRect.left,oInfoRect.top);
pDC->LineTo(oInfoRect.right,oInfoRect.top);
pDC->MoveTo(oInfoRect.left,oInfoRect.top);
pDC->LineTo(oInfoRect.left,oInfoRect.bottom);
pDC->MoveTo(oInfoRect.right-1,oInfoRect.bottom-1);
pDC->LineTo(oInfoRect.left,oInfoRect.bottom-1);
pDC->MoveTo(oInfoRect.right-1,oInfoRect.bottom-1);
pDC->LineTo(oInfoRect.right-1,oInfoRect.top);
pDC->SelectObject(pOldPen);
penBlack.DeleteObject();
/////////////////////////////////////////// Draw Back Boarder ////////
pDC->SetTextAlign(TA_LEFT);
pDC->GetTextMetrics(&TM);
nTextHigh = TM.tmHeight + TM.tmExternalLeading - 2;
nX = oInfoRect.left + 8;
nY = oInfoRect.top + 5;
int nSize = pTipInfo->m_ayCellInfo.GetSize();
for( register int i = 0; i < nSize; i++)
{
pDC->TextOut(nX, nY, pTipInfo->m_ayCellInfo[i]);
nY += m_nFontSize - 1;
}
pDC->SelectObject(pOldBrush);
oBorderBrush.DeleteObject();
m_pParentWnd->ReleaseDC(pDC);
}
CellTipInfo* CToolTipEx::IsCellIDExisting(UINT uCellID)
{
CellTipInfo *pTipInfo = NULL;
int nSize = m_ayCellInfo.GetSize();
for( register int i = 0; i < nSize; i++ )
{
pTipInfo = (CellTipInfo*)m_ayCellInfo.GetAt( i );
if( pTipInfo->m_iCellID == uCellID ) // if found!
{
m_currentCellID = uCellID;
return pTipInfo;
}
}
m_currentCellID = -1; // if not found, reset the current control ID to refresh the display
return NULL;
}
void CToolTipEx::ErasePreviousCellTipDisplay(UINT uCellID)
{
//This assertion fails because you did not call Create() first.
//m_pParentWnd was not initialized.
ASSERT(m_pParentWnd);
CellTipInfo *pTipInfo = NULL;
int nSize = m_ayCellInfo.GetSize();
for( register int i = 0; i < nSize; i++)
{
pTipInfo = (CellTipInfo *)m_ayCellInfo.GetAt(i);
if(pTipInfo->m_iCellID == uCellID)
{
break;
}
}
if(pTipInfo == NULL) return;
//if erase already, do not do it again
if(!pTipInfo->m_bShowing) return;
m_pParentWnd->InvalidateRect(&(pTipInfo->m_rectShow));
m_pParentWnd->UpdateWindow();
pTipInfo->m_bShowing = FALSE;
}
BOOL CToolTipEx::RemoveCellInfo(UINT uCellID)
{
CellTipInfo *pTipInfo = NULL;
int nSize = m_ayCellInfo.GetSize();
for( register int i = 0; i < nSize; i++)
{
pTipInfo = (CellTipInfo *)m_ayCellInfo.GetAt(i);
if(pTipInfo->m_iCellID == uCellID)
{
if(m_currentCellID == uCellID)
ErasePreviousCellTipDisplay(uCellID);
m_ayCellInfo.RemoveAt(i);
delete pTipInfo;
break;
}
}
return TRUE;
}
void CToolTipEx::ShowCellTip(UINT uCellID,CPoint &pt)
{
if(m_currentCellID != uCellID)
{
ErasePreviousCellTipDisplay(m_currentCellID);
CellTipInfo *pTipInfo = IsCellIDExisting( uCellID );
if( pTipInfo == NULL )
return;
DisplayCellInfo( pTipInfo,pt );
}
}
void CToolTipEx::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CWnd::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -