📄 text.cpp
字号:
nWidth = sz.cx;
s = szText;
nSpaces = 0;
while (*s)
{
if (*s == ' ')
++nSpaces;
++s;
}
int nPos = sz.cy;
GetTextExtentPoint32 (hdc, szText, strlen (szText), &sz);
if (nSpaces)
{
SetTextJustification (hdc, nWidth - sz.cx, nSpaces);
// DrawText (hdc, szText, strlen (szText), rc, (4 << 8) | DT_TABSTOP | DT_EXPANDTABS);
DrawText (hdc, szText, strlen (szText), rc, 0);
// TextOut (hdc, rc->left, rc->top, szText, strlen (szText));
// ExtTextOut (hdc, rc->left, rc->top, 0, NULL, szText, strlen (szText), NULL);
SetTextJustification (hdc, 0, nSpaces);
}
else
{
int nExtra = nWidth - sz.cx;
nExtra /= strlen (szText);
if (nExtra)
{
SetTextCharacterExtra (hdc, nExtra);
DrawText (hdc, szText, strlen (szText), rc, 0);
}
else
{
int *lpDx = new int [strlen (szText)];
memset (lpDx, 0, sizeof (int) * strlen (szText));
int nWidths[256];
GetCharWidth32 (hdc, 0, 255, nWidths);
for (unsigned int i = 0; i < strlen (szText); ++i)
lpDx[i] = nWidths[szText[i]];
for (i = 0; i < (unsigned int)(nWidth - sz.cx); ++i)
lpDx[i] += 1;
ExtTextOut (hdc, rc->left, rc->top, 0, NULL, szText, strlen (szText), lpDx);
delete [] lpDx;
}
// TextOut (hdc, 0, nPos, szText, strlen (szText));
// SetTextCharacterExtra (hdc, -nExtra);
}
return (0);
}
void DoCharWidth32 (HDC hdc, HWND hWnd)
{
HFONT hFont;
int nWidths[26];
ABC ABCWidths[26];
hFont = CreatePointFont (120, "Times New Roman", hdc);
HFONT hOldFont = (HFONT) SelectObject (hdc, hFont);
GetCharWidth32 (hdc, 'A', 'Z', nWidths);
GetCharABCWidths (hdc, 'A', 'Z', ABCWidths);
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
}
HFONT CreatePointFont (int nPoints, char *szFace, HDC hdc)
{
LOGFONT lf;
memset (&lf, '\0', sizeof (LOGFONT));
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfClipPrecision = OUT_TT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;
lf.lfHeight = nPoints;
strcpy (lf.lfFaceName, szFace);
return (CreatePointFontIndirect (&lf, hdc));
}
HFONT CreatePointFontIndirect (LOGFONT *lf, HDC hdc)
{
POINT ptView;
SetViewportOrgEx (hdc, 0, 0, &ptView);
POINT pt;
pt.y = ::GetDeviceCaps(hdc, LOGPIXELSY) * lf->lfHeight;
pt.x = ::GetDeviceCaps(hdc, LOGPIXELSX) * lf->lfHeight;
pt.y /= 720;
pt.x /= 720;
DPtoLP(hdc, &pt, 1);
POINT ptOrg = { 0, 0 };
DPtoLP(hdc, &ptOrg, 1);
lf->lfHeight = -abs(pt.y - ptOrg.y);
// lf->lfWidth = abs(pt.x - ptOrg.x);
SetViewportOrgEx (hdc, ptView.x, ptView.y, NULL);
return (CreateFontIndirect (lf));
}
int GetSizeInPicas (int nPicas, HDC hdc)
{
// nPicas *= 12;
POINT pt;
pt.y = ::GetDeviceCaps(hdc, LOGPIXELSX) * nPicas;
pt.y /= 72; // 72 points per inch
DPtoLP(hdc, &pt, 1);
POINT ptOrg = { 0, 0 };
DPtoLP(hdc, &ptOrg, 1);
return (abs(pt.y - ptOrg.y));
}
int CALLBACK DrawGrayText (HDC hdc, LPARAM lParam, int wParam);
struct GRAYTEXT
{
char *Text;
int x;
int y;
int Width;
int Depth;
int Tabs;
int *TabStops;
int TabOrigin;
};
void DoTabbedText (HDC hdc, HWND hWnd)
{
HFONT hFont, hOldFont;
int nTabs[2], nLineWidth, i;
SIZE size;
char *szText[] =
{
"Division\tSales\tReturns",
"Widgets\t12654\t345",
"Wombats\t96734\t3829",
"Weasels\t4583\t485"
};
HBRUSH hBrush = CreateSolidBrush (0x00ff00ff);
SelectObject (hdc, hBrush);
hFont = CreatePointFont (120, "Times New Roman", hdc);
hOldFont = (HFONT) SelectObject (hdc, hFont);
GetTextExtentPoint32 (hdc, szText[0], strlen (szText[0]), &size);
nLineWidth = GetSizeInPicas (180, hdc);
nTabs[0] = nTabs[1] = nLineWidth / 3;
MoveToEx (hdc, 0, size.cy, NULL);
LineTo (hdc, nLineWidth, size.cy);
TabbedTextOut (hdc, 0, 0, szText[0], strlen (szText[0]), 2, nTabs, 0);
int nPoints = size.cy;
size.cy += 3;
for (i = 1; i < (sizeof(szText)/sizeof (char *)); ++i)
{
TabbedTextOut (hdc, 0, size.cy, szText[i], strlen (szText[i]), 2, nTabs, 0);
size.cy += nPoints;
}
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
DeleteObject (hBrush);
}
void DoRotateText (HDC hdc, HWND hWnd)
{
RECT rcClient;
POINT ptOrigin;
HFONT hFont, hOldFont;
LOGFONT lf;
char *szText = " Hey! to the world ";
int nOldR2;
int nOldAlign;
GetClientRect (hWnd, &rcClient);
SetViewportOrgEx (hdc, rcClient.right / 2, rcClient.bottom / 2, &ptOrigin);
memset (&lf, '\0', sizeof (LOGFONT));
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfClipPrecision = OUT_TT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;
strcpy (lf.lfFaceName, "Times New Roman Bold");
lf.lfHeight = 240;
nOldAlign = SetTextAlign (hdc, TA_CENTER | TA_BOTTOM);
hOldFont = (HFONT) GetStockObject (SYSTEM_FONT);
nOldR2 = SetROP2(hdc, R2_XORPEN);
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfHeight = 240;
for (int i = 0; i < 3601; i += 5)
{
hFont = CreatePointFontIndirect (&lf, hdc);
SelectObject (hdc, hFont);
TextOut (hdc, 0, 0, szText, strlen (szText));
Sleep (5);
TextOut (hdc, 0, 0, szText, strlen (szText));
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
lf.lfEscapement = i;
lf.lfOrientation = i;
lf.lfHeight = 240;
}
SetROP2(hdc, nOldR2);
SetTextAlign (hdc, nOldAlign);
SetViewportOrgEx (hdc, ptOrigin.x, ptOrigin.y, NULL);
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
}
void DoPieChart (HWND hWnd, HDC hdc)
{
RECT rcClient;
POINT pOldOrigin;
POINT pPoint[6];
SIZE sizeText;
int nVals[] = {72, 22, 158, 104, 276};
int nSum, i;
int nOldBkMode;
HBRUSH hBrush[5], hOldBrush;
HPEN hPen, hOldPen;
double fAngle, fRadius, fTextAngle[5];
COLORREF crColors[] = {0x00ffff00, 0x00ff00ff, 0x001111ff,
0x0000ffff, 0x0011ff11};
HFONT hFont, hOldFont;
LOGFONT lf;
char *szText[] =
{
"Widgets",
"Wombats",
"Weasels",
"Wiseguys",
"Wallabies"
};
GetClientRect (hWnd, &rcClient);
if (rcClient.right > rcClient.bottom)
fRadius = rcClient.bottom / 3;
else
fRadius = rcClient.right / 3;
fAngle = PI / 4; // Start at 45 degrees
hBrush[0] = CreateSolidBrush (0x0);
SetViewportOrgEx (hdc, rcClient.right / 2 + 6,
rcClient.bottom / 2 + 6, &pOldOrigin);
hOldBrush = (HBRUSH) SelectObject (hdc, hBrush[0]);
hPen = CreatePen (PS_SOLID, 2, 0);
hOldPen = (HPEN) SelectObject (hdc, hPen);
Ellipse (hdc, (int) -fRadius, (int) -fRadius,
(int) fRadius, (int) fRadius);
SelectObject (hdc, hOldBrush);
DeleteObject (hBrush[0]);
SetViewportOrgEx (hdc, rcClient.right / 2,
rcClient.bottom / 2, NULL);
memset (&lf, '\0', sizeof (LOGFONT));
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfClipPrecision = OUT_TT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;
strcpy (lf.lfFaceName, "Times New Roman Bold");
nSum = 0;
for (i = 0; i < 5; ++i)
{
hBrush[i] = CreateSolidBrush (crColors[i]);
nSum += nVals[i];
}
hPen = CreatePen (PS_SOLID, 2, 0);
hOldPen = (HPEN) SelectObject (hdc, hPen);
hOldBrush = (HBRUSH) SelectObject (hdc, hBrush[0]);
lf.lfHeight = 90;
hFont = CreatePointFontIndirect (&lf, hdc);
hOldFont = (HFONT) SelectObject (hdc, hFont);
GetTextExtentPoint32 (hdc, szText[0], strlen (szText[0]), &sizeText);
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
double fTextOffset = asin (((double) sizeText.cy ) / fRadius);
fTextOffset *= 10.0 * 57.28578;
for (i = 0; i < 5; ++i)
{
pPoint[i].x = (long)(fRadius * cos (fAngle) + 0.5);
pPoint[i].y = (long)(-fRadius * sin (fAngle) + 0.5);
fTextAngle[i] = fAngle;
fAngle += (nVals[i] * 2.0 * PI) / nSum;
fTextAngle[i] += (fAngle - fTextAngle[i]) / 2;
fTextAngle[i] *= 57.29578;
fTextAngle[i] *= 10;
fTextAngle[i] += fTextOffset;
}
pPoint[5] = pPoint[0];
for (i = 0; i < 5; ++i)
{
SelectObject (hdc, hBrush[i]);
Pie (hdc, (int) -fRadius, (int) -fRadius,
(int) fRadius, (int) fRadius,
pPoint[i].x, pPoint[i].y,
pPoint[i+1].x, pPoint[i+1].y);
SelectObject (hdc, hOldBrush);
DeleteObject (hBrush[i]);
}
nOldBkMode = SetBkMode (hdc, TRANSPARENT);
for (i = 0; i < 5; ++i)
{
if ((fTextAngle[i] > 899) && (fTextAngle[i] < 2700))
{
lf.lfEscapement = (int) fTextAngle[i] + 1800;
lf.lfOrientation = (int) fTextAngle[i] + 1800;
}
else
{
lf.lfEscapement = (int) fTextAngle[i];
lf.lfOrientation = (int) fTextAngle[i];
}
lf.lfHeight = 90;
hFont = CreatePointFontIndirect (&lf, hdc);
SelectObject (hdc, hFont);
SIZE size;
GetTextExtentPoint32 (hdc, szText[i], strlen (szText[i]), &size);
POINT ptText;
ptText.x = (long)((fRadius * .9 - size.cx) * cos (fTextAngle[i]/572.9578) + 0.5);
ptText.y = (long)(-(fRadius * .9 - size.cx) * sin (fTextAngle[i]/572.9578) + 0.5);
if ((fTextAngle[i] > 899) && (fTextAngle[i] < 2700))
{
SetTextAlign (hdc, TA_RIGHT | TA_BOTTOM);
TextOut (hdc, ptText.x, ptText.y, szText[i], strlen (szText[i]));
}
else
{
SetTextAlign (hdc, TA_LEFT | TA_TOP);
TextOut (hdc, ptText.x, ptText.y, szText[i], strlen (szText[i]));
}
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
}
SetBkMode (hdc, nOldBkMode);
SelectObject (hdc, hOldPen);
DeleteObject (hPen);
SetViewportOrgEx(hdc, pOldOrigin.x, pOldOrigin.y, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -