📄 lines.cpp
字号:
if (ptLast.x >= 0)
{
DoMouseLine (hdc, ptLast, point, color);
}
ReleaseDC (hWnd, hdc);
ptLast = point;
break;
case WM_MOUSEMOVE:
if (nFlags & MK_RBUTTON)
{
DoMouseEvent (hWnd, WM_RBUTTONDOWN, nFlags, point, color);
break;
}
if (!(nFlags & MK_LBUTTON))
return;
if (bTracking)
{
POINT pt;
pt = point;
hdc = GetDC (hWnd);
DoMouseLine (hdc, ptLineStart, ptLineEnd, color);
DoMouseLine (hdc, ptLineStart, pt, color);
ReleaseDC (hWnd, hdc);
}
ptLineEnd = point;
bTracking = true;
break;
}
}
void DoMouseLine (HDC hdc, POINT & ptLineStart, POINT & ptLineEnd, COLORREF color)
{
HPEN hOldPen;
HPEN hPen = CreatePen (PS_SOLID, 2, color ^ 0x00ffffff);
int nOldMode = SetROP2 (hdc, R2_XORPEN);// R2_NOT);
MoveToEx (hdc, ptLineStart.x, ptLineStart.y, NULL);
hOldPen = (HPEN) SelectObject (hdc, hPen);
LineTo (hdc, ptLineEnd.x, ptLineEnd.y);
SetROP2 (hdc, nOldMode);
SelectObject (hdc, hOldPen);
DeleteObject (hPen);
}
void DoPolyLine (HWND hWnd, HDC hdc, bool bMenuClicked)
{
RECT rcClient;
POINT *pPoint;
POINT pOldOrigin;
LOGFONT lf;
HPEN hOldPen, hPen;
HBRUSH hOldBrush, hBrush;
HFONT hFont, hOldFont;
double fAngle, fRadius;
int nStartAt;
if (bMenuClicked)
nStartAt = 2;
else
nStartAt = 50;
hPen = CreatePen (PS_SOLID, 2, 0x0);
hOldPen = (HPEN) SelectObject (hdc, hPen);
hBrush = CreateSolidBrush (0x00ffffff);
hOldBrush = (HBRUSH) SelectObject (hdc, hBrush);
GetClientRect (hWnd, &rcClient);
CreateLogFont (hdc, lf, 14);
hFont = CreateFontIndirect (&lf);
hOldFont = (HFONT) SelectObject (hdc, hFont);
//
// Try it changing the viewport. Comment out the line below and
// uncomment the following line. Also uncomment the two lines
// at the bottom of the function.
//
SetViewportOrgEx (hdc, rcClient.right / 2, rcClient.bottom / 2, &pOldOrigin);
// SetViewportOrgEx (hdc, rcClient.right / 4, rcClient.bottom / 2, &pOldOrigin);
fRadius = rcClient.bottom / 3;
RECT newClient;
RECT rcText;
newClient.left = -rcClient.right / 2;
newClient.right = rcClient.right / 2;
newClient.top = -rcClient.bottom / 2;
newClient.bottom = rcClient.bottom / 2;
rcText.left = -rcClient.bottom / 5;
rcText.right = -rcText.left;
rcText.top = -lf.lfHeight / 2;
rcText.bottom = - rcText.top;
for (int cSides = nStartAt; cSides < 51; ++cSides)
{
FillRect (hdc, &newClient, hBrush);
fAngle = PI / 2;
pPoint = new POINT[cSides + 1];
for (int i = 0; i < cSides; ++i)
{
pPoint[i].x = (long) (fRadius * cos (fAngle) + 0.5);
pPoint[i].y = (long) (-fRadius * sin (fAngle) + 0.5);
fAngle += 2 * PI / cSides;
}
pPoint[cSides] = pPoint[0];
Polyline (hdc, pPoint, cSides + 1);
delete [] pPoint;
if (i > 2)
{
char strText [_MAX_PATH];
sprintf (strText, "Sides = %d", i);
DrawText (hdc, strText, strlen (strText), &rcText, TEXT_FORMAT);
}
Sleep (300);
}
// SetViewportOrgEx (hdc, 3 * rcClient.right / 4, rcClient.bottom / 2, NULL);
// Ellipse (hdc, (int) -fRadius, (int) -fRadius, (int) fRadius, (int) fRadius);
SelectObject (hdc, hOldPen);
SelectObject (hdc, hOldBrush);
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
DeleteObject (hPen);
DeleteObject (hBrush);
SetViewportOrgEx (hdc, pOldOrigin.x, pOldOrigin.y, NULL);
}
void DoArc (HWND hWnd, HDC hdc)
{
POINT ptOldOrigin;
POINT ptStart, ptEnd;
HPEN hPen, hOldPen;
HBRUSH hBrush, hOldBrush;
RECT rcClient;
double fAngle, fRadius;
hBrush = CreateSolidBrush (0x00ffffff);
hOldBrush = (HBRUSH) SelectObject (hdc, hBrush);
hPen = CreatePen (PS_SOLID, 2, 0x0);
hOldPen = (HPEN) SelectObject (hdc, hPen);
GetClientRect (hWnd, &rcClient);
SetViewportOrgEx (hdc, rcClient.right / 2, rcClient.bottom / 2, &ptOldOrigin);
ptEnd.x = 150;
ptEnd.y = 0;
fAngle = (2 * PI) / 5;
fRadius = 150.0;
RECT rcCircle = {-150, -150, 150, 150};
for (int i = 0; i < 10; ++i)
{
if (i == 5)
{
ptEnd.x = 150;
ptEnd.y = 0;
fAngle = (2 * PI) / 5;
}
ptStart = ptEnd;
ptEnd.x = (long) (fRadius * cos (fAngle) + 0.5);
ptEnd.y = (long) (-fRadius * sin (fAngle) + 0.5);
Arc (hdc, -150, -150, 150, 150, ptStart.x, ptStart.y, ptEnd.x, ptEnd.y);
fAngle += (2 * PI) / 5;
if (i < 5)
{
Sleep (300);
FillRect (hdc, &rcCircle, hBrush);
}
}
SetViewportOrgEx (hdc, ptOldOrigin.x, ptOldOrigin.y, NULL);
SelectObject (hdc, hOldPen);
SelectObject (hdc, hOldBrush);
}
void CreateLogFont (HDC hdc, LOGFONT & lf, int nPoints, char *szFace)
{
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 = MulDiv(nPoints, GetDeviceCaps(hdc, LOGPIXELSY), 72);
strcpy (lf.lfFaceName, szFace);
}
void DoPieChart (HWND hWnd, HDC hdc)
{
RECT rcClient;
POINT pOldOrigin;
POINT pPoint[6];
//int nVals[] = {72, 156, 108, 84, 276};
int nVals[] = {72, 16, 148, 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);
}
void DoPieChart2 (HWND hWnd, HDC hdc)
{
RECT rcClient;
POINT pOldOrigin;
POINT pPoint[6];
POINT ptCenter;
POINT pExplodePoint[6];
POINT pShadowPoint[6];
int nVals[] = {72, 156, 108, 84, 276};
int nSum, i;
HBRUSH hBrush[5], hOldBrush, hbrBlack;
HPEN hPen, hOldPen;
double fAngle, fRadius;
double fExplodeAngle, fExplodeRadius;
double fShadowRadius;
COLORREF crColors[] = {0x00ffff00, 0x00ff00ff, 0x001111ff,
0x0000ffff, 0x0011ff11};
GetClientRect (hWnd, &rcClient);
ptCenter.x = rcClient.right / 4;
ptCenter.y = rcClient.bottom / 2;
SetViewportOrgEx (hdc, ptCenter.x, ptCenter.y, &pOldOrigin);
hbrBlack = CreateSolidBrush (0x00777777);
hOldBrush = (HBRUSH) SelectObject (hdc, hbrBlack);
hPen = CreatePen (PS_SOLID, 2, 0);
hOldPen = (HPEN) SelectObject (hdc, hPen);
fRadius = rcClient.bottom / 3;
fExplodeRadius = 10;
fShadowRadius = 5;
fAngle = PI / 4;
SetViewportOrgEx (hdc, rcClient.right / 4 + 4, rcClient.bottom / 2 + 4, &pOldOrigin);
Ellipse (hdc, (int)(-fRadius) + 12, (int) -fRadius + 12, (int) fRadius + 12, (int) fRadius + 12);
SelectObject (hdc, hOldBrush);
DeleteObject (hbrBlack);
hbrBlack = CreateSolidBrush (0x0);
SelectObject (hdc, hbrBlack);
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];
}
for (i = 0; i < 5; ++i)
{
pPoint[i].x = (long) (fRadius * cos (fAngle) + 0.5);
pPoint[i].y = (long) (-fRadius * sin (fAngle) + 0.5);
fExplodeAngle = fAngle;
fAngle += (nVals[i] * 2.0 * PI) / nSum;
fExplodeAngle += (nVals[i] * 2.0 * PI) / (nSum * 2.0);
pExplodePoint[i].x = (long) (fExplodeRadius * cos (fExplodeAngle) + 0.5);
pExplodePoint[i].y = (long) (-fExplodeRadius * sin (fExplodeAngle) + 0.5);
pShadowPoint[i].x = abs((int)(long) (fShadowRadius * cos (fExplodeAngle) + 0.5));
pShadowPoint[i].y = abs ((int)(long) (-fShadowRadius * sin (fExplodeAngle) + 0.5));
}
pPoint[5] = pPoint[0];
for (i = 0; i < 5; ++i)
{
// SelectObject (hdc, hbrBlack);
// SetViewportOrgEx (hdc, ptCenter.x + pShadowPoint[i].x + pExplodePoint[i].x, ptCenter.y +pShadowPoint[i].y + pExplodePoint[i].x, NULL);
// Pie (hdc, (int) -fRadius, (int) -fRadius, (int) fRadius, (int) fRadius, pPoint[i].x, pPoint[i].y, pPoint[i+1].x, pPoint[i+1].y);
SetViewportOrgEx (hdc, ptCenter.x + pExplodePoint[i].x, ptCenter.y +pExplodePoint[i].y, NULL);
// 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, 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);
SetViewportOrgEx (hdc, ptCenter.x, ptCenter.y, NULL);
SelectObject (hdc, hOldBrush);
DeleteObject (hBrush[i]);
}
SelectObject (hdc, hOldPen);
DeleteObject (hbrBlack);
SetViewportOrgEx (hdc, pOldOrigin.x, pOldOrigin.y, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -