📄 cadview.cpp
字号:
// cadView.cpp : implementation of the CCadView class
//
#include "stdafx.h"
#include "cad.h"
#include <cmath>
#include "cadDoc.h"
#include "cadView.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCadView
IMPLEMENT_DYNCREATE(CCadView, CView)
BEGIN_MESSAGE_MAP(CCadView, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CCadView)
ON_COMMAND(IDR_GRAPHICS_LINE, OnGraphicsLine)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(IDR_GRAPHICS_CIRCLE, OnGraphicsCircle)
ON_COMMAND(IDR_GRAPHICS_RECTANGLE, OnGraphicsRectangle)
ON_UPDATE_COMMAND_UI(IDR_GRAPHICS_CIRCLE, OnUpdateGraphicsCircle)
ON_UPDATE_COMMAND_UI(IDR_GRAPHICS_LINE, OnUpdateGraphicsLine)
ON_UPDATE_COMMAND_UI(IDR_GRAPHICS_RECTANGLE, OnUpdateGraphicsRectangle)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCadView construction/destruction
CCadView::CCadView()
{
// TODO: add construction code here
m_line=0;
m_start.x=m_start.y=m_end.x=0;
m_circle=m_rectangle=m_end.y=m_step=0;
}
CCadView::~CCadView()
{
}
BOOL CCadView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCadView drawing
void CCadView::OnDraw(CDC* pDC)
{
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
poldpen=(CPen *)pDC->SelectStockObject(BLACK_PEN);
poldbrush=(CBrush *)pDC->SelectStockObject(NULL_BRUSH);
int indexline,indexcircle,indexrectangle;
indexline=pDoc->m_linearray.GetUpperBound();
indexcircle=pDoc->m_circlearray.GetUpperBound();
indexrectangle=pDoc->m_rectanglearray.GetUpperBound();
while(indexline>=0)
{
pDoc->m_linearray.GetAt(indexline)->DrawLine(pDC);
indexline--;
}
while(indexcircle>=0)
{
pDoc->m_circlearray.GetAt(indexcircle)->DrawCircle(pDC);
indexcircle--;
}
while(indexrectangle>=0)
{
pDoc->m_rectanglearray.GetAt(indexrectangle)->DrawRectangle(pDC);
indexrectangle--;
}
}
/////////////////////////////////////////////////////////////////////////////
// CCadView printing
BOOL CCadView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCadView diagnostics
#ifdef _DEBUG
void CCadView::AssertValid() const
{
CView::AssertValid();
}
void CCadView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCadDoc* CCadView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCadDoc)));
return (CCadDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCadView message handlers
void CCadView::OnGraphicsLine()
{
// TODO: Add your command handler code here
m_line=1;
m_rectangle=m_circle=m_end.x=0;
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->SetGraphKind(1);
}
void CCadView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CView::OnLButtonDown(nFlags, point);
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_line)
{
if(m_step==0) {m_start=point;m_step++;}
else if(m_step==1)
{ pDoc->AddLine(m_start,m_end);
m_step=m_line=m_start.x=m_start.y=m_end.x=m_end.y=0;
}
}
else if(m_rectangle)
{
if(m_step==0) {m_start=point;m_step++;}
else if(m_step==1)
{
pDoc->AddRectangle(m_start,m_end);
m_step=0;m_rectangle=m_start.x=m_start.y=m_end.x=m_end.y=0;
}
}
else if(m_circle)
{
if(m_step==0) {m_start=point;m_step++;m_end.x=0;}
else if(m_step==1)
{
pDoc->AddCircle(m_start,m_end);
m_step=0;m_circle=m_start.x=m_start.y=m_end.x=m_end.y=0;
}
}
}
void CCadView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SetROP2(R2_NOTXORPEN);
CView::OnMouseMove(nFlags, point);
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_line)
{
if(m_step==1)
{ if(m_end.x==0)
{
dc.MoveTo(m_start);
dc.LineTo(point);
}
else
{
dc.MoveTo(m_start);dc.LineTo(m_end);
dc.MoveTo(m_start);dc.LineTo(point);
}
}
}
else if(m_rectangle)
{
if(m_step==1)
{
if(m_end.x==0) dc.Rectangle(m_start.x,m_start.y,point.x,point.y);
else {dc.Rectangle(m_start.x,m_start.y,m_end.x,m_end.y);dc.Rectangle(m_start.x,m_start.y,point.x,point.y);}
}
}
else if(m_circle)
{
if(m_step==1)
{
int r1=(int)sqrt((m_start.x-point.x)*(m_start.x-point.x)+(m_start.y-point.y)*(m_start.y-point.y));
if(m_end.x==0)
{
dc.Ellipse(m_start.x-r1,m_start.y-r1,m_start.x+r1,m_start.y+r1);
}
else
{
dc.Ellipse(m_start.x-r2,m_start.y-r2,m_start.x+r2,m_start.y+r2);
dc.Ellipse(m_start.x-r1,m_start.y-r1,m_start.x+r1,m_start.y+r1);
}
}
}
m_end=point;
r2=(int)sqrt((m_start.x-point.x)*(m_start.x-point.x)+(m_start.y-point.y)*(m_start.y-point.y));
}
void CCadView::OnGraphicsCircle()
{
// TODO: Add your command handler code here
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
m_circle=1;
m_rectangle=m_line=m_end.x=0;
if(pDoc->GetGraphKind()!=1)
pDoc->SetGraphKind(2);
}
void CCadView::OnGraphicsRectangle()
{
// TODO: Add your command handler code here
m_rectangle=1;
m_circle=m_line=m_end.x=0;
CCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->GetGraphKind()!=1&&pDoc->GetGraphKind()!=2)
pDoc->SetGraphKind(3);
}
void CCadView::OnUpdateGraphicsCircle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_circle);
}
void CCadView::OnUpdateGraphicsLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_line);
}
void CCadView::OnUpdateGraphicsRectangle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_rectangle);
}
void CCadView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Pop-up Menu component { if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(CG_IDR_POPUP_CAD_VIEW)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -