void CMFECToolTip::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;
void
CMFECToolTip::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
}
//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;
}
//calculate the width and height of the box dynamically
CalculateHeightAndWidth( pToolTip->nControlInfo );
CalculateInfoBoxRect( pToolTip->nControlID, &oInfoRect );
int nSize = straInfo.GetSize();
if( pToolTip <= 0 ) // no information, don't add to the list and return FALSE
{
delete pToolTip;
return FALSE;
}
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;
}
ToolTipInfo* CMFECToolTip::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;
}
BOOL CMFECToolTip::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
}
}
// this structure holds all the tooltip information
struct ToolTipInfo : public CObject
{
UINT nControlID; // ID of the control
UINT nInfoSize; // number of lines in the info
CStringArray nControlInfo; // container of the information
COLORREF nTextColor; // text color
COLORREF nBackColor; // background color
};
class CMFECToolTip : public CWnd
{
// Construction
public:
CMFECToolTip(); // standard constructor
virtual ~CMFECToolTip(); // destructor
void Create(CWnd* pWnd);
void ErasePreviousToolTipDisplay( UINT );
void ShowToolTip( UINT ); // explicitly shown the tooltip given the control ID
// NOTE: the user must override the PreTranslateMessage in the calling window in order
// to handle mousemovent.
void ShowToolTip( CPoint& ); // called only during Mousemovement
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon