DrawSysView.cpp
资源名称:DrawSys.zip [点击查看]
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:9k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // DrawSysView.cpp : implementation of the CDrawSysView class
- //
- #include "stdafx.h"
- #include "DrawSys.h"
- #include "DrawSysDoc.h"
- #include "DrawSysView.h"
- #include "MainFrm.h"
- #include "DrawShape.h"
- #include "DrawRect.h"
- #include "DrawEllipse.h"
- #include "DrawCircle.h"
- #include "DrawFillRect.h"
- #include "DrawFillEllipse.h"
- #include "DrawLine.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView
- IMPLEMENT_DYNCREATE(CDrawSysView, CScrollView)
- BEGIN_MESSAGE_MAP(CDrawSysView, CScrollView)
- //{{AFX_MSG_MAP(CDrawSysView)
- ON_COMMAND(ID_BUTTON_RECT, OnButtonRect)
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_COMMAND(ID_BUTTON_ELLIPSE, OnButtonEllipse)
- ON_COMMAND(ID_BUTTON_FILLELLIPSE, OnButtonFillellipse)
- ON_COMMAND(ID_BUTTON_CIRCLE, OnButtonCircle)
- ON_COMMAND(ID_BUTTON_FILLRECT, OnButtonFillrect)
- ON_COMMAND(ID_BUTTON_LINE, OnButtonLine)
- ON_MESSAGE(WM_CHOOSE, OnChoose)
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_WM_RBUTTONDOWN()
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView construction/destruction
- CDrawSysView::CDrawSysView()
- {
- // TODO: add construction code here
- m_pDrawing = NULL;
- m_iMode = -1;
- m_nTimer = 0;
- m_dwNewSel = 0;
- m_dwOldSel = 0;
- m_bNeed = FALSE;
- }
- CDrawSysView::~CDrawSysView()
- {
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- }
- BOOL CDrawSysView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CScrollView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView drawing
- void CDrawSysView::OnDraw(CDC* pDC)
- {
- CDrawSysDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- int iTotal = pDoc->m_obArray.GetSize();
- for(int i=0; i<iTotal; i++)
- {
- ((CDrawShape*)pDoc->m_obArray.GetAt(i))->Draw(pDC);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView printing
- BOOL CDrawSysView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CDrawSysView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CDrawSysView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView diagnostics
- #ifdef _DEBUG
- void CDrawSysView::AssertValid() const
- {
- CScrollView::AssertValid();
- }
- void CDrawSysView::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
- CDrawSysDoc* CDrawSysView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawSysDoc)));
- return (CDrawSysDoc*)m_pDocument;
- }
- #endif //_DEBUG
- void CDrawSysView::OnInitialUpdate()
- {
- CScrollView ::OnInitialUpdate();
- // TODO: Add your specialized code here and/or call the base class
- SetScrollSizes(MM_TEXT, CSize(800,600));
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawSysView message handlers
- void CDrawSysView::OnButtonRect()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawRect();
- m_iMode = 0;
- MyKillTimer();
- }
- void CDrawSysView::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CDC* pDC = NULL;
- if(m_pDrawing)
- {
- pDC = GetDC();
- OnPrepareDC(pDC);
- pDC->DPtoLP(&point);
- m_pDrawing->OnMouseMove(pDC, nFlags, point);
- ReleaseDC(pDC);
- }
- CScrollView::OnMouseMove(nFlags, point);
- }
- void CDrawSysView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- MyKillTimer();
- CDC* pDC = NULL;
- if(m_pDrawing)
- {
- pDC = GetDC();
- OnPrepareDC(pDC);
- pDC->DPtoLP(&point);
- m_pDrawing->OnLButtonDown(pDC, nFlags, point);
- ReleaseDC(pDC);
- }
- CScrollView::OnLButtonDown(nFlags, point);
- }
- void CDrawSysView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CDrawSysDoc* pDoc = NULL;
- CDC* pDC = NULL;
- int iIndex = 0;
- BOOL bSave = FALSE;
- if(m_pDrawing)
- {
- pDC = GetDC();
- OnPrepareDC(pDC);
- pDC->DPtoLP(&point);
- bSave = m_pDrawing->OnLButtonUp(pDC, nFlags, point);
- ReleaseDC(pDC);
- if(bSave)
- {
- //存储对象
- m_pDrawing->m_tmCreate = CTime::GetCurrentTime();
- m_pDrawing->m_iID = ((CMainFrame*)AfxGetMainWnd())->m_wndShapeTree.GetID(m_pDrawing);
- pDoc = GetDocument();
- pDoc->AddShape(m_pDrawing);
- //更新树
- iIndex = pDoc->m_obArray.GetSize();
- ((CMainFrame*)AfxGetMainWnd())->m_wndShapeTree.InsertShape(m_pDrawing, iIndex);
- }
- else
- {
- //释放对象
- delete m_pDrawing;
- }
- m_pDrawing = NULL;
- //创建新对象
- switch(m_iMode)
- {
- case 0:
- m_pDrawing = new CDrawRect();
- break;
- case 1:
- m_pDrawing = new CDrawEllipse();
- break;
- case 2:
- m_pDrawing = new CDrawCircle();
- break;
- case 3:
- m_pDrawing = new CDrawFillRect();
- break;
- case 4:
- m_pDrawing = new CDrawFillEllipse();
- break;
- case 5:
- m_pDrawing = new CDrawLine();
- break;
- default:
- //选择模式
- break;
- }
- }
- CScrollView::OnLButtonUp(nFlags, point);
- }
- void CDrawSysView::OnButtonEllipse()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawEllipse();
- m_iMode = 1;
- MyKillTimer();
- }
- void CDrawSysView::OnButtonFillellipse()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawFillEllipse();
- m_iMode = 4;
- MyKillTimer();
- }
- void CDrawSysView::OnButtonCircle()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawCircle();
- m_iMode = 2;
- MyKillTimer();
- }
- void CDrawSysView::OnButtonFillrect()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawFillRect();
- m_iMode = 3;
- MyKillTimer();
- }
- void CDrawSysView::OnButtonLine()
- {
- // TODO: Add your command handler code here
- if(m_pDrawing)
- {
- delete m_pDrawing;
- m_pDrawing = NULL;
- }
- m_pDrawing = new CDrawLine();
- m_iMode = 5;
- MyKillTimer();
- }
- LRESULT CDrawSysView::OnChoose(WPARAM wParam, LPARAM lParam)
- {
- MyKillTimer();
- m_dwNewSel = wParam;
- m_dwOldSel = lParam;
- m_nTimer = SetTimer(0, 200, NULL);
- return 0;
- }
- void CDrawSysView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- FlashShape();
- CScrollView::OnTimer(nIDEvent);
- }
- void CDrawSysView::MyKillTimer()
- {
- if(m_nTimer)
- {
- KillTimer(m_nTimer);
- m_nTimer = 0;
- }
- //清除以前闪烁
- if(m_bNeed)
- {
- FlashShape();
- }
- m_dwNewSel = 0;
- m_dwOldSel = 0;
- m_bNeed = FALSE;
- }
- void CDrawSysView::FlashShape()
- {
- CDC* pDC = NULL;
- CDrawSysDoc* pDoc = NULL;
- int iDrawMode = 0;
- CPen pen(PS_SOLID, 3, RGB(255, 0, 0));
- CPen* pOldPen = NULL;
- if(m_dwNewSel)
- {
- pDoc = GetDocument();
- pDC = GetDC();
- OnPrepareDC(pDC);
- iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
- pOldPen = pDC->SelectObject(&pen);
- ((CDrawShape*)(pDoc->m_obArray.GetAt(m_dwNewSel-1)))->Draw(pDC);
- pDC->SelectObject(pOldPen);
- pen.DeleteObject();
- pDC->SetROP2(iDrawMode);
- ReleaseDC(pDC);
- pDC = NULL;
- m_bNeed = !m_bNeed;
- }
- }
- void CDrawSysView::OnDestroy()
- {
- CScrollView::OnDestroy();
- // TODO: Add your message handler code here
- MyKillTimer();
- }
- BOOL CDrawSysView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
- {
- // TODO: Add your specialized code here and/or call the base class
- MyKillTimer();
- return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
- }
- BOOL CDrawSysView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
- {
- // TODO: Add your specialized code here and/or call the base class
- MyKillTimer();
- return CScrollView::OnScrollBy(sizeScroll, bDoScroll);
- }
- void CDrawSysView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CDrawSysDoc* pDoc = GetDocument();
- int iTotal = pDoc->m_obArray.GetSize();
- BOOL m_bIn = FALSE;
- CDC* pDC = NULL;
- ASSERT_VALID(pDoc);
- pDC = GetDC();
- OnPrepareDC(pDC);
- pDC->DPtoLP(&point);
- for(int i=0; i<iTotal; i++)
- {
- m_bIn = ((CDrawShape*)pDoc->m_obArray.GetAt(i))->PtInShape(point);
- if(m_bIn)
- break;
- }
- if(m_bIn)
- {
- ((CDrawShape*)pDoc->m_obArray.GetAt(i))->ShowProperty();
- }
- pDC->LPtoDP(&point);
- ReleaseDC(pDC);
- CScrollView::OnRButtonDown(nFlags, point);
- }