📄 myrect.cpp
字号:
// MyRect.cpp: implementation of the CMyRect class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "geditor.h"
#include "MyRect.h"
#include "prop.h"
#include "geditorView.h"
#include "Polygon.h"
#include <cmath>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CMyRect, CShape,1)
CMyRect::CMyRect(CPoint& pt1,CPoint& pt2)
{
RotateNoInit=0;
edgeclr=0;
m_tRect.left = pt1.x, m_tRect.right = pt2.x;
m_tRect.top = pt1.y, m_tRect.bottom = pt2.y;
m_tRect.NormalizeRect();
}
CMyRect::CMyRect()
{
RotateNoInit=0;
edgeclr=0;
}
CMyRect::~CMyRect()
{
}
void CMyRect::Draw(CDC* pDC)
{
CBrush brush((COLORREF)m_color);
CBrush* oldBrush = pDC->SelectObject(&brush);
CPen pen(linesty,width,(COLORREF)edgeclr);
CPen *oldPen = pDC->SelectObject(&pen);
if (fills) pDC->Rectangle(m_tRect);
else DrawEdge(pDC);
CPoint pt;
if ((dragmode>=1)&&(dragmode<=9))
UpdateProp(0);
if (dragmode!=0)
{
CPen pens(PS_SOLID,1,RGB(0,0,0));
pDC->SelectObject(&pens);
CBrush bs(RGB(255,255,255));
pDC->SelectObject(&bs);
int RecWd=4;
pDC->Rectangle(m_tRect.left-RecWd,m_tRect.top-RecWd,m_tRect.left+RecWd,m_tRect.top+RecWd);
pDC->Rectangle(m_tRect.right-RecWd,m_tRect.top-RecWd,m_tRect.right+RecWd,m_tRect.top+RecWd);
pDC->Rectangle(m_tRect.left-RecWd,m_tRect.bottom-RecWd,m_tRect.left+RecWd,m_tRect.bottom+RecWd);
pDC->Rectangle(m_tRect.right-RecWd,m_tRect.bottom-RecWd,m_tRect.right+RecWd,m_tRect.bottom+RecWd);
int Mid=(int)((m_tRect.left+m_tRect.right)/2);
pDC->Rectangle(Mid-RecWd,m_tRect.top-RecWd,Mid+RecWd,m_tRect.top+RecWd);
pDC->Rectangle(Mid-RecWd,m_tRect.bottom-RecWd,Mid+RecWd,m_tRect.bottom+RecWd);
Mid=(int)((m_tRect.top+m_tRect.bottom)/2);
pDC->Rectangle(m_tRect.left-RecWd,Mid-RecWd,m_tRect.left+RecWd,Mid+RecWd);
pDC->Rectangle(m_tRect.right-RecWd,Mid-RecWd,m_tRect.right+RecWd,Mid+RecWd);
DeleteObject(pens);
};
pDC->SelectObject(oldBrush);
pDC->SelectObject(oldPen);
DeleteObject(pen);
DeleteObject(brush);
}
void CMyRect::SetRect(CPoint& pt1,CPoint& pt2)
{
m_tRect.left = pt1.x, m_tRect.right = pt2.x;
m_tRect.top = pt1.y, m_tRect.bottom = pt2.y;
m_tRect.NormalizeRect();
}
void CMyRect::Serialize(CArchive& ar)
{
//Call base class Serialize()
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar<<m_tRect.left<<m_tRect.top<<m_tRect.right<<m_tRect.bottom;
ar<<name<<m_color<<dragmode<<linesty;
}
else
{
ar>>m_tRect.left>>m_tRect.top>>m_tRect.right>>m_tRect.bottom;
ar>>name>>m_color>>dragmode>>linesty;
}
}
#ifdef _DEBUG
void CMyRect::AssertValid() const
{
CObject::AssertValid();
}
void CMyRect::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif //_DEBUG
void CMyRect::dragobj(int leash,CDC *pDC, CPoint point, CGeditorView *pView)
{
invalid(pView);
switch (dragmode)
{
case 1://move all
m_tRect.left=m_tRect.left+point.x-movept.x;//deltaX
m_tRect.right=m_tRect.right+point.x-movept.x;
m_tRect.top=m_tRect.top+point.y-movept.y;
m_tRect.bottom=m_tRect.bottom+point.y-movept.y;
break;
case 2://(left,top)
m_tRect.left=m_tRect.left+point.x-movept.x;
m_tRect.top=m_tRect.top+point.y-movept.y;
break;
case 3://(right,bottom)
m_tRect.right=m_tRect.right+point.x-movept.x;
m_tRect.bottom=m_tRect.bottom+point.y-movept.y;
break;
case 4://(left,bottom)
m_tRect.left=m_tRect.left+point.x-movept.x;
m_tRect.bottom=m_tRect.bottom+point.y-movept.y;
break;
case 5://(right,top)
m_tRect.right=m_tRect.right+point.x-movept.x;
m_tRect.top=m_tRect.top+point.y-movept.y;
break;
case 6://top
m_tRect.top=m_tRect.top+point.y-movept.y;
break;
case 7://bottom
m_tRect.bottom=m_tRect.bottom+point.y-movept.y;
break;
case 8://left
m_tRect.left=m_tRect.left+point.x-movept.x;
break;
case 9:
m_tRect.right=m_tRect.right+point.x-movept.x;
break;
case 0:
break;
default:
break;
};
movept=point;
Draw(pDC);
}
int CMyRect::InTest(CPoint pt)
{
HCURSOR m_hCrossCursor;
int temp=0;
int Mid=(int)((m_tRect.left+m_tRect.right)/2);
if ((abs(m_tRect.left-pt.x)<=4)&&(abs(m_tRect.top-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
::SetCursor(m_hCrossCursor);
return 2;
}
else if ((abs(m_tRect.right-pt.x)<=4)&&(abs(m_tRect.bottom-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
::SetCursor(m_hCrossCursor);
return 3;
}
else if ((abs(m_tRect.left-pt.x)<=4)&&(abs(m_tRect.bottom-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENESW);
::SetCursor(m_hCrossCursor);
return 4;
}
else if ((abs(m_tRect.right-pt.x)<=4)&&(abs(m_tRect.top-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENESW);
::SetCursor(m_hCrossCursor);
return 5;
}
else if ((abs(Mid-pt.x)<=4)&&(abs(m_tRect.top-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENS);
::SetCursor(m_hCrossCursor);
return 6;
}
else if ((abs(Mid-pt.x)<=4)&&(abs(m_tRect.bottom-pt.y)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZENS);
::SetCursor(m_hCrossCursor);
return 7;
};
Mid=(int)((m_tRect.top+m_tRect.bottom)/2);
if ((abs(Mid-pt.y)<=4)&&(abs(m_tRect.left-pt.x)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
::SetCursor(m_hCrossCursor);
return 8;
};
if ((abs(Mid-pt.y)<=4)&&(abs(m_tRect.right-pt.x)<=4))
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
::SetCursor(m_hCrossCursor);
return 9;
};
if ((pt.x>m_tRect.left)&&(pt.x<m_tRect.right)&&
(pt.y<m_tRect.bottom)&&(pt.y>m_tRect.top)&&(fills))//填充模式
{
m_hCrossCursor=AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);
::SetCursor(m_hCrossCursor);
return 1;
};
if ((PtInEdge(pt))&&(!fills))
{
::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSEL));
return 1;
};
//is out of all point:
if ((pt.x<m_tRect.left)||(pt.x>m_tRect.right)||
(pt.y>m_tRect.bottom)||(pt.y<m_tRect.top))
return 0;
return 0;
}
CShape * CMyRect::copys(TCHAR &ch)
{
CMyRect *r;
r=new CMyRect;
r->m_tRect=m_tRect;
r->m_tRect.left+=10;
r->m_tRect.right+=10;
r->m_tRect.top+=10;
r->m_tRect.bottom+=10;
ch='R';
return r;
}
void CMyRect::rotate(int rsel,CDC *pDC, CPoint pt, CGeditorView *pView)
{
if((rsel==1)&&(RotateNoInit==1))//90度
{
invalid(pView);
RotateNoInit=0;
int x0,y0;
x0=(int)((m_tRect.left+m_tRect.right)/2);
y0=(int)((m_tRect.top+m_tRect.bottom)/2);
int ht=abs(m_tRect.bottom-m_tRect.top);
int wd=abs(m_tRect.right-m_tRect.left);
m_tRect.left=x0-(int)(ht/2);
m_tRect.right=x0+(int)(ht/2);
m_tRect.top=y0-(int)(wd/2);
m_tRect.bottom=y0+(int)(wd/2);
Draw(pDC);
}
else if((rsel==0)&&(dragmode!=1))
{//以下将Rect-=>Polygon
CPolygon *py;
py=new CPolygon;
CPoint tpt;
tpt.x=m_tRect.left;
tpt.y=m_tRect.top;
py->AddPoint(tpt);
tpt.x=m_tRect.right;
tpt.y=m_tRect.top;
py->AddPoint(tpt);
tpt.x=m_tRect.right;
tpt.y=m_tRect.bottom;
py->AddPoint(tpt);
tpt.x=m_tRect.left;
tpt.y=m_tRect.bottom;
py->AddPoint(tpt);
POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition( );
CDocTemplate* docTemplate = AfxGetApp()->GetNextDocTemplate(pos);
ASSERT_VALID(docTemplate);
pos = docTemplate->GetFirstDocPosition( );
CGeditorDoc* pDoc = (CGeditorDoc*)docTemplate->GetNextDoc(pos);
ASSERT_VALID(pDoc);
py->SetColor(m_color);
pDoc->NumPgn++;
char nanum[2];
sprintf(nanum,"%2d",pDoc->NumPgn);
py->name="Pgn#";
py->name+=nanum;
//// p->m_name.AddString(py->name);
pDoc->CurObjNum=(pDoc->m_shapeList).GetCount();
// p->m_name.SetCurSel(pDoc->CurObjNum);
py->dragmode=10;
py->Draw(pDC);
pDoc->objitems+="P";
pDoc->m_shapeList.AddTail(py);
pDoc->SetModifiedFlag();
pView->DeleteObj(pView->objnumber);
pView->objnumber=pDoc->CurObjNum;
switch(dragmode)
{
case 2:
py->Curpt=0;
break;
case 3:
py->Curpt=2;
break;
case 4:
py->Curpt=3;
break;
case 5:
py->Curpt=1;
break;
default:
break;
};
if((dragmode==2)||(dragmode==3)||(dragmode==4)||(dragmode==5))
py->dragmode=2;
py->CalForRot(pt);
py->ChangeBase((m_tRect.left+m_tRect.right)/2,(m_tRect.top+m_tRect.bottom)/2);
pView->CurObj=py;
};
}
void CMyRect::Mirror(int syd, CDC *pDC, CPoint &basept, CGeditorView *pView)
{
invalid(pView);
int temp;
switch(syd)
{//原点对称可以是0 or 1
case 0://x zhou
temp=m_tRect.top;
m_tRect.top=2*basept.y-m_tRect.bottom;
m_tRect.bottom=2*basept.y-temp;
break;
case 1://y zhou
temp=m_tRect.left;
m_tRect.left=2*basept.x-m_tRect.right;
m_tRect.right=2*basept.x-temp;
break;
default:
break;
};
Draw(pDC);
}
void CMyRect::CalForRot(CPoint &pt)
{
RotateNoInit=1;
}
void CMyRect::invalid(CGeditorView *pView)
{
POINT apoly[2];
int delta=10;
apoly[0].x=m_tRect.left-delta;
apoly[0].y=m_tRect.top-delta;
apoly[1].x=m_tRect.right+delta;
apoly[1].y=m_tRect.bottom+delta;
CRect pRect(apoly[0],apoly[1]);
pView->InvalidateRect(pRect);
}
void CMyRect::InitProp()
{
}
void CMyRect::UpdateProp(int sel)
{
}
void CMyRect::DrawEdge(CDC *pDC)
{
CPen pen(linesty,width,m_color);
CPen *oldpen=pDC->SelectObject(&pen);
CPoint pt;
pt.x=m_tRect.left;
pt.y=m_tRect.top;
pDC->MoveTo(pt);
pt.x=m_tRect.right;
pt.y=m_tRect.top;
pDC->LineTo(pt);
pt.x=m_tRect.right;
pt.y=m_tRect.bottom;
pDC->LineTo(pt);
pt.x=m_tRect.left;
pt.y=m_tRect.bottom;
pDC->LineTo(pt);
pt.x=m_tRect.left;
pt.y=m_tRect.top;
pDC->LineTo(pt);
pDC->SelectObject(oldpen);
DeleteObject(pen);
}
BOOL CMyRect::PtInEdge(CPoint &pt)
{
int ner=2+width;
if (ner>10)
ner=width;
CRect rect;
rect.left=m_tRect.left-ner;
rect.right=m_tRect.right+ner;
rect.top=m_tRect.top-ner;
rect.bottom=m_tRect.top+ner;
if (rect.PtInRect(pt))
return TRUE;
rect.left=m_tRect.right-ner;
rect.right=m_tRect.right+ner;
rect.top=m_tRect.top-ner;
rect.bottom=m_tRect.bottom+ner;
if (rect.PtInRect(pt))
return TRUE;
rect.left=m_tRect.left-ner;
rect.right=m_tRect.right+ner;
rect.top=m_tRect.bottom-ner;
rect.bottom=m_tRect.bottom+ner;
if (rect.PtInRect(pt))
return TRUE;
rect.left=m_tRect.left-ner;
rect.right=m_tRect.left+ner;
rect.top=m_tRect.top-ner;
rect.bottom=m_tRect.bottom+ner;
if (rect.PtInRect(pt))
return TRUE;
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -