home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch34 / draw / drawview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  5.8 KB  |  278 lines

  1. // drawview.cpp : implementation of the CDrawView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "draw.h"
  6.  
  7. #include "drawdoc.h"
  8. #include "drawview.h"
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CDrawView
  17.  
  18. IMPLEMENT_DYNCREATE(CDrawView, CView)
  19.  
  20. BEGIN_MESSAGE_MAP(CDrawView, CView)
  21.     //{{AFX_MSG_MAP(CDrawView)
  22.     ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  23.     ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
  24.     ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
  25.     ON_COMMAND(ID_DRAW_CLEAR, OnDrawClear)
  26.     //}}AFX_MSG_MAP
  27.     // Standard printing commands
  28.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  29.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CDrawView construction/destruction
  34.  
  35. CDrawView::CDrawView()
  36. {
  37.     // TODO: add construction code here
  38.  
  39.      //////////////////////
  40.      // MY CODE STARTS HERE
  41.      //////////////////////
  42.  
  43.      m_Shape = 0;
  44.  
  45.      ////////////////////
  46.      // MY CODE ENDS HERE
  47.      ////////////////////
  48.  
  49. }
  50.  
  51. CDrawView::~CDrawView()
  52. {
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CDrawView drawing
  57.  
  58. void CDrawView::OnDraw(CDC* pDC)
  59. {
  60.     CDrawDoc* pDoc = GetDocument();
  61.     ASSERT_VALID(pDoc);
  62.  
  63.     // TODO: add draw code for native data here
  64.     
  65.      //////////////////////
  66.      // MY CODE STARTS HERE
  67.      //////////////////////
  68.  
  69.     // Beep (to demonstrate that the OnDraw() function
  70.     // was executed)
  71.     // MessageBeep(-1);
  72.  
  73.  
  74.     // Draw the line.
  75.     if ( m_Shape == 1 )
  76.        {
  77.  
  78.        // Create a new pen
  79.         CPen NewPen (PS_SOLID,          // The Style
  80.                      10,                // The width
  81.                      RGB(0,255,0) );    // The color
  82.  
  83.        // Set the new pen (and save the original pen)
  84.        CPen* pOriginalPen = pDC->SelectObject ( &NewPen );
  85.  
  86.        pDC->MoveTo ( 10, 30 );
  87.        pDC->LineTo ( 50, 30 );
  88.  
  89.        // Restore the original pen
  90.        pDC->SelectObject ( pOriginalPen );
  91.  
  92.  
  93.        // Draw a line with the original pen
  94.        pDC->MoveTo ( 50, 30 );
  95.        pDC->LineTo ( 80, 30 );
  96.  
  97.  
  98.        }
  99.  
  100.  
  101.     // Draw the circle
  102.     if ( m_Shape == 2 )
  103.        {
  104.  
  105.        // Create a new pen
  106.        CPen NewPen (PS_SOLID,            // The Style
  107.                     40,                  // The width
  108.                     RGB(255, 0, 0) );    // The color
  109.  
  110.        // Set the new pen (and save the original pen)
  111.        CPen* pOriginalPen = pDC->SelectObject ( &NewPen );
  112.  
  113.  
  114.        // Create a rectangle object
  115.        CRect theRect ( 20, 100, 120, 200 );
  116.  
  117.        // Draw the Circle
  118.        pDC->Ellipse ( &theRect );
  119.  
  120.        // Restore the original pen
  121.        pDC->SelectObject ( pOriginalPen );
  122.  
  123.  
  124.        }
  125.  
  126.  
  127.     // Draw the rectangle
  128.     if ( m_Shape == 3 )
  129.        {
  130.        // Create a rectangle object
  131.        CRgn theRgn;
  132.        theRgn.CreateRectRgn ( 50, 100, 150, 200 );
  133.  
  134.        // Create a new brush
  135.        CBrush MyBrush( HS_BDIAGONAL, RGB(0, 0, 255) );
  136.  
  137.        // Draw the rectangle
  138.        pDC->FillRgn ( &theRgn, &MyBrush );
  139.  
  140.        }
  141.  
  142.      ////////////////////
  143.      // MY CODE ENDS HERE
  144.      ////////////////////
  145.  
  146.  
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CDrawView printing
  151.  
  152. BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  153. {
  154.     // default preparation
  155.     return DoPreparePrinting(pInfo);
  156. }
  157.  
  158. void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  159. {
  160.     // TODO: add extra initialization before printing
  161. }
  162.  
  163. void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  164. {
  165.     // TODO: add cleanup after printing
  166. }
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CDrawView diagnostics
  170.  
  171. #ifdef _DEBUG
  172. void CDrawView::AssertValid() const
  173. {
  174.     CView::AssertValid();
  175. }
  176.  
  177. void CDrawView::Dump(CDumpContext& dc) const
  178. {
  179.     CView::Dump(dc);
  180. }
  181.  
  182. CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
  183. {
  184.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
  185.     return (CDrawDoc*)m_pDocument;
  186. }
  187. #endif //_DEBUG
  188.  
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CDrawView message handlers
  191.  
  192. void CDrawView::OnDrawLine()
  193. {
  194.     // TODO: Add your command handler code here
  195.  
  196.      //////////////////////
  197.      // MY CODE STARTS HERE
  198.      //////////////////////
  199.  
  200.  
  201.     // Set the flag to 1 (Line)
  202.     m_Shape = 1;
  203.  
  204.     // Trigger a call to OnDraw()
  205.     Invalidate();
  206.  
  207.  
  208.      ////////////////////
  209.      // MY CODE ENDS HERE
  210.      ////////////////////
  211.  
  212.     
  213. }
  214.  
  215. void CDrawView::OnDrawCircle()
  216. {
  217.     // TODO: Add your command handler code here
  218.     
  219.      //////////////////////
  220.      // MY CODE STARTS HERE
  221.      //////////////////////
  222.  
  223.     // Set the flag to 2 (Circle)
  224.     m_Shape = 2;
  225.  
  226.     // Trigger a call to OnDraw()
  227.     Invalidate();
  228.  
  229.  
  230.      ////////////////////
  231.      // MY CODE ENDS HERE
  232.      ////////////////////
  233.  
  234.  
  235. }
  236.  
  237. void CDrawView::OnDrawRectangle()
  238. {
  239.     // TODO: Add your command handler code here
  240.  
  241.      //////////////////////
  242.      // MY CODE STARTS HERE
  243.      //////////////////////
  244.  
  245.      // Set the flag to 3 (Rectangle)
  246.      m_Shape = 3;
  247.  
  248.      // Trigger a call to OnDraw()
  249.      Invalidate();
  250.  
  251.  
  252.      ////////////////////
  253.      // MY CODE ENDS HERE
  254.      ////////////////////
  255.  
  256. }
  257.  
  258. void CDrawView::OnDrawClear()
  259. {
  260.     // TODO: Add your command handler code here
  261.  
  262.      //////////////////////
  263.      // MY CODE STARTS HERE
  264.      //////////////////////
  265.  
  266.     // Set the flag to 0 (clear)
  267.     m_Shape = 0;
  268.  
  269.     // Trigger a call to OnDraw()
  270.     Invalidate();
  271.  
  272.  
  273.      ////////////////////
  274.      // MY CODE ENDS HERE
  275.      ////////////////////
  276.     
  277. }
  278.