drawView.cpp
资源名称:GDIUSE.rar [点击查看]
上传用户:jinlangri
上传日期:2022-07-17
资源大小:10774k
文件大小:14k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // drawView.cpp : implementation of the CDrawView class
- //
- #include "stdafx.h"
- #include "draw.h"
- #include "TsFont.h"
- #include "word.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_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONUP()
- ON_WM_RBUTTONUP()
- ON_COMMAND(ID_DRAW_FONT, OnDrawFont)
- //}}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
- m_Dragging = 0;
- m_IsRect = FALSE;
- m_hArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
- m_hCross = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
- m_PenDotted.CreatePen(PS_DOT, 1, RGB(0,0,0));
- m_cTextColor = RGB(0,0,0);
- m_lf.lfHeight = 30;
- m_lf.lfWidth = 0;
- m_lf.lfEscapement = 0;
- m_lf.lfOrientation = 0;
- m_lf.lfWeight = FW_NORMAL;
- m_lf.lfItalic = FALSE;
- m_lf.lfUnderline = FALSE;
- m_lf.lfStrikeOut = FALSE;
- m_lf.lfCharSet = GB2312_CHARSET;
- m_lf.lfOutPrecision = OUT_STROKE_PRECIS;
- m_lf.lfClipPrecision = CLIP_STROKE_PRECIS;
- m_lf.lfQuality = DRAFT_QUALITY;
- m_lf.lfPitchAndFamily = VARIABLE_PITCH|FF_MODERN;
- strcpy(m_lf.lfFaceName, "仿宋_GB2312");
- }
- CDrawView::~CDrawView()
- {
- }
- BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- m_ClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, 0,
- (HBRUSH)::GetStockObject(WHITE_BRUSH), 0);
- cs.lpszClass = m_ClassName;
- return CScrollView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawView drawing
- void CDrawView::OnDraw(CDC* pDC)
- {
- CDrawDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- CSize ScrollSize = GetTotalSize();
- pDC->MoveTo(ScrollSize.cx, 0);
- pDC->LineTo(ScrollSize.cx, ScrollSize.cy);
- pDC->LineTo(0, ScrollSize.cy);
- CRect ClipRect;
- CRect DimRect;
- CRect IntRect;
- CDrawBase *pDraw;
- pDC->GetClipBox(&ClipRect);
- int NumDraws = pDoc->GetNumDraws();
- for(int Index = 0; Index < NumDraws; ++Index)
- {
- pDraw = pDoc->GetDraw(Index);
- DimRect = pDraw->GetDimRect();
- if(IntRect.IntersectRect(DimRect, ClipRect))
- pDraw->Draw(pDC);
- }
- }
- void CDrawView::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- sizeTotal.cx = 800;
- sizeTotal.cy = 600;
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
- /////////////////////////////////////////////////////////////////////////////
- // 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
- {
- CScrollView::AssertValid();
- }
- void CDrawView::Dump(CDumpContext& dc) const
- {
- CScrollView::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::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CClientDC ClientDC(this);
- OnPrepareDC(&ClientDC);
- CDrawApp *pApp = (CDrawApp *)AfxGetApp();
- if(m_IsRect)
- {
- CDC *pDC = GetDC();
- CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
- rect.NormalizeRect();
- pDC->DrawFocusRect(rect);
- ReleaseDC(pDC);
- m_IsRect = FALSE;
- }
- if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
- ClientDC.DPtoLP(&point);
- CSize ScrollSize = GetTotalSize();
- CRect ScrollRect(0, 0, ScrollSize.cx, ScrollSize.cy);
- if(!ScrollRect.PtInRect(point))
- return;
- m_PointOrigin = point;
- m_PointOld = point;
- SetCapture();
- m_Dragging = 1;
- ClientDC.LPtoDP(&ScrollRect);
- CRect ViewRect;
- GetClientRect(&ViewRect);
- CRect IntRect;
- IntRect.IntersectRect(&ScrollRect, &ViewRect);
- ClientToScreen(&IntRect);
- ::ClipCursor(&IntRect);
- CScrollView::OnLButtonDown(nFlags, point);
- }
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- CClientDC ClientDC(this);
- OnPrepareDC(&ClientDC);
- CDrawApp *pApp = (CDrawApp *)AfxGetApp();
- if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
- ClientDC.DPtoLP(&point);
- if(!m_Dragging)
- {
- CSize ScrollSize = GetTotalSize();
- CRect ScrollRect(0,0,ScrollSize.cx, ScrollSize.cy);
- if(ScrollRect.PtInRect(point))
- ::SetCursor(m_hCross);
- else
- ::SetCursor(m_hArrow);
- return;
- }
- ClientDC.SetROP2(R2_NOT);
- ClientDC.SelectObject(&m_PenDotted);
- ClientDC.SetBkColor(TRANSPARENT);
- ClientDC.SelectStockObject(NULL_BRUSH);
- switch(((CDrawApp *)AfxGetApp())->m_CurrentTool)
- {
- case ID_DRAW_LINE:
- ClientDC.MoveTo(m_PointOrigin);
- ClientDC.LineTo(m_PointOld);
- ClientDC.MoveTo(m_PointOrigin);
- ClientDC.LineTo(point);
- break;
- case ID_DRAW_SELECT:
- case ID_DRAW_WORD:
- {
- CDC *pDC = GetDC();
- CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
- rect.NormalizeRect();
- pDC->DrawFocusRect(rect);
- rect.SetRect(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y);
- rect.NormalizeRect();
- pDC->DrawFocusRect(rect);
- m_IsRect = TRUE;
- ReleaseDC(pDC);
- break;
- }
- case ID_DRAW_RECT:
- case ID_DRAW_RECTFILL:
- ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
- point.x, point.y);
- break;
- case ID_DRAW_RECTROUND:
- case ID_DRAW_RECTROUNDFILL:
- {
- int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
- abs(m_PointOld.y - m_PointOrigin.x))/6;
- ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y,
- SizeRound, SizeRound);
- SizeRound = (abs(point.x - m_PointOrigin.x) +
- abs(point.y - m_PointOrigin.x))/6;
- ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
- point.x, point.y, SizeRound, SizeRound);
- break;
- }
- case ID_DRAW_CIRCLE:
- case ID_DRAW_CIRCLEFILL:
- {
- int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
- + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
- + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- break;
- }
- case ID_DRAW_ELLIPSE:
- case ID_DRAW_ELLIPSEFILL:
- ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
- point.x, point.y);
- break;
- }
- m_PointOld = point;
- CScrollView::OnMouseMove(nFlags, point);
- }
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if(!m_Dragging) return;
- m_Dragging = 0;
- ::ReleaseCapture();
- ::ClipCursor(NULL);
- CClientDC ClientDC(this);
- OnPrepareDC(&ClientDC);
- ClientDC.DPtoLP(&point);
- ClientDC.SelectObject(&m_PenDotted);
- ClientDC.SetROP2(R2_NOT);
- ClientDC.SetBkMode(TRANSPARENT);
- ClientDC.SelectStockObject(NULL_BRUSH);
- CDrawApp *pApp = (CDrawApp *)AfxGetApp();
- CDrawBase *pDraw;
- BOOL flag = FALSE;
- switch(pApp->m_CurrentTool)
- {
- case ID_DRAW_LINE:
- ClientDC.MoveTo(m_PointOrigin);
- ClientDC.LineTo(m_PointOld);
- pDraw = new CLine(m_PointOrigin.x, m_PointOrigin.y,
- point.x, point.y, pApp->m_PenColor, pApp->m_CurrentPenThickness);
- break;
- case ID_DRAW_RECT:
- ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- pDraw = new CRectangle(m_PointOrigin.x, m_PointOrigin.y,point.x, point.y,
- pApp->m_PenColor, pApp->m_CurrentPenThickness);
- break;
- case ID_DRAW_RECTFILL:
- ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- pDraw = new CRectFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
- pApp->m_FillColor);
- break;
- case ID_DRAW_RECTROUND:
- {
- int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
- abs(m_PointOld.y - m_PointOrigin.x))/6;
- ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y,
- SizeRound, SizeRound);
- pDraw = new CRectRound(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
- pApp->m_PenColor, pApp->m_CurrentPenThickness);
- break;
- }
- case ID_DRAW_RECTROUNDFILL:
- { int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
- abs(m_PointOld.y-m_PointOrigin.x))/6;
- ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y,
- SizeRound, SizeRound);
- pDraw = new CRectRoundFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
- pApp->m_FillColor);
- break;
- }
- case ID_DRAW_CIRCLE:
- {
- int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
- + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
- + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- pDraw = new CCircle(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim,
- pApp->m_PenColor, pApp->m_CurrentPenThickness);
- break;
- }
- case ID_DRAW_CIRCLEFILL:
- {
- ClientDC.MoveTo(m_PointOrigin);
- ClientDC.LineTo(m_PointOld);
- int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
- + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
- + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
- ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim);
- pDraw = new CCircleFill(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
- m_PointOrigin.x + dim, m_PointOrigin.y + dim,
- pApp->m_FillColor);
- break;
- }
- case ID_DRAW_ELLIPSE:
- {
- ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- pDraw = new CCircle(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
- pApp->m_PenColor, pApp->m_CurrentPenThickness);
- break;
- }
- case ID_DRAW_ELLIPSEFILL:
- ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
- m_PointOld.x, m_PointOld.y);
- pDraw = new CCircleFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
- pApp->m_FillColor);
- break;
- case ID_DRAW_WORD:
- {
- CPoint PointOrigin = m_PointOrigin;
- ClientDC.DPtoLP(&PointOrigin);
- CString str;
- CWord WordDlg;
- if(WordDlg.DoModal() == IDOK)
- {
- str = WordDlg.m_Word;
- pDraw = new CText(PointOrigin.x, PointOrigin.y, point.x, point.y, m_cTextColor, m_lf, str);
- flag = TRUE;
- }
- if(m_IsRect)
- {
- CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
- rect.NormalizeRect();
- ClientDC.DrawFocusRect(rect);
- m_IsRect = FALSE;
- }
- }
- }
- if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
- {
- ClientDC.SetROP2(R2_COPYPEN);
- pDraw->Draw(&ClientDC);
- CDrawDoc *pDoc = GetDocument();
- pDoc->AddDraw(pDraw);
- pDoc->UpdateAllViews(this, 0, pDraw);
- }
- else if(pApp->m_CurrentTool == ID_DRAW_WORD&&flag)
- {
- ClientDC.SetROP2(R2_COPYPEN);
- pDraw->Draw(&ClientDC);
- CDrawDoc *pDoc = GetDocument();
- pDoc->AddDraw(pDraw);
- pDoc->UpdateAllViews(this, 0, pDraw);
- }
- CScrollView::OnLButtonUp(nFlags, point);
- }
- void CDrawView::OnRButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(m_IsRect)
- {
- CDC *pDC = GetDC();
- CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
- rect.NormalizeRect();
- pDC->DrawFocusRect(rect);
- ReleaseDC(pDC);
- m_IsRect = FALSE;
- }
- CScrollView::OnRButtonUp(nFlags, point);
- }
- void CDrawView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
- {
- if(pHint != 0)
- {
- CRect InvalidRect = ((CLine *)pHint)->GetDimRect();
- CClientDC ClientDC(this);
- OnPrepareDC(&ClientDC);
- ClientDC.LPtoDP(&InvalidRect);
- InvalidateRect(&InvalidRect);
- }
- else
- CScrollView::OnUpdate(pSender,lHint,pHint);
- }
- void CDrawView::OnDrawFont()
- {
- CTsFont TsFont;
- TsFont.m_lfHeight = m_lf.lfHeight;
- TsFont.m_lfWidth = m_lf.lfWidth;
- TsFont.m_lfEscapement = m_lf.lfEscapement;
- TsFont.m_lfOrientation = m_lf.lfOrientation;
- TsFont.m_lfWeight = m_lf.lfWeight;
- TsFont.m_crColor = m_cTextColor;
- if(TsFont.DoModal() == IDOK)
- {
- m_lf.lfHeight = TsFont.m_lfHeight;
- m_lf.lfWidth = TsFont.m_lfWidth;
- m_lf.lfEscapement = TsFont.m_lfEscapement;
- m_lf.lfOrientation = TsFont.m_lfOrientation;
- m_lf.lfWeight = TsFont.m_lfWeight;
- m_cTextColor = TsFont.m_crColor;
- }
- }