⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sketcherview.cpp

📁 Beginning Visual C++ 6源码。Wrox。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SketcherView.cpp : implementation of the CSketcherView class
//

#include "stdafx.h"
#include "Sketcher.h"

#include <math.h>

#include "PrintData.h" 
#include "TextDialog.h"
#include "ChildFrm.h"
#include "ScaleDialog.h"
#include "DllImports.h"
#include "SketcherDoc.h"
#include "SketcherView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSketcherView

IMPLEMENT_DYNCREATE(CSketcherView, CScrollView)

BEGIN_MESSAGE_MAP(CSketcherView, CScrollView)
	//{{AFX_MSG_MAP(CSketcherView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_MOVE, OnMove)
	ON_COMMAND(ID_DELETE, OnDelete)
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_SENDTOBACK, OnSendtoback)
	ON_COMMAND(ID_VIEW_SCALE, OnViewScale)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CSketcherView construction/destruction

CSketcherView::CSketcherView()
{
	// TODO: add construction code here
   m_FirstPoint = CPoint(0,0);         // Set 1st recorded point to 0,0
   m_SecondPoint = CPoint(0,0);        // Set 2nd recorded point to 0,0
   m_pTempElement = NULL;              // Set temporary element pointer to 0
   m_pSelected = NULL;                 // No element selected initially
   m_MoveMode = FALSE;                 // Set move mode off
   m_CursorPos = CPoint(0,0);          // Initialize as zero
   m_FirstPos = CPoint(0,0);           // Initialize as zero
   m_Scale = 1;                        // Set scale to 1:1
   SetScrollSizes(MM_TEXT, CSize(0,0));// Set arbitrary scrollers
}

CSketcherView::~CSketcherView()
{
}

BOOL CSketcherView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSketcherView drawing

void CSketcherView::OnDraw(CDC* pDC)
{
   CSketcherDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);

   POSITION aPos = pDoc->GetListHeadPosition();
   CElement* pElement = 0;              // Store for an element pointer
   while(aPos)                          // Loop while aPos is not null
   {
      pElement = pDoc->GetNext(aPos);   // Get the current element pointer
      // If the element is visible...
      if(pDC->RectVisible(pElement->GetBoundRect()))
         pElement->Draw(pDC, m_pSelected);  // ...draw it
   }
}

/////////////////////////////////////////////////////////////////////////////
// CSketcherView printing

BOOL CSketcherView::OnPreparePrinting(CPrintInfo* pInfo)
{
   // Create a print data object 
   CPrintData* pPrintData;
   pInfo->m_lpUserData = pPrintData = new CPrintData;

   CSketcherDoc* pDoc = GetDocument();    // Get a document pointer

   // Get the whole document area
   CRect DocExtent = pDoc->GetDocExtent();

   // Save the reference point for the whole document
   pPrintData->m_DocRefPoint = CPoint(DocExtent.left, DocExtent.bottom);

   // Get the name of the document file and save it
   pPrintData->m_DocTitle = pDoc->GetTitle();

   // Calculate how many printed page widths of 600 units are required
   // to accommodate the width of the document
   pPrintData->m_nWidths =
                         (UINT)ceil(static_cast<double>(DocExtent.Width()) / 600.0);

   // Calculate how many printed page lengths of 900 units are required
   // to accommodate the document length
   pPrintData->m_nLengths =
                         (UINT)ceil(static_cast<double>(DocExtent.Height()) / 900.0);

   // Set the first page number as 1 and
   // set the last page number as the total number of pages
   pInfo->SetMinPage(1);
   pInfo->SetMaxPage(pPrintData->m_nWidths * pPrintData->m_nLengths);

   if(!DoPreparePrinting(pInfo))
     {
        delete static_cast<CPrintData*>(pInfo->m_lpUserData);
        return FALSE;
     }

   return TRUE;
}

void CSketcherView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSketcherView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* pInfo)
{
   // Delete our print data object
   delete static_cast<CPrintData*>(pInfo->m_lpUserData);
}

/////////////////////////////////////////////////////////////////////////////
// CSketcherView diagnostics

#ifdef _DEBUG
void CSketcherView::AssertValid() const
{
	CView::AssertValid();
}

