MyDraw.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:22k
- #include "stdafx.h"
- #include "math.h"
- #include "MyDraw.h"
- #include "MyDefine.h"
- CDrawGrapObject::CDrawGrapObject()
- {
- OnceFlag = FALSE;
- }
- void CDrawGrapObject::Initialize()
- {
- }
- void CDrawGrapObject::SetOnceFlag()
- {
- OnceFlag = TRUE;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CDrawGrapObject::DrawLineXOR(CDC* pDC, POINT StartPoint, POINT EndPoint, POINT oldEndPoint)
- {
- //基于CDC
- pDC->SetROP2(R2_NOT);
- if(OnceFlag == FALSE)
- {
- pDC->MoveTo(StartPoint);
- pDC->LineTo(oldEndPoint);
- }
- OnceFlag = FALSE;
-
- pDC->MoveTo(StartPoint);
- pDC->LineTo(EndPoint);
-
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawLine(CDC* pDC, POINT StartPoint, POINT EndPoint, int LineStyle,
- COLORREF PenColor)
- {
- //基于CDC
- CPen Pen;
- CPen* OldPen;
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
- pDC->SetROP2(R2_COPYPEN);
- pDC->MoveTo(StartPoint);
- pDC->LineTo(EndPoint);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::DrawSelLine(CDC* pDC, POINT StartPoint, POINT EndPoint, int LineStyle,
- COLORREF PenColor)
- {
- CPen* Pen;
- CPen* OldPen;
- CPoint MiddlePoint;
- RECT rect;
- Pen = new CPen(PS_DOT, 1, PenColor);
- OldPen = pDC->SelectObject(Pen);
-
- pDC->MoveTo(StartPoint);
- pDC->LineTo(EndPoint);
- pDC->SelectObject(OldPen);
- delete Pen;
- Pen = new CPen(PS_SOLID, 1, RGB(0, 0, 255));
- OldPen = pDC->SelectObject(Pen);
- pDC->SelectStockObject(NULL_BRUSH);
- rect.left = StartPoint.x - SELECT_RECT_WIDTH;
- rect.top = StartPoint.y - SELECT_RECT_WIDTH;
- rect.right = StartPoint.x + SELECT_RECT_WIDTH + 1;
- rect.bottom = StartPoint.y + SELECT_RECT_WIDTH + 1;
- pDC->Rectangle(&rect);
- MiddlePoint.x = (StartPoint.x + EndPoint.x) / 2;
- MiddlePoint.y = (StartPoint.y + EndPoint.y) / 2;
- rect.left = MiddlePoint.x - SELECT_RECT_WIDTH;
- rect.top = MiddlePoint.y - SELECT_RECT_WIDTH;
- rect.right = MiddlePoint.x + SELECT_RECT_WIDTH + 1;
- rect.bottom = MiddlePoint.y + SELECT_RECT_WIDTH + 1;
- pDC->Rectangle(&rect);
- rect.left = EndPoint.x - SELECT_RECT_WIDTH;
- rect.top = EndPoint.y - SELECT_RECT_WIDTH;
- rect.right = EndPoint.x + SELECT_RECT_WIDTH + 1;
- rect.bottom = EndPoint.y + SELECT_RECT_WIDTH + 1;
- pDC->Rectangle(&rect);
- pDC->SelectObject(OldPen);
- pDC->SetROP2(R2_COPYPEN);
- delete Pen;
- }
- void CDrawGrapObject::CancelDrawLine(CDC* pDC, POINT StartPoint, POINT EndPoint)
- {
- pDC->SetROP2(R2_NOT);
- pDC->MoveTo(StartPoint);
- pDC->LineTo(EndPoint);
-
- pDC->SetROP2(R2_COPYPEN);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- void CDrawGrapObject::DrawRectXOR(CDC* pDC, POINT StartPoint, POINT EndPoint, POINT oldEndPoint)
- {
- //基于CDC
- pDC->SetROP2(R2_NOT);
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- CRect rect;
- if(OnceFlag == FALSE)
- {
- rect.left = StartPoint.x;
- rect.top = StartPoint.y;
- rect.right = oldEndPoint.x;
- rect.bottom = oldEndPoint.y;
- rect.NormalizeRect();
- pDC->Rectangle(rect.left, rect.top, rect.right + 1, rect.bottom +1);
- }
- OnceFlag = FALSE;
- rect.left = StartPoint.x;
- rect.top = StartPoint.y;
- rect.right = EndPoint.x;
- rect.bottom = EndPoint.y;
- rect.NormalizeRect();
- pDC->Rectangle(rect.left, rect.top, rect.right + 1, rect.bottom + 1);
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawRect(CDC* pDC, POINT StartPoint, POINT EndPoint, int LineStyle,
- COLORREF PenColor)
- {
- //基于CDC
- CPen Pen;
- CPen* OldPen;
-
- CRect rect;
- rect.left = StartPoint.x;
- rect.top = StartPoint.y;
- rect.right = EndPoint.x;
- rect.bottom = EndPoint.y;
- rect.NormalizeRect();
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- pDC->SetROP2(R2_COPYPEN);
- //注意画矩形时,不包括右下角那一点,所以右下角x,y坐标加1
- pDC->Rectangle(rect.left, rect.top, rect.right + 1, rect.bottom + 1);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::CancelDrawRect(CDC* pDC, POINT StartPoint, POINT EndPoint)
- {
- pDC->SetROP2(R2_NOT);
- pDC->SelectStockObject(NULL_BRUSH);
- CRect rect;
- rect.left = StartPoint.x;
- rect.top = StartPoint.y;
- rect.right = EndPoint.x;
- rect.bottom = EndPoint.y;
- rect.NormalizeRect();
- //注意画矩形时,不包括右下角那一点,所以右下角x,y坐标加1
- pDC->Rectangle(rect.left, rect.top, rect.right + 1, rect.bottom + 1);
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawSelRect(CDC* pDC, POINT StartPoint, POINT EndPoint, int LineStyle,
- COLORREF PenColor)
- {
- CPen *Pen;
- CPen* OldPen;
- CRect rect;
- CPoint mid;
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- Pen = new CPen(PS_DOT, 1, PenColor);
- OldPen = pDC->SelectObject(Pen);
-
- rect.left = StartPoint.x;
- rect.top = StartPoint.y;
- rect.right = EndPoint.x;
- rect.bottom = EndPoint.y;
- rect.NormalizeRect();
- //注意画矩形时,不包括右下角那一点,所以右下角x,y坐标加1
- pDC->Rectangle(rect.left, rect.top, rect.right + 1, rect.bottom + 1);
- pDC->SelectObject(OldPen);
- delete Pen;
- Pen = new CPen(PS_SOLID, 1, RGB(0, 0, 255));
- OldPen = pDC->SelectObject(Pen);
- pDC->Rectangle(rect.left - SELECT_RECT_WIDTH, rect.top - SELECT_RECT_WIDTH,
- rect.left + SELECT_RECT_WIDTH + 1, rect.top + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(rect.left - SELECT_RECT_WIDTH, rect.bottom - SELECT_RECT_WIDTH,
- rect.left + SELECT_RECT_WIDTH + 1, rect.bottom + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(rect.right - SELECT_RECT_WIDTH, rect.top - SELECT_RECT_WIDTH,
- rect.right + SELECT_RECT_WIDTH + 1, rect.top + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(rect.right - SELECT_RECT_WIDTH, rect.bottom - SELECT_RECT_WIDTH,
- rect.right + SELECT_RECT_WIDTH + 1, rect.bottom + SELECT_RECT_WIDTH + 1);
- mid.x = (rect.left + rect.right) / 2;
- mid.y = rect.top;
- pDC->Rectangle(mid.x - SELECT_RECT_WIDTH, mid.y - SELECT_RECT_WIDTH,
- mid.x + SELECT_RECT_WIDTH + 1, mid.y + SELECT_RECT_WIDTH + 1);
- mid.x = (rect.left + rect.right) / 2;
- mid.y = rect.bottom;
- pDC->Rectangle(mid.x - SELECT_RECT_WIDTH, mid.y - SELECT_RECT_WIDTH,
- mid.x + SELECT_RECT_WIDTH + 1, mid.y + SELECT_RECT_WIDTH + 1);
-
- mid.x = rect.left;
- mid.y = (rect.top + rect.bottom) / 2;
- pDC->Rectangle(mid.x - SELECT_RECT_WIDTH, mid.y - SELECT_RECT_WIDTH,
- mid.x + SELECT_RECT_WIDTH + 1, mid.y + SELECT_RECT_WIDTH + 1);
- mid.x = rect.right;
- mid.y = (rect.top + rect.bottom) / 2;
- pDC->Rectangle(mid.x - SELECT_RECT_WIDTH, mid.y - SELECT_RECT_WIDTH,
- mid.x + SELECT_RECT_WIDTH + 1, mid.y + SELECT_RECT_WIDTH + 1);
- pDC->SelectObject(OldPen);
- delete Pen;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CDrawGrapObject::DrawCircleXOR(CDC* pDC, POINT StartPoint, POINT EndPoint,
- POINT oldEndPoint)
- {
- RECT rect;
- long Radius;
- //基于CDC
- pDC->SetROP2(R2_NOT);
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- if(OnceFlag == FALSE)
- {
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(oldEndPoint.x, oldEndPoint.y);
-
- Radius = (long)sqrt(pow((float)(StartPoint.x - oldEndPoint.x), 2) + pow((float)(StartPoint.y - oldEndPoint.y), 2));
- rect.left = StartPoint.x - Radius;
- rect.top = StartPoint.y - Radius;
- rect.right = StartPoint.x + Radius;
- rect.bottom = StartPoint.y + Radius;
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- }
- OnceFlag = FALSE;
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(EndPoint.x, EndPoint.y);
- Radius = (long)sqrt(pow((float)(StartPoint.x - EndPoint.x), 2) + pow((float)(StartPoint.y - EndPoint.y), 2));
- rect.left = StartPoint.x - Radius;
- rect.top = StartPoint.y - Radius;
- rect.right = StartPoint.x + Radius;
- rect.bottom = StartPoint.y + Radius;
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawCircle(CDC* pDC, POINT StartPoint, POINT EndPoint, int LineStyle,
- COLORREF PenColor)
- {
- CPen Pen;
- CPen* OldPen;
- RECT rect;
- long Radius;
- pDC->SetROP2(R2_NOT);
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(EndPoint.x, EndPoint.y);
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- pDC->SetROP2(R2_COPYPEN);
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
- Radius = (long)sqrt(pow((float)(StartPoint.x - EndPoint.x), 2) + pow((float)(StartPoint.y - EndPoint.y), 2));
- rect.left = StartPoint.x - Radius;
- rect.top = StartPoint.y - Radius;
- rect.right = StartPoint.x + Radius;
- rect.bottom = StartPoint.y + Radius;
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::DrawCircle(CDC* pDC, POINT StartPoint, long Radius, int LineStyle,
- COLORREF PenColor)
- {
- CPen Pen;
- CPen* OldPen;
- RECT rect;
-
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- pDC->SetROP2(R2_COPYPEN);
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
-
- rect.left = StartPoint.x - Radius;
- rect.top = StartPoint.y - Radius;
- rect.right = StartPoint.x + Radius;
- rect.bottom = StartPoint.y + Radius;
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::CancelDrawCircle(CDC* pDC, POINT StartPoint, POINT EndPoint)
- {
- RECT rect;
- long Radius;
- pDC->SetROP2(R2_NOT);
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(EndPoint.x, EndPoint.y);
-
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- Radius = (long)sqrt(pow((float)(StartPoint.x - EndPoint.x), 2) + pow((float)(StartPoint.y - EndPoint.y), 2));
-
- rect.left = (StartPoint.x) - Radius;
- rect.top = (StartPoint.y) - Radius;
- rect.right = (StartPoint.x) + Radius;
- rect.bottom = (StartPoint.y) + Radius;
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawSelCircle(CDC* pDC, POINT StartPoint, long Radius, int LineStyle,
- COLORREF PenColor)
- {
- CPen *Pen;
- CPen* OldPen;
- RECT rect;
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- rect.left = StartPoint.x - Radius;
- rect.top = StartPoint.y - Radius;
- rect.right = StartPoint.x + Radius;
- rect.bottom = StartPoint.y + Radius;
- //用异或擦除原有的圆(圆比较特殊,需先擦除原来的)
- Pen = new CPen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(Pen);
- pDC->SetROP2(R2_NOT);
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SelectObject(OldPen);
- delete Pen;
- //重画线形为点线的圆,保持颜色不变
- Pen = new CPen(PS_DOT, 1, PenColor);
- OldPen = pDC->SelectObject(Pen);
- pDC->SetROP2(R2_COPYPEN);
- pDC->Ellipse(rect.left, rect.top, rect.right, rect.bottom);
- pDC->SelectObject(OldPen);
- delete Pen;
-
- Pen = new CPen(PS_SOLID, 1, RGB(0, 0, 255));
- OldPen = pDC->SelectObject(Pen);
- pDC->Rectangle(StartPoint.x - SELECT_RECT_WIDTH, StartPoint.y - SELECT_RECT_WIDTH,
- StartPoint.x + SELECT_RECT_WIDTH + 1, StartPoint.y + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(StartPoint.x - SELECT_RECT_WIDTH, StartPoint.y - Radius - SELECT_RECT_WIDTH ,
- StartPoint.x + SELECT_RECT_WIDTH + 1, StartPoint.y - Radius + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(StartPoint.x - SELECT_RECT_WIDTH, StartPoint.y + Radius - SELECT_RECT_WIDTH,
- StartPoint.x + SELECT_RECT_WIDTH + 1, StartPoint.y + Radius + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(StartPoint.x - Radius - SELECT_RECT_WIDTH, StartPoint.y - SELECT_RECT_WIDTH,
- StartPoint.x - Radius + SELECT_RECT_WIDTH + 1, StartPoint.y + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(StartPoint.x + Radius - SELECT_RECT_WIDTH, StartPoint.y - SELECT_RECT_WIDTH,
- StartPoint.x + Radius + SELECT_RECT_WIDTH + 1, StartPoint.y + SELECT_RECT_WIDTH + 1);
- //注意以下两句顺序不可颠倒
- OldPen = pDC->SelectObject(Pen);
- pDC->SelectObject(OldPen);
- delete Pen;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- void CDrawGrapObject::DrawArcXOR(CDC* pDC, POINT StartPoint, POINT MidPoint, POINT oldMidPoint,
- POINT EndPoint, POINT oldEndPoint, int times)
- {
- static int flag;
- POINT OriginPoint;
- double originX;
- double originY;
- double k1;
- double k2;
- double mid1X;
- double mid1Y;
- double mid2X;
- double mid2Y;
- long x1, y1, x2, y2;
- long Radius;
- pDC->SetROP2(R2_NOT);
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- switch(times)
- {
- case 1:
- if(OnceFlag == FALSE)
- {
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(oldMidPoint.x, oldMidPoint.y);
- }
- OnceFlag = FALSE;
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(MidPoint.x, MidPoint.y);
- flag = 1;
- break;
- case 2:
- RECT rect;
- if(flag == 1)
- {
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(MidPoint.x, MidPoint.y);
- flag = 0;
- }
-
- if( OnceFlag == FALSE)
- {
- if(StartPoint.y == MidPoint.y)
- k1 = 1000000.0;
- else
- k1 = (- (double)(StartPoint.x - MidPoint.x) / (double)(StartPoint.y - MidPoint.y));
- if(oldEndPoint.y == MidPoint.y)
- k2 = 1000000.0;
- else
- k2 = (- (double)(oldEndPoint.x - MidPoint.x) / (double)(oldEndPoint.y - MidPoint.y));
- mid1X = (double)(StartPoint.x + MidPoint.x) / 2.0;
- mid1Y = (double)(StartPoint.y + MidPoint.y) / 2.0;
- mid2X = (double)(oldEndPoint.x + MidPoint.x) / 2.0;
- mid2Y = (double)(oldEndPoint.y + MidPoint.y) / 2.0;
- if(k1 == k2)
- return;
- else
- {
- if(k1 == 1000000.0)
- {
- originX = mid1X;
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- else
- if(k2 == 1000000.0)
- {
- originX = mid2X;
- originY = k1 * (originX - mid1X) + mid1Y;
- }
- else
- {
- originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- Radius = (long)sqrt(pow((double)(originX - StartPoint.x), 2) + pow((double)(originY - StartPoint.y), 2));
- OriginPoint.x = (long)originX;
- OriginPoint.y = (long)originY ;
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- x1 = MidPoint.x - StartPoint.x;
- y1 = MidPoint.y - StartPoint.y;
- x2 = oldEndPoint.x - StartPoint.x;
- y2 = oldEndPoint.y - StartPoint.y;
- //判断优弧
- if(x1*y2 - x2*y1 < 0)
- pDC->Arc(&rect, StartPoint, oldEndPoint);
- else
- pDC->Arc(&rect, oldEndPoint, StartPoint);
- }
- }
- OnceFlag = FALSE;
- if(StartPoint.y == MidPoint.y)
- k1 = 1000000.0;
- else
- k1 = (- (double)(StartPoint.x - MidPoint.x) / (double)(StartPoint.y - MidPoint.y));
- if(EndPoint.y == MidPoint.y)
- k2 = 1000000.0;
- else
- k2 = (- (double)(EndPoint.x - MidPoint.x) / (double)(EndPoint.y - MidPoint.y));
- mid1X = (double)(StartPoint.x + MidPoint.x) / 2.0;
- mid1Y = (double)(StartPoint.y + MidPoint.y) / 2.0;
- mid2X = (double)(EndPoint.x + MidPoint.x) / 2.0;
- mid2Y = (double)(EndPoint.y + MidPoint.y) / 2.0;
- if(k1 == k2)
- return;
- else
- {
- if(k1 == 1000000.0)
- {
- originX = mid1X;
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- else
- if(k2 == 1000000.0)
- {
- originX = mid2X;
- originY = k1 * (originX - mid1X) + mid1Y;
- }
- else
- {
- originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- Radius = (long)sqrt(pow((double)(originX - StartPoint.x), 2) + pow((double)(originY - StartPoint.y), 2));
- OriginPoint.x = (long)originX;
- OriginPoint.y = (long)originY ;
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- x1 = MidPoint.x - StartPoint.x;
- y1 = MidPoint.y - StartPoint.y;
- x2 = EndPoint.x - StartPoint.x;
- y2 = EndPoint.y - StartPoint.y;
- //判断优弧
- if(x1*y2 - x2*y1 < 0)
- pDC->Arc(&rect, StartPoint, EndPoint);
- else
- pDC->Arc(&rect, EndPoint, StartPoint);
- }
- break;
- }
- pDC->SetROP2(R2_COPYPEN);
- }
- void CDrawGrapObject::DrawArc(CDC* pDC, POINT StartPoint, POINT MidPoint, POINT EndPoint,
- int LineStyle, COLORREF PenColor)
- {
- CPen Pen;
- CPen* OldPen;
- POINT OriginPoint;
- double originX;
- double originY;
- double k1;
- double k2;
- double mid1X;
- double mid1Y;
- double mid2X;
- double mid2Y;
- RECT rect;
- long Radius;
- long x1, y1, x2, y2;
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- pDC->SetROP2(R2_COPYPEN);
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
- if(StartPoint.y == MidPoint.y)
- k1 = 1000000.0;
- else
- k1 = (- (double)(StartPoint.x - MidPoint.x) / (double)(StartPoint.y - MidPoint.y));
- if(EndPoint.y == MidPoint.y)
- k2 = 1000000.0;
- else
- k2 = (- (double)(EndPoint.x - MidPoint.x) / (double)(EndPoint.y - MidPoint.y));
- mid1X = (double)(StartPoint.x + MidPoint.x) / 2.0;
- mid1Y = (double)(StartPoint.y + MidPoint.y) / 2.0;
- mid2X = (double)(EndPoint.x + MidPoint.x) / 2.0;
- mid2Y = (double)(EndPoint.y + MidPoint.y) / 2.0;
- if(k1 == k2)
- return;
-
- if(k1 == 1000000.0)
- {
- originX = mid1X;
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- else
- if(k2 == 1000000.0)
- {
- originX = mid2X;
- originY = k1 * (originX - mid1X) + mid1Y;
- }
- else
- {
- originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- Radius = (long)sqrt(pow((double)(originX - StartPoint.x), 2) + pow((double)(originY - StartPoint.y), 2));
- OriginPoint.x = (long)originX;
- OriginPoint.y = (long)originY ;
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- x1 = MidPoint.x - StartPoint.x;
- y1 = MidPoint.y - StartPoint.y;
- x2 = EndPoint.x - StartPoint.x;
- y2 = EndPoint.y - StartPoint.y;
- if(x1*y2 - x2*y1 < 0)
- pDC->Arc(&rect, StartPoint, EndPoint);
- else
- pDC->Arc(&rect, EndPoint, StartPoint);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::DrawArc(CDC* pDC, POINT OriginPoint, POINT StartPoint, POINT EndPoint, long Radius,
- int LineStyle, COLORREF PenColor)
- {
- CPen Pen;
- CPen* OldPen;
- RECT rect;
- Pen.CreatePen(LineStyle, 1, PenColor);
- OldPen = pDC->SelectObject(&Pen);
- pDC->SetROP2(R2_COPYPEN);
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- pDC->Arc(&rect, StartPoint, EndPoint);
- pDC->SelectObject(OldPen);
- }
- void CDrawGrapObject::CancelDrawArc(CDC* pDC, POINT StartPoint, POINT MidPoint,
- POINT EndPoint, int times)
- {
- POINT OriginPoint;
- double originX;
- double originY;
- double k1;
- double k2;
- double mid1X;
- double mid1Y;
- double mid2X;
- double mid2Y;
- RECT rect;
- long Radius;
- long x1, y1, x2, y2;
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- pDC->SetROP2(R2_NOT);
- if(times == 1)
- {
- pDC->MoveTo(StartPoint.x, StartPoint.y);
- pDC->LineTo(MidPoint.x, MidPoint.y);
- return;
- }
- if(StartPoint.y == MidPoint.y)
- k1 = 1000000.0;
- else
- k1 = (- (double)(StartPoint.x - MidPoint.x) / (double)(StartPoint.y - MidPoint.y));
- if(EndPoint.y == MidPoint.y)
- k2 = 1000000.0;
- else
- k2 = (- (double)(EndPoint.x - MidPoint.x) / (double)(EndPoint.y - MidPoint.y));
- mid1X = (double)(StartPoint.x + MidPoint.x) / 2.0;
- mid1Y = (double)(StartPoint.y + MidPoint.y) / 2.0;
- mid2X = (double)(EndPoint.x + MidPoint.x) / 2.0;
- mid2Y = (double)(EndPoint.y + MidPoint.y) / 2.0;
- if(k1 == k2)
- return;
-
- if(k1 == 1000000.0)
- {
- originX = mid1X;
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- else
- if(k2 == 1000000.0)
- {
- originX = mid2X;
- originY = k1 * (originX - mid1X) + mid1Y;
- }
- else
- {
- originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
- originY = k2 * (originX - mid2X) + mid2Y;
- }
- Radius = (long)sqrt(pow((double)(originX - StartPoint.x), 2) + pow((double)(originY - StartPoint.y), 2));
- OriginPoint.x = (long)originX;
- OriginPoint.y = (long)originY ;
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- x1 = MidPoint.x - StartPoint.x;
- y1 = MidPoint.y - StartPoint.y;
- x2 = EndPoint.x - StartPoint.x;
- y2 = EndPoint.y - StartPoint.y;
- if(x1*y2 - x2*y1 < 0)
- pDC->Arc(&rect, StartPoint, EndPoint);
- else
- pDC->Arc(&rect, EndPoint, StartPoint);
- }
- void CDrawGrapObject::DrawSelArc(CDC* pDC, POINT OriginPoint, POINT StartPoint, POINT EndPoint,
- long Radius, int LineStyle, COLORREF PenColor)
- {
- CPen *Pen;
- CPen* OldPen;
- RECT rect;
-
- //设置不填充状态
- pDC->SelectStockObject(NULL_BRUSH);
- //重画线形为点线的圆弧
- Pen = new CPen(PS_DOT, 1, PenColor);
- OldPen = pDC->SelectObject(Pen);
- pDC->SetROP2(R2_COPYPEN);
- rect.left = OriginPoint.x - Radius;
- rect.top = OriginPoint.y - Radius;
- rect.right = OriginPoint.x + Radius;
- rect.bottom = OriginPoint.y + Radius;
- pDC->Arc(&rect, StartPoint, EndPoint);
- pDC->SelectObject(OldPen);
- delete Pen;
-
- Pen = new CPen(PS_SOLID, 1, RGB(0, 0, 255));
- OldPen = pDC->SelectObject(Pen);
- pDC->Rectangle(StartPoint.x - SELECT_RECT_WIDTH, StartPoint.y - SELECT_RECT_WIDTH ,
- StartPoint.x + SELECT_RECT_WIDTH + 1, StartPoint.y + SELECT_RECT_WIDTH + 1);
- pDC->Rectangle(EndPoint.x - SELECT_RECT_WIDTH, EndPoint.y - SELECT_RECT_WIDTH,
- EndPoint.x + SELECT_RECT_WIDTH + 1, EndPoint.y + SELECT_RECT_WIDTH + 1);
- //注意以下两句顺序不可颠倒
- OldPen = pDC->SelectObject(Pen);
- pDC->SelectObject(OldPen);
- delete Pen;
- }