1 //////////////////////////////////////////////
\r
3 // Definitions for the CView class
\r
7 #include "resource.h"
\r
10 using namespace std;
\r
12 CView::CView() : m_PenColor(RGB(0,0,0))
\r
14 m_hBrush.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 // Here we use double buffering (drawing to a memory DC) for smoother rendering
\r
38 // Set up our Memory DC and bitmap
\r
40 int Width = GetClientRect().Width();
\r
41 int Height = GetClientRect().Height();
\r
42 MemDC.CreateCompatibleBitmap(pDC, Width, Height);
\r
43 MemDC.FillRect(GetClientRect(), &m_hBrush);
\r
45 if (m_points.size() > 0)
\r
47 bool bDraw = false; //Start with the pen up
\r
48 for (unsigned int i = 0 ; i < m_points.size(); i++)
\r
51 MemDC.CreatePen(PS_SOLID, 1, m_points[i].color);
\r
53 MemDC.LineTo(m_points[i].x, m_points[i].y);
\r
55 MemDC.MoveTo(m_points[i].x, m_points[i].y);
\r
57 bDraw = m_points[i].PenDown;
\r
61 // Copy from the memory DC to our painting dc
\r
62 pDC->BitBlt(0, 0, Width, Height, &MemDC, 0, 0, SRCCOPY);
\r
65 void CView::PreCreate(CREATESTRUCT &cs)
\r
67 // Set the extra style to provide a sunken effect
\r
68 cs.dwExStyle = WS_EX_CLIENTEDGE;
\r
71 void CView::PreRegisterClass(WNDCLASS &wc)
\r
73 // Set the background brush, class name and cursor
\r
74 wc.hbrBackground = m_hBrush;
\r
75 wc.lpszClassName = _T("Scribble Window");
\r
76 wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));
\r
79 BOOL CView::FileOpen(LPCTSTR szFilename)
\r
81 // empty the PlotPoint vector
\r
84 BOOL bResult = FALSE;
\r
86 // Create a handle to the file
\r
88 if (File.Open(szFilename, OPEN_EXISTING))
\r
93 nBytesRead = File.Read(&pp, sizeof(PlotPoint));
\r
94 if (nBytesRead == sizeof(PlotPoint))
\r
95 m_points.push_back(pp);
\r
97 } while (nBytesRead == sizeof(PlotPoint));
\r
99 if ((0 != nBytesRead) || (m_points.empty()))
\r
101 // Failed to read all of the file
\r
103 ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
111 CString strErrMsg = _T("Failed to open file ");
\r
112 strErrMsg += szFilename;
\r
113 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
120 BOOL CView::FileSave(LPCTSTR szFilename)
\r
122 BOOL bResult = TRUE;
\r
124 if (!hFile.Open(szFilename, CREATE_ALWAYS))
\r
126 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
133 for (size_t i = 0; i < m_points.size(); ++i)
\r
135 if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))
\r
137 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
143 // Verify file length
\r
144 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))
\r
146 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
154 void CView::SetPen(COLORREF color)
\r
156 m_PenColor = color;
\r
159 void CView::StorePoint(int x, int y, bool PenDown)
\r
164 P1.PenDown = PenDown;
\r
165 P1.color = m_PenColor;
\r
167 m_points.push_back(P1); //Add the point to the vector
\r
170 void CView::OnLButtonDown(LPARAM lParam)
\r
172 // Capture mouse input.
\r
173 ::SetCapture(m_hWnd);
\r
175 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
178 void CView::OnLButtonUp(LPARAM lParam)
\r
180 if (GetCapture() == this)
\r
182 //Release the capture on the mouse
\r
183 ::ReleaseCapture();
\r
185 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);
\r
189 void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)
\r
191 // hold down the left mouse button and move mouse to draw lines.
\r
192 if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )
\r
194 if (GetCapture() == this)
\r
197 str.Format( _T("Draw Point: %hd, %hd\n"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) );
\r
200 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
\r
201 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
206 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
210 case WM_LBUTTONDOWN:
\r
211 OnLButtonDown(lParam);
\r
215 OnMouseMove(wParam, lParam);
\r
219 OnLButtonUp(lParam);
\r
223 //Use the default message handling for remaining messages
\r
224 return WndProcDefault(uMsg, wParam, lParam);
\r