void CSketcherView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSketcherDoc* CSketcherView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSketcherDoc)));
	return (CSketcherDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSketcherView message handlers

void CSketcherView::OnLButtonDown(UINT nFlags, CPoint point) 
{
   CClientDC aDC(this);                // Create a device context
   OnPrepareDC(&aDC);                  // Get origin adjusted
   aDC.DPtoLP(&point);                 // convert point to Logical

   if(m_MoveMode)
   {
      // In moving mode, so drop the element
      m_MoveMode = FALSE;                 // Kill move mode
      m_pSelected = 0;                    // De-select the element
      GetDocument()->UpdateAllViews(0);   // Redraw all the views
      GetDocument()->SetModifiedFlag();   // Set the modified flag
   }
   else
   {
      CSketcherDoc* pDoc = GetDocument();   // Get a document pointer
      if(pDoc->GetElementType() == TEXT)
      {
         CTextDialog aDlg;
         if(aDlg.DoModal() == IDOK)
         {
            // Exit OK so create a text element 
            CSize TextExtent = aDC.GetTextExtent(aDlg.m_TextString);

            // Get bottom right of text rectangle - MM_LOENGLISH
            CPoint BottomRt(point.x+TextExtent.cx, point.y-TextExtent.cy);
            CText* pTextElement = new CText(point, BottomRt,
                              aDlg.m_TextString, pDoc->GetElementColor());

            // Add the element to the document
            pDoc->AddElement(pTextElement);

            // Get all views updated
            pDoc->UpdateAllViews(0,0,pTextElement);
         }
         return;
      }

      m_FirstPoint = point;               // Record the cursor position
      SetCapture();                       // Capture subsequent mouse messages
   }
}

void CSketcherView::OnLButtonUp(UINT nFlags, CPoint point)
{
   if(this == GetCapture())
      ReleaseCapture();      // Stop capturing mouse messages

   // If there is an element, add it to the document
   if(m_pTempElement)
   {
      GetDocument()->AddElement(m_pTempElement);
      GetDocument()->UpdateAllViews(0,0,m_pTempElement);  
                                // Tell all the views
      m_pTempElement = 0;       // Reset the element pointer
   }
}

void CSketcherView::OnMouseMove(UINT nFlags, CPoint point)
{
   // Define a Device Context object for the view
   CClientDC aDC(this);
   OnPrepareDC(&aDC);            // Get origin adjusted

   // If we are in move mode, move the selected element and return
   if(m_MoveMode)
   {
      aDC.DPtoLP(&point);        // Convert to logical coordinatess
      MoveElement(aDC, point);   // Move the element
      return;
   }

   if((nFlags & MK_LBUTTON) && (this == GetCapture()))
   {
      aDC.DPtoLP(&point);        // convert point to Logical
	  m_SecondPoint = point;     // Save the current cursor position

      if(m_pTempElement)
      {
         if(CURVE == GetDocument()->GetElementType())   // Is it a curve?
         {  // We are drawing a curve
            // so add a segment to the existing curve
            (static_cast<CCurve*>(m_pTempElement))->AddSegment(m_SecondPoint);
            m_pTempElement->Draw(&aDC);  // Now draw it
            return;                      // We are done
         }

         aDC.SetROP2(R2_NOTXORPEN);                 // Set drawing mode

         // Redraw the old element so it disappears from the view
         m_pTempElement->Draw(&aDC);
         delete m_pTempElement;        // Delete the old element
         m_pTempElement = 0;           // Reset the pointer to 0
      }

      // Create a temporary element of the type and color that
      // is recorded in the document object, and draw it
      m_pTempElement = CreateElement();  // Create a new element
      m_pTempElement->Draw(&aDC);        // Draw the element
   }
   else        // We are not drawing an element...
   {           // ...so do highlighting
      CRect aRect;
      CElement* pCurrentSelection = SelectElement(point);

      if(pCurrentSelection!=m_pSelected)
      {
         if(m_pSelected)             // Old elemented selected?
         {                           // Yes, so draw it unselected
            aRect = m_pSelected->GetBoundRect(); // Get bounding rectangle
            aDC.LPtoDP(aRect);                   // Conv to device coords
            aRect.NormalizeRect();               // Normalize
            InvalidateRect(aRect, FALSE);        // Invalidate area
         }
         m_pSelected = pCurrentSelection;        // Save elem under cursor
         if(m_pSelected)                         // Is there one?
         {                                       // Yes, so get it redrawn
            aRect = m_pSelected->GetBoundRect(); // Get bounding rectangle
            aDC.LPtoDP(aRect);                   // Conv to device coords
            aRect.NormalizeRect();               // Normalize

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -