0fefb09150903e54e1d43968b41f48a57e465cbd
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / tutorials / Tutorial7 / View.cpp
1 //////////////////////////////////////////////\r
2 // View.cpp\r
3 //  Definitions for the CView class\r
4 \r
5 #include "view.h"\r
6 #include "resource.h"\r
7 \r
8 \r
9 CView::CView() : m_PenColor(RGB(0,0,0))\r
10 {\r
11         m_Brush.CreateSolidBrush(RGB(255,255,230));\r
12 }\r
13 \r
14 CView::~CView()\r
15 {\r
16 }\r
17 \r
18 void CView::DrawLine(int x, int y)\r
19 {\r
20         CClientDC dcClient(this);\r
21         dcClient.CreatePen(PS_SOLID, 1, m_points.back().color);\r
22         dcClient.MoveTo(m_points.back().x, m_points.back().y);\r
23         dcClient.LineTo(x, y);\r
24 }\r
25 \r
26 void CView::OnDraw(CDC* pDC)\r
27 {\r
28         if (m_points.size() > 0)\r
29         {\r
30                 bool bDraw = false;  //Start with the pen up\r
31                 for (unsigned int i = 0 ; i < m_points.size(); i++)\r
32                 {\r
33                         pDC->CreatePen(PS_SOLID, 1, m_points[i].color);\r
34                         if (bDraw)\r
35                                 pDC->LineTo(m_points[i].x, m_points[i].y);\r
36                         else\r
37                                 pDC->MoveTo(m_points[i].x, m_points[i].y);\r
38                         \r
39                         bDraw = m_points[i].PenDown;\r
40                 }\r
41         }\r
42 }\r
43 \r
44 void CView::PreCreate(CREATESTRUCT &cs)\r
45 {\r
46         // Set the extra style to provide a sunken edge\r
47         cs.dwExStyle = WS_EX_CLIENTEDGE;\r
48 }\r
49 \r
50 void CView::PreRegisterClass(WNDCLASS &wc)\r
51 {\r
52         // Set the background brush and cursor\r
53         wc.hbrBackground = m_Brush;\r
54         wc.lpszClassName = _T("Scribble Window");\r
55         wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));\r
56 }\r
57 \r
58 void CView::SetPen(COLORREF color)\r
59 {\r
60         m_PenColor = color;\r
61 }\r
62 \r
63 void CView::StorePoint(int x, int y, bool PenDown)\r
64 {\r
65         PlotPoint P1;\r
66         P1.x = x;\r
67         P1.y = y;\r
68         P1.PenDown = PenDown;\r
69         P1.color = m_PenColor;\r
70 \r
71         m_points.push_back(P1); //Add the point to the vector\r
72 }\r
73 \r
74 void CView::OnLButtonDown(LPARAM lParam)\r
75 {\r
76         // Capture mouse input.\r
77         SetCapture();\r
78 \r
79         StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
80 }\r
81 \r
82 void CView::OnLButtonUp(LPARAM lParam)\r
83 {\r
84         {\r
85                 //Release the capture on the mouse\r
86                 ReleaseCapture();\r
87 \r
88                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);\r
89         }\r
90 }\r
91 \r
92 void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)\r
93 {\r
94         // hold down the left mouse button and move mouse to draw lines.\r
95         if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )\r
96         {\r
97                 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\r
98                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
99         }\r
100 }\r
101 \r
102 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
103 {\r
104         switch (uMsg)\r
105         {\r
106         case WM_LBUTTONDOWN:\r
107                 OnLButtonDown(lParam);\r
108                 break;\r
109 \r
110         case WM_MOUSEMOVE:\r
111                 OnMouseMove(wParam, lParam);\r
112         break;\r
113 \r
114     case WM_LBUTTONUP:\r
115                 OnLButtonUp(lParam);\r
116                 break;\r
117         }\r
118 \r
119         //Use the default message handling for remaining messages\r
120         return WndProcDefault(uMsg, wParam, lParam);\r
121 }\r
122 \r
123 \r
124 \r
125 \r

UCC git Repository :: git.ucc.asn.au