📄 pptooltip.cpp
字号:
return &LogFont;
}
/////////////////////////////////////////////////////////////////
// Prints the formating string of the tooltip
//
// Parameters:
// pDC - [in] The device context to print the string
// str - [in] The formating string for drawing
// rect - [in] The rectangle to draws the tooltip
// bEnableAlign - [in] If TRUE align's operators are enables.
//
// Return values:
// None.
//---------------------------------------------------------------
// Format of string:
// <b> text </b> - The bold string
// <i> text </i> - The italic string
// <u> text </u> - The underline string
// <s> text </s> - The strike out string
//
// <cb=0x123456> text </cb> - The background color (RGB (12,34,56))
// <ct=0x123456> text </ct> - The text color (RGB (12,34,56))
//
// <cbi=1> text </cbi> - The index background color(0 - F)
// <cti=1> text </cti> - The index text color(0 - F)
//
// <al> or <al_l> - sets align to left edge
// <al_c> - sets align to the center
// <al_r> - sets align to the right edge
//
// <hr=100%> - the horizontal line with length 100%
// <hr=32> - the horizontal line with length 32 pixels.
//
// <a="link"> text </a> - The hyperlink
//
// <h=1> text </h> - hot zone (number of zone)
//
// <br[=1]> - new line (numbers of the lines)
// <t[=1]> - tabulation (number of tabulations)
//
// <img[=0x0000] [cx=0] [cy=0]> - draws the image by his name
// <ilst[=0x0000]> - draws the image from image list
// <bmp[=0x0000] mask[=0xFF00FF] [cx=0] [cy=0]> - draws bitmap
// <icon[=0x0000] [cx=0] [cy=0]> - draws the icon
////////////////////////////////////////////////////////////////
CSize CPPToolTip::PrintTitleString(CDC * pDC, CRect rect, CString str, BOOL bCalculate /* = TRUE */)
{
enum{ CMD_NONE = 0,
CMD_BOLD,
CMD_ITALIC,
CMD_STRIKE,
CMD_UNDERLINE,
CMD_COLOR_TEXT,
CMD_COLOR_TEXT_INDEX,
CMD_COLOR_BK,
CMD_COLOR_BK_INDEX,
CMD_NEW_LINE,
CMD_TABULATION,
CMD_HORZ_LINE,
CMD_HORZ_LINE_PERCENT,
CMD_DRAW_IMAGE,
CMD_DRAW_IMAGE_LIST,
CMD_DRAW_BITMAP,
CMD_DRAW_BITMAP_MASK,
CMD_DRAW_ICON
};
enum{ ALIGN_LEFT = 0,
ALIGN_CENTER,
ALIGN_RIGHT
};
//Clears the length of the lines
if (bCalculate)
{
m_nLengthLines.RemoveAll();
m_nHeightLines.RemoveAll();
}
int nLine = 0;
int nCmd = CMD_NONE;
int nAlign = ALIGN_LEFT;
BOOL bCloseTag = FALSE;
CSize sz(0, 0);
CSize szLine (0, 0);
CSize szIcon (0, 0); //The size of icon
if (str.IsEmpty())
return sz;
CPoint pt = rect.TopLeft();
CPoint ptCur = pt;
// Copies default logfont's structure
LOGFONT lf;
memcpy(&lf, &m_LogFont, sizeof(LOGFONT));
CFont font;
font.CreateFontIndirect(&lf);
CFont * pOldFont = pDC->SelectObject(&font);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int nHeight = tm.tmHeight; //The height of the font
int nWidth = tm.tmAveCharWidth; //The width of the font
CString strTag = _T(""); // Tag's name
CString strText = _T(""); // The current text to output
CString sParam = _T(""); // The text parameter
UINT nParam = 0, nParam1 = 0;
int nLineHeight = bCalculate ? nHeight : m_nHeightLines.GetAt(0); //The height of the current line
CUIntArray percent;
percent.Add(0);
int nTemp = 0; //the temporary variable
BOOL bFirstOutput = TRUE;
for (int i = 0; i <= str.GetLength(); i++)
{
if (i < str.GetLength())
{
nCmd = CMD_NONE;
strText = SearchBeginOfTag(str, i);
if (strText.IsEmpty())
{
//Tag was found
strTag = GetNameOfTag(str, i);
bCloseTag = (strTag.GetAt(0) == _T('/')) ? TRUE : FALSE;
if (bCloseTag)
strTag = strTag.Right(strTag.GetLength() - 1);
if (!strTag.CompareNoCase(_T("b")))
{
nCmd = CMD_BOLD;
}
else if (!strTag.CompareNoCase(_T("i")))
{
nCmd = CMD_ITALIC;
}
else if (!strTag.CompareNoCase(_T("u")))
{
nCmd = CMD_UNDERLINE;
}
else if (!strTag.CompareNoCase(_T("s")))
{
nCmd = CMD_STRIKE;
}
else if (!strTag.CompareNoCase(_T("br")))
{
nCmd = CMD_NEW_LINE;
nParam = GetUIntValue(str, i, 1);
}
else if (!strTag.CompareNoCase(_T("t")))
{
nCmd = CMD_TABULATION;
nParam = GetUIntValue(str, i, 1);
}
else if (strTag.GetAt(0) == _T('\n'))
{
nCmd = CMD_NEW_LINE;
nParam = 1;
}
else if (strTag.GetAt(0) == _T('\t'))
{
nCmd = CMD_TABULATION;
nParam = 1;
}
else if (!strTag.CompareNoCase(_T("ct")))
{
nCmd = CMD_COLOR_TEXT;
nParam = GetUIntValue(str, i, (UINT)m_crColor[PPTOOLTIP_COLOR_FG]);
}
else if (!strTag.CompareNoCase(_T("cti")))
{
nCmd = CMD_COLOR_TEXT;
nParam = GetUIntValue(str, i, PPTOOLTIP_COLOR_FG);
}
else if (!strTag.CompareNoCase(_T("cb")))
{
nCmd = CMD_COLOR_BK;
nParam = GetUIntValue(str, i, (UINT)m_crColor[PPTOOLTIP_COLOR_BK_BEGIN]);
}
else if (!strTag.CompareNoCase(_T("cbi")))
{
nCmd = CMD_COLOR_BK_INDEX;
nParam = GetUIntValue(str, i, PPTOOLTIP_COLOR_BK_BEGIN);
}
else if (!strTag.CompareNoCase(_T("al")) || !strTag.CompareNoCase(_T("al_l")))
{
nAlign = ALIGN_LEFT;
}
else if (!strTag.CompareNoCase(_T("al_c")))
{
if (!bCalculate)
nAlign = ALIGN_CENTER;
}
else if (!strTag.CompareNoCase(_T("al_r")))
{
if (!bCalculate)
nAlign = ALIGN_RIGHT;
}
else if (!strTag.CompareNoCase(_T("hr")))
{
sParam = GetStringValue(str, i);
if (!sParam.IsEmpty())
nParam = _tcstoul(sParam, 0, 0);
else
nParam = 100;
nCmd = (sParam.Right(1) == _T("%"))? CMD_HORZ_LINE_PERCENT : CMD_HORZ_LINE;
}
else if (!strTag.CompareNoCase(_T("img")))
{
nCmd = CMD_DRAW_IMAGE;
sParam = GetStringValue(str, i);
szIcon = CSize(0, 0);
//Gets two param
for (nTemp = 0; nTemp < 2; nTemp++)
{
strTag = GetPropertiesOfTag(str, i);
if (!strTag.CompareNoCase(_T("cx")))
szIcon.cx = GetUIntValue(str, i, 0);
else if (!strTag.CompareNoCase(_T("cy")))
szIcon.cy = GetUIntValue(str, i, 0);
}
}
else if (!strTag.CompareNoCase(_T("ilst")))
{
nCmd = CMD_DRAW_IMAGE_LIST;
nParam = GetUIntValue(str, i, 0);
}
else if (!strTag.CompareNoCase(_T("icon")))
{
nCmd = CMD_DRAW_ICON;
nParam = GetUIntValue(str, i, 0);
szIcon = CSize(0, 0);
//Gets two param
for (nTemp = 0; nTemp < 2; nTemp++)
{
strTag = GetPropertiesOfTag(str, i);
if (!strTag.CompareNoCase(_T("cx")))
szIcon.cx = GetUIntValue(str, i, 0);
else if (!strTag.CompareNoCase(_T("cy")))
szIcon.cy = GetUIntValue(str, i, 0);
}
}
else if (!strTag.CompareNoCase(_T("bmp")))
{
nCmd = CMD_DRAW_BITMAP;
nParam = GetUIntValue(str, i, 0);
sParam.Empty();
//Gets three param
for (nTemp = 0; nTemp < 3; nTemp++)
{
strTag = GetPropertiesOfTag(str, i);
if (!strTag.CompareNoCase(_T("mask")))
{
sParam = strTag;
nParam1 = GetUIntValue(str, i, 0xFF00FF);
}
else if (!strTag.CompareNoCase(_T("cx")))
szIcon.cx = GetUIntValue(str, i, 0);
else if (!strTag.CompareNoCase(_T("cy")))
szIcon.cy = GetUIntValue(str, i, 0);
}
}
else nCmd = CMD_NONE;
SearchEndOfTag(str, i);
}
else
{
//If text to output is exist
if (bFirstOutput)
{
switch (nAlign)
{
case ALIGN_CENTER:
ptCur.x = pt.x + (rect.Width() - m_nLengthLines.GetAt(nLine)) / 2;
break;
case ALIGN_RIGHT:
ptCur.x = pt.x + rect.Width() - m_nLengthLines.GetAt(nLine);
break;
}
}
szLine = pDC->GetTextExtent(strText);
if (bCalculate)
nLineHeight = max(nLineHeight, szLine.cy);
else
pDC->TextOut(ptCur.x, ptCur.y + m_nHeightLines.GetAt(nLine) - nHeight, strText);
ptCur.x += szLine.cx;
strText = _T("");
bFirstOutput = FALSE;
i--;
}
}
else
{
nCmd = CMD_NEW_LINE;
nParam = 1;
}
//Prepares to first draw in line
switch (nCmd)
{
case CMD_DRAW_IMAGE:
case CMD_DRAW_IMAGE_LIST:
case CMD_DRAW_BITMAP:
case CMD_DRAW_ICON:
if (bFirstOutput)
{
switch (nAlign)
{
case ALIGN_CENTER:
ptCur.x = pt.x + (rect.Width() - m_nLengthLines.GetAt(nLine)) / 2;
break;
case ALIGN_RIGHT:
ptCur.x = pt.x + rect.Width() - m_nLengthLines.GetAt(nLine);
break;
}
bFirstOutput = FALSE;
}
break;
}
//Executes command
switch (nCmd)
{
case CMD_BOLD:
//Bold text
pDC->SelectObject(pOldFont);
font.DeleteObject();
lf.lfWeight = m_LogFont.lfWeight;
if (!bCloseTag)
{
lf.lfWeight *= 2;
if (lf.lfWeight > FW_BLACK)
lf.lfWeight = FW_BLACK;
}
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
break;
case CMD_ITALIC:
//Italic text
pDC->SelectObject(pOldFont);
font.DeleteObject();
lf.lfItalic = bCloseTag ? FALSE : TRUE;
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
break;
case CMD_STRIKE:
//Strikeout text
pDC->SelectObject(pOldFont);
font.DeleteObject();
lf.lfStrikeOut = bCloseTag ? FALSE : TRUE;
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
break;
case CMD_UNDERLINE:
//Underline text
pDC->SelectObject(pOldFont);
font.DeleteObject();
lf.lfUnderline = bCloseTag ? FALSE : TRUE;
font.CreateFontIndirect(&lf);
pDC->SelectObject(&font);
break;
case CMD_COLOR_TEXT:
//Color of the text
pDC->SetTextColor((COLORREF)nParam);
break;
case CMD_COLOR_TEXT_INDEX:
//Indexed color of the text
if (nParam < PPTOOLTIP_MAX_COLORS)
pDC->SetTextColor(m_crColor[nParam]);
break;
case CMD_COLOR_BK:
//Color of the background
pDC->SetBkColor((COLORREF)nParam);
pDC->SetBkMode(bCloseTag ? TRANSPARENT : OPAQUE);
break;
case CMD_COLOR_BK_INDEX:
//Indexed color of the background
if (nParam < PPTOOLTIP_MAX_COLORS)
{
pDC->SetBkColor(m_crColor[nParam]);
pDC->SetBkMode(bCloseTag ? TRANSPARENT : OPAQUE);
}
break;
case CMD_HORZ_LINE_PERCENT:
//Horizontal line with percent length
if (bCalculate)
{
percent.SetAt(nLine, percent.GetAt(nLine) + nParam);
nParam = 0;
}
else nParam = ::MulDiv(rect.Width(), nParam, 100);
case CMD_HORZ_LINE:
//Horizontal line with absolute length
//If text to output is exist
if (!bCalculate)
DrawHorzLine(pDC, ptCur.x, ptCur.x + nParam, ptCur.y + m_nHeightLines.GetAt(nLine) / 2);
ptCur.x += nParam;
break;
case CMD_DRAW_IMAGE:
if (!sParam.IsEmpty())
{
if (bCalculate)
{
szLine = DrawResource(sParam, pDC, ptCur, 0, szIcon, bCalculate);
nLineHeight = max (nLineHeight, szLine.cy);
}
else
szLine = DrawResource(sParam, pDC, ptCur, m_nHeightLines.GetAt(nLine), szIcon, bCalculate);
ptCur.x += szLine.cx;
}
break;
case CMD_DRAW_IMAGE_LIST:
if (m_imgTooltip.m_hImageList != NULL)
{
if (bCalculate)
{
szLine = DrawIconFromImageList(pDC, ptCur, m_szImage, m_imgTooltip, nParam, bCalculate);
nLineHeight = max (nLineHeight, szLine.cy);
}
else
{
szLine = DrawIconFromImageList(pDC, ptCur, m_szImage, m_imgTooltip, nParam, bCalculate);
}
// If in one line a few bitmap with different height, then store max height
ptCur.x += szLine.cx; //m_szImage.cx;
}
break;
case CMD_DRAW_BITMAP:
if (nParam != 0)
{
if (bCalculate)
{
szLine = DrawBitmap(pDC, ptCur, 0, nParam, !sParam.IsEmpty(), nParam1, szIcon, bCalculate);
nLineHeight = max (nLineHeight, szLine.cy);
}
else
{
szLine = DrawBitmap(pDC, ptCur, m_nHeightLines.GetAt(nLine), nParam, !sParam.IsEmpty(), nParam1, szIcon, bCalculate);
}
// If in one line a few bitmap with different height, then store max height
ptCur.x += szLine.cx;
}
case CMD_DRAW_ICON:
if (nParam != 0)
{
if (bCalculate)
{
szLine = DrawIcon(pDC, ptCur, 0, nParam, szIcon, bCalculate);
nLineHeight = max (nLineHeight, szLine.cy);
}
else
{
szLine = DrawIcon(pDC, ptCur, m_nHeightLines.GetAt(nLine), nParam, szIcon, bCalculate);
}
// If in one line a few bitmap with different height, then store max height
ptCur.x += szLine.cx;
}
break;
case CMD_NEW_LINE:
//New line
if (!nParam)
nParam = 1;
if (bCalculate)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -