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_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
38 // Here we use double buffering (drawing to a memory DC) for smoother rendering
\r
39 // Set up our Memory DC and bitmap
\r
41 int Width = GetClientRect().Width();
\r
42 int Height = GetClientRect().Height();
\r
43 MemDC.CreateCompatibleBitmap(pDC, Width, Height);
\r
44 MemDC.FillRect(GetClientRect(), &m_Brush);
\r
46 if (m_points.size() > 0)
\r
48 bool bDraw = false; //Start with the pen up
\r
49 for (unsigned int i = 0 ; i < m_points.size(); i++)
\r
52 MemDC.CreatePen(PS_SOLID, 1, m_points[i].color);
\r
54 MemDC.LineTo(m_points[i].x, m_points[i].y);
\r
56 MemDC.MoveTo(m_points[i].x, m_points[i].y);
\r
58 bDraw = m_points[i].PenDown;
\r
62 // Copy from the memory DC to our painting dc
\r
63 pDC->BitBlt(0, 0, Width, Height, &MemDC, 0, 0, SRCCOPY);
\r
66 void CView::PreCreate(CREATESTRUCT &cs)
\r
68 // Set the extra style to provide a sunken effect
\r
69 cs.dwExStyle = WS_EX_CLIENTEDGE;
\r
72 void CView::PreRegisterClass(WNDCLASS &wc)
\r
74 // Set the background brush, class name and cursor
\r
75 wc.hbrBackground = m_Brush;
\r
76 wc.lpszClassName = _T("Scribble Window");
\r
77 wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));
\r
80 BOOL CView::FileOpen(LPCTSTR szFilename)
\r
82 // empty the PlotPoint vector
\r
85 BOOL bResult = FALSE;
\r
87 // Create a handle to the file
\r
89 if (File.Open(szFilename, OPEN_EXISTING))
\r
94 nBytesRead = File.Read(&pp, sizeof(PlotPoint));
\r
95 if (nBytesRead == sizeof(PlotPoint))
\r
96 m_points.push_back(pp);
\r
98 } while (nBytesRead == sizeof(PlotPoint));
\r
100 if ((0 != nBytesRead) || (m_points.empty()))
\r
102 // Failed to read all of the file
\r
104 ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
112 CString strErrMsg = _T("Failed to open file ");
\r
113 strErrMsg += szFilename;
\r
114 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
121 BOOL CView::FileSave(LPCTSTR szFilename)
\r
123 BOOL bResult = TRUE;
\r
125 if (!hFile.Open(szFilename, CREATE_ALWAYS))
\r
127 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
134 for (size_t i = 0; i < m_points.size(); ++i)
\r
136 if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))
\r
138 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
144 // Verify file length
\r
145 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))
\r
147 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);
\r
155 void CView::SetPen(COLORREF color)
\r
157 m_PenColor = color;
\r
160 void CView::StorePoint(int x, int y, bool PenDown)
\r
165 P1.PenDown = PenDown;
\r
166 P1.color = m_PenColor;
\r
168 m_points.push_back(P1); //Add the point to the vector
\r
171 void CView::OnLButtonDown(LPARAM lParam)
\r
173 // Capture mouse input.
\r
176 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
179 void CView::OnLButtonUp(LPARAM lParam)
\r
182 //Release the capture on the mouse
\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
195 str.Format( _T("Draw Point: %hd, %hd\n"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) );
\r
198 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
\r
199 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);
\r
203 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
207 case WM_LBUTTONDOWN:
\r
208 OnLButtonDown(lParam);
\r
212 OnMouseMove(wParam, lParam);
\r
216 OnLButtonUp(lParam);
\r
220 //Use the default message handling for remaining messages
\r
221 return WndProcDefault(uMsg, wParam, lParam);
\r