📄 text.cpp
字号:
{
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 DoGrayedText (HDC hdc, HWND hWnd)
{
HFONT hFont, hOldFont;
int nPoints, nColumn;
HBRUSH hGrayBrush, hOldBrush;
RECT rcClient;
RECT rcMenu, rcText, rcStart;
char *szMenu[] =
{
"Menu Item 1",
"Menu Item 2",
"Menu Item 3",
"Menu Item 4"
};
hGrayBrush = CreateSolidBrush (0x00777777);
GetClientRect (hWnd, &rcClient);
hFont = CreatePointFont (100, "Times New Roman", hdc);
{
LOGFONT lf;
GetObject (hFont, sizeof (LOGFONT), &lf);
nPoints = abs(lf.lfHeight);
LOGBRUSH lb;
GetObject (GetStockObject (LTGRAY_BRUSH), sizeof (LOGBRUSH), &lb);
SetBkColor (hdc, lb.lbColor);
}
// hOldBrush = (HBRUSH) SelectObject (hdc, hGrayBrush);
hOldBrush = (HBRUSH) SelectObject (hdc, GetStockObject (LTGRAY_BRUSH));
hOldFont = (HFONT) SelectObject (hdc, hFont);
rcMenu.left = rcClient.right / 4 + 10;
rcMenu.top = rcClient.bottom / 4 + 5;
rcMenu.right = 3 * rcClient.right / 4 - 10;
rcMenu.bottom = 3 * rcClient.bottom / 4 - 5;
rcStart = rcMenu;
rcStart.bottom = rcStart.top + 1;
rcStart.right = rcStart.left + 1;
//may be ADINI_CLOSE, IDANI_OPEN or IDANI_CAPTION
// DrawAnimatedRects(hWnd, IDANI_CLOSE, &rcStart, &rcMenu);
// Rectangle (hdc, rcMenu.left, rcMenu.top, rcMenu.right, rcMenu.bottom);
rcText = rcMenu;
rcText.left += 10;
rcText.top += 5;
rcText.bottom = rcText.top + nPoints;
nColumn = rcText.right - rcText.left;
GrayString (hdc, NULL, DrawGrayText, (LPARAM) "HEY", 3, rcText.left, rcText.top, nColumn, nPoints);
// TextOut (hdc, rcText.left, rcText.top, "HEY", 3);
SelectObject (hdc, hOldBrush);
SelectObject (hdc, hOldFont);
DeleteObject (hGrayBrush);
DeleteObject (hFont);
}
BOOL CALLBACK DrawGrayText (HDC hdc, LPARAM lParam, int wParam)
{
GRAYTEXT *gt = (GRAYTEXT *) lParam;
//HBITMAP hBmp;
// hBmp = (HBITMAP) LoadImage (hInst, "E:\\CFiles\\VisualC++\\Hey\\Windows\\Patterns\\DUDLEY.BMP", IMAGE_BITMAP,
// 300, 300, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
HDC hTemp = CreateCompatibleDC (hdc);
// SelectObject (hTemp, hBmp);
TextOut (hTemp, 0, 25, (char *) lParam, wParam);
BitBlt (hdc, gt->x, gt->y + 100, gt->Width, gt->Depth, hTemp, 0, 0, SRCCOPY);
DeleteDC (hTemp);
// DeleteObject (hBmp);
// TabbedTextOut (hdc, gt->x, gt->y, gt->Text, strlen (gt->Text), gt->Tabs, gt->TabStops, gt->TabOrigin);
return (TRUE);
}
// TabbedTextOut (hdc, 0, sz.cy, szText[i], strlen (szText[i]), 2, nTabs, 0);
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];
int nVals[] = {72, 156, 108, 84, 276};
int nSum, i;
HBRUSH hBrush[5], hOldBrush;
HPEN hPen, hOldPen;
double fAngle, fRadius;
COLORREF crColors[] = {0x00ffff00, 0x00ff00ff, 0x001111ff,
0x0000ffff, 0x0011ff11};
GetClientRect (hWnd, &rcClient);
fRadius = rcClient.bottom / 3;
fAngle = PI / 4;
hBrush[0] = CreateSolidBrush (0x0);
SetViewportOrgEx (hdc, rcClient.right / 4 + 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 / 4,
rcClient.bottom / 2, NULL);
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]);
for (i = 0; i < 5; ++i)
{
pPoint[i].x = (long)(fRadius * cos (fAngle) + 0.5);
pPoint[i].y = (long)(-fRadius * sin (fAngle) + 0.5);
fAngle += (nVals[i] * 2.0 * PI) / nSum;
}
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]);
}
SelectObject (hdc, hOldPen);
DeleteObject (hPen);
SetViewportOrgEx(hdc, pOldOrigin.x, pOldOrigin.y, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -