📄 drawview.cpp
字号:
// drawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "draw.h"
#include "drawDoc.h"
#include "drawView.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
//{{AFX_MSG_MAP(CDrawView)
ON_COMMAND(ID_BUTTON32771, OnButton32771)
ON_COMMAND(ID_BUTTON32772, OnButton32772)
ON_COMMAND(ID_BUTTON32773, OnButton32773)
ON_COMMAND(ID_BUTTON32774, OnButton32774)
ON_COMMAND(ID_BUTTON32775, OnButton32775)
ON_COMMAND(ID_BUTTON32776, OnButton32776)
ON_COMMAND(ID_BUTTON32777, OnButton32777)
ON_COMMAND(ID_BUTTON32778, OnButton32778)
ON_COMMAND(ID_BUTTON32779, OnButton32779)
ON_WM_LBUTTONUP()
ON_COMMAND(ID_ZOOMUP, OnZoomup)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{ //建立一个透明的画刷并选入CDC中
LOGBRUSH lpLogBrush;//画刷结构
lpLogBrush.lbStyle=BS_HOLLOW;//画刷结构属性为透明
CBrush mybrush;
mybrush.CreateBrushIndirect(&lpLogBrush);
pDC->SelectObject(mybrush );
//画刷选取
CDrawDoc* pDoc = GetDocument();
POSITION pos = pDoc->mygraph.GetHeadPosition();
for (int i=0;i < pDoc->mygraph.GetCount();i++)
{thegraph& pthegraph=pDoc->mygraph.GetNext(pos);
switch(pthegraph.type)//根据图形对象类型来画
{case 1:
{//如果为一点则画此点(包括临时的点)
pDC->Ellipse(pthegraph.point1.x-2,pthegraph.point1.y-2,pthegraph.point1.x+2,pthegraph.point1.y+2);
break;
}
case 2:
{//如果为直线则画两个端点和一条连线
pDC->Ellipse(pthegraph.point1.x-2,pthegraph.point1.y-2,pthegraph.point1.x+2,pthegraph.point1.y+2);
pDC->Ellipse(pthegraph.point2.x-2,pthegraph.point2.y-2,pthegraph.point2.x+2,pthegraph.point2.y+2);
pDC->MoveTo(pthegraph.point1);
pDC->LineTo(pthegraph.point2);
break;
}
case 3:
{//如果为圆则画圆心和圆外那一点和圆圈
pDC->Ellipse(pthegraph.point1.x-2,pthegraph.point1.y-2,pthegraph.point1.x+2,pthegraph.point1.y+2);
pDC->Ellipse(pthegraph.point2.x-2,pthegraph.point2.y-2,pthegraph.point2.x+2,pthegraph.point2.y+2);
int x1,x2,y1,y2,r;
x1=pthegraph.point1.x;
x2=pthegraph.point2.x;
y1=pthegraph.point1.y;
y2=pthegraph.point2.y;
r=(int)sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
pDC->Ellipse(pthegraph.point1.x-r,pthegraph.point1.y-r,pthegraph.point1.x+r,pthegraph.point1.y+r);
break;
}
default:break;
}
}
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CDrawView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// TODO: calculate the total size of this view
CDrawDoc* pDoc = GetDocument();
zoomnum=pDoc->windowsSize.cx/2048;//计算放大的倍数
SetScrollSizes(MM_TEXT, pDoc->windowsSize);//设置窗口尺寸完毕
ScrollToPosition(CPoint(pDoc->windowsSize.cx/2-512,pDoc->windowsSize.cy/2-384));//将起始位置设为纸的中间完毕
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
void CDrawView::OnButton32771()
{
// TODO: Add your command handler code here
CDrawDoc* pDoc = GetDocument();
if(index==21)pDoc->mygraph.RemoveTail();//去掉临时点
index=10;
}
void CDrawView::OnButton32772()
{
// TODO: Add your command handler code here
CDrawDoc* pDoc = GetDocument();
if(index==11)pDoc->mygraph.RemoveTail();//去掉临时点
index=20;
}
void CDrawView::OnButton32773()
{
// TODO: Add your command handler code here
index=30;
}
void CDrawView::OnButton32774()
{
// TODO: Add your command handler code here
index=40;
}
void CDrawView::OnButton32775()
{
// TODO: Add your command handler code here
index=50;
}
void CDrawView::OnButton32776()
{
// TODO: Add your command handler code here
index=60;
}
void CDrawView::OnButton32777()
{
// TODO: Add your command handler code here
index=70;
}
void CDrawView::OnButton32778()
{
// TODO: Add your command handler code here
index=80;
}
void CDrawView::OnButton32779()
{
// TODO: Add your command handler code here
index=90;
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//将point的值由设备坐标转换为逻辑坐标
CClientDC dc(this);
OnPrepareDC(&dc);
dc.DPtoLP(&point);
//转换完毕
tonearbypoint(point);//转移到己有的邻近点上
CDrawDoc* pDoc = GetDocument();
switch(index)
{
case 10:{pDoc->mygraph.AddTail(thegraph(point));
index=11;
pDoc->UpdateAllViews(NULL);
}
break;
case 11:{pDoc->mygraph.GetTail().point2=point;
pDoc->mygraph.GetTail().type=2;
index=10;
pDoc->SetModifiedFlag( );//每产生一个对象设置一次文档改动标签
pDoc->UpdateAllViews(NULL);
}
break;
case 20:{pDoc->mygraph.AddTail(thegraph(point));
index=21;
pDoc->UpdateAllViews(NULL);
}
break;
case 21:{pDoc->mygraph.GetTail().point2=point;
pDoc->mygraph.GetTail().type=3;
index=20;
pDoc->SetModifiedFlag( );//每产生一个对象设置一次文档改动标签
pDoc->UpdateAllViews(NULL);
}
break;
default:break;
}
CView::OnLButtonUp(nFlags, point);
}
void CDrawView::tonearbypoint(CPoint& point)
{//如果点击的是一个很接近己有点的点,则修正此点为己有点
CDrawDoc* pDoc = GetDocument();
POSITION pos = pDoc->mygraph.GetHeadPosition();
for (int i=0;i < pDoc->mygraph.GetCount();i++)
{
thegraph& pthegraph=pDoc->mygraph.GetNext(pos);
switch(pthegraph.type)
{
case 1:if(abs(pthegraph.point1.x-point.x)<2&&abs(pthegraph.point1.y-point.y)<2)point=pthegraph.point1;break;
case 2:
case 3:if(abs(pthegraph.point1.x-point.x)<2&&abs(pthegraph.point1.y-point.y)<2)point=pthegraph.point1;
if(abs(pthegraph.point2.x-point.x)<2&&abs(pthegraph.point2.y-point.y)<2)point=pthegraph.point2;break;
case 4:if(abs(pthegraph.point1.x-point.x)<2&&abs(pthegraph.point1.y-point.y)<2)point=pthegraph.point1;
if(abs(pthegraph.point2.x-point.x)<2&&abs(pthegraph.point2.y-point.y)<2)point=pthegraph.point2;
if(abs(pthegraph.point3.x-point.x)<2&&abs(pthegraph.point3.y-point.y)<2)point=pthegraph.point3;break;
}
}
}
void CDrawView::OnZoomup()
{
// TODO: Add your command handler code here
if(zoomnum<4)
{zoomnum++;
CDrawDoc* pDoc = GetDocument();
pDoc->windowsSize=CSize(2*pDoc->windowsSize.cx,2*pDoc->windowsSize.cy);
SetScrollSizes(MM_TEXT,pDoc->windowsSize);
}
else MessageBox("现在己经是最大纸张了!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -