1 //////////////////////////////////////////////
\r
3 // Definitions for the CView class
\r
8 #include "resource.h"
\r
10 using namespace std;
\r
12 CView::CView() : m_PenColor(RGB(0,0,0))
\r
14 m_Brush.CreateSolidBrush(RGB(255,255,230));
\r
21 void CView::ClearPoints()
\r
27 void CView::DrawLine(int x, int y)
\r
29 CClientDC dcClient(this);
\r
30 dcClient.CreatePen(PS_SOLID, 1, m_points.back().color);
\r
31 dcClient.MoveTo(m_points.back().x, m_points.back().y);
\r
32 dcClient.LineTo(x, y);
\r
35 void CView::OnDraw(CDC* pDC)
\r
37 if (m_points.size() > 0)
\r
39 bool bDraw = false; //Start with the pen up
\r
40 for (unsigned int i = 0 ; i < m_points.size(); i++)
\r
42 pDC->CreatePen(PS_SOLID, 1, m_points[i].color);
\r
44 pDC->LineTo(m_points[i].x, m_points[i].y);
\r
46 pDC->MoveTo(m_points[i].x, m_points[i].y);
\r
48 bDraw = m_points[i].PenDown;
\r
53 void CView::PreCreate(CREATESTRUCT &cs)
\r
55 // Set the extra style to provide a sunken effect
\r
56 cs.dwExStyle = WS_EX_CLIENTEDGE;
\r
59 void CView::PreRegisterClass(WNDCLASS &wc)
\r
61 // Set the background brush, class name and cursor
\r
62 wc.hbrBackground = m_Brush;
\r
63 wc.lpszClassName = _T("Scribble Window");
\r
64 wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));
\r
67 BOOL CView::FileOpen(LPCTSTR szFilename)
\r
69 // empty the PlotPoint vector
\r
72 BOOL bResult = FALSE;
\r
74 // Create a handle to the file
\r
76 if (File.Open(szFilename, OPEN_EXISTING))
\r
81 nBytesRead = File.Read(&pp, sizeof(PlotPoint));
\r
82 if (nBytesRead == sizeof(PlotPoint))
\r
83 m_points.push_back(pp);
\r
85 } while (nBytesRead == sizeof(PlotPoint));
\r
87 if ((0 != nBytesRead) || (m_points.empty()))
\r
89 // Failed to read all of the file
\r
91 ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
99 CString strErrMsg = _T("Failed to open file ");
\r
100 strErrMsg += szFilename;
\r
101 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
108 BOOL CView::FileSave(LPCTSTR szFilename)
\r
110 BOOL bResult = TRUE;
\r
112 if (!hFile.Open(szFilename, CREATE_ALWAYS))
\r
114 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
121 for (size_t i = 0; i < m_points.size(); ++i)
\r
123 if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))
\r
125 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
131 // Verify file length
\r
132 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))
\r
134 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
142 void CView::SetPen(COLORREF color)
\r
144 m_PenColor = color;
\r
147 void CView::StorePoint(int x, int y, bool PenDown)
\r
152 P1.PenDown = PenDown;
\r
153 P1.color = m_PenColor;
\r
155 m_points.push_back(P1); //Add the point to the vector
\r
158 void CView::OnLButtonDown(LPARAM lParam)
\r
160 // Capture mouse input.
\r
163 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
166 void CView::OnLButtonUp(LPARAM lParam)
\r
169 //Release the capture on the mouse
\r
172 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);
\r
176 void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)
\r
178 // hold down the left mouse button and move mouse to draw lines.
\r
179 if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )
\r
181 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
\r
182 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
186 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
190 case WM_LBUTTONDOWN:
\r
191 OnLButtonDown(lParam);
\r
195 OnMouseMove(wParam, lParam);
\r
199 OnLButtonUp(lParam);
\r
203 //Use the default message handling for remaining messages
\r
204 return WndProcDefault(uMsg, wParam, lParam);
\r