GraphicView.cpp
资源名称:ac.rar [点击查看]
上传用户:herolh
上传日期:2022-07-18
资源大小:3590k
文件大小:7k
源码类别:
绘图程序
开发平台:
Visual C++
- // GraphicView.cpp : implementation of the CGraphicView class
- //
- #include "stdafx.h"
- #include "Graphic.h"
- #include "GraphicDoc.h"
- #include "GraphicView.h"
- #include "SettingDialog.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView
- IMPLEMENT_DYNCREATE(CGraphicView, CView)
- BEGIN_MESSAGE_MAP(CGraphicView, CView)
- //{{AFX_MSG_MAP(CGraphicView)
- ON_COMMAND(ID_POINT, OnPoint)
- ON_COMMAND(ID_LINE, OnLine)
- ON_COMMAND(ID_RETANGLE, OnRetangle)
- ON_COMMAND(ID_ELLIPSE, OnEllipse)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
- ON_UPDATE_COMMAND_UI(ID_POINT, OnUpdatePoint)
- ON_UPDATE_COMMAND_UI(ID_ELLIPSE, OnUpdateEllipse)
- ON_UPDATE_COMMAND_UI(ID_RETANGLE, OnUpdateRetangle)
- ON_COMMAND(ID_LINEWIDTHSETTING, OnLinewidthsetting)
- ON_COMMAND(ID_COLOR, OnColor)
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView construction/destruction
- CGraphicView::CGraphicView()
- {
- // TODO: add construction code here
- m_nDrawType=0;
- m_originpt=0;
- m_nLineWidth=1;
- m_nColorSetting=RGB(0,0,255);
- m_nLineStyle=0;
- }
- CGraphicView::~CGraphicView()
- {
- }
- BOOL CGraphicView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView drawing
- void CGraphicView::OnDraw(CDC* pDC)
- {
- CGraphicDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- pDC->SelectObject(pBrush);
- for(int i=0;i<m_ptrArray.GetSize();i++)
- {
- CPen pen(((StorePoint*)m_ptrArray.GetAt(i))->m_nLineStyle,((StorePoint*)m_ptrArray.GetAt(i))->m_nLineWidth,
- /*RGB(0,0,255)m_nColorSetting*/((StorePoint*)m_ptrArray.GetAt(i))->m_nColor);//画笔
- pDC->SelectObject(&pen);
- switch(((StorePoint*)m_ptrArray.GetAt(i))->m_nDrawType)
- {
- case 1:
- pDC->SetPixel(((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd,RGB(0,0,0));
- break;
- case 2:
- pDC->MoveTo(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin);
- pDC->LineTo(((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd);
- break;
- case 3:
- pDC->Rectangle(CRect(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin,
- ((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd));
- break;
- case 4:pDC->Ellipse(CRect(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin,
- ((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd));
- }
- }
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView printing
- BOOL CGraphicView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CGraphicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CGraphicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView diagnostics
- #ifdef _DEBUG
- void CGraphicView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CGraphicView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CGraphicDoc* CGraphicView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));
- return (CGraphicDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CGraphicView message handlers
- void CGraphicView::OnPoint()
- {
- // TODO: Add your command handler code here
- m_nDrawType=1 ;
- }
- void CGraphicView::OnLine()
- {
- // TODO: Add your command handler code here
- m_nDrawType=2;
- }
- void CGraphicView::OnRetangle()
- {
- // TODO: Add your command handler code here
- m_nDrawType=3;
- }
- void CGraphicView::OnEllipse()
- {
- // TODO: Add your command handler code here
- m_nDrawType=4;
- }
- void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- m_originpt=point;
- CView::OnLButtonDown(nFlags, point);
- }
- void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CClientDC dc(this);
- CPen pen(m_nLineStyle,m_nLineWidth,/*RGB(0,0,255)*/m_nColorSetting);//画笔
- pOldpen=dc.SelectObject(&pen);
- CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//画刷
- pOldbrush=dc.SelectObject(pBrush);
- switch(m_nDrawType)
- {
- case 1:
- dc.SetPixel(point,m_nColorSetting);
- break;
- case 2:
- dc.MoveTo(m_originpt);
- dc.LineTo(point);
- break;
- case 3:
- dc.Rectangle(CRect(m_originpt,point));
- break;
- case 4:
- dc.Ellipse(CRect(m_originpt,point));
- break;
- }
- tag_Point *pStorePoint=new tag_Point(m_originpt,point,m_nDrawType,m_nColorSetting,m_nLineWidth,m_nLineStyle);
- m_ptrArray.Add(pStorePoint);
- CView::OnLButtonUp(nFlags, point);
- }
- void CGraphicView::OnUpdateLine(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck((m_nDrawType==2));
- }
- void CGraphicView::OnUpdatePoint(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck((m_nDrawType==1));
- }
- void CGraphicView::OnUpdateEllipse(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck((m_nDrawType==4));
- }
- void CGraphicView::OnUpdateRetangle(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck((m_nDrawType==3));
- }
- void CGraphicView::OnLinewidthsetting()
- {
- // TODO: Add your command handler code here
- SettingDialog dlg;
- dlg.m_nLineStyle=m_nLineStyle;
- dlg.m_nLineWidth=m_nLineWidth;
- if(IDOK==dlg.DoModal())
- {
- m_nLineWidth=dlg.m_nLineWidth;
- m_nLineStyle=dlg.m_nLineStyle;
- }
- }
- void CGraphicView::OnColor()
- {
- // TODO: Add your command handler code here
- CColorDialog dlg;
- dlg.m_cc.Flags|=CC_RGBINIT|CC_FULLOPEN;//初始化
- dlg.m_cc.rgbResult=m_nColorSetting;
- if(IDOK==dlg.DoModal())
- {
- m_nColorSetting=dlg.m_cc.rgbResult;
- };
- }
English
