1 ////////////////////////////////////////////////////
\r
2 // Mainfrm.cpp - definitions for the CMainFrame class
\r
5 #include "resource.h"
\r
8 CMainFrame::CMainFrame()
\r
10 // Set m_View as the view window of the frame
\r
14 CMainFrame::~CMainFrame()
\r
18 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
\r
20 // Process the messages from the Menu and Tool Bar
\r
21 switch (LOWORD(wParam))
\r
24 m_View.ClearPoints();
\r
25 m_PathName = _T("");
\r
33 case IDM_FILE_SAVEAS:
\r
36 case IDM_FILE_PRINT:
\r
40 m_View.SetPen(RGB(255,0,0));
\r
43 m_View.SetPen(RGB(0,0,255));
\r
46 m_View.SetPen(RGB(0,196,0));
\r
49 m_View.SetPen(RGB(0,0,0));
\r
51 case IDM_HELP_ABOUT:
\r
55 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
\r
62 void CMainFrame::OnFileOpen()
\r
65 CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);
\r
69 // Retrieve the PlotPoint data
\r
70 if (m_View.FileOpen(str))
\r
72 // Save the filename
\r
81 void CMainFrame::OnFileSave()
\r
83 if (m_PathName == _T(""))
\r
86 m_View.FileSave(m_PathName);
\r
89 void CMainFrame::OnFileSaveAs()
\r
92 CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);
\r
94 // Store the PlotPoint data in the file
\r
99 // Save the file name
\r
100 m_View.FileSave(str);
\r
105 // Sends the bitmap extracted from the View window to a printer of your choice
\r
106 // This function provides a useful reference for printing bitmaps in general
\r
107 void CMainFrame::OnFilePrint()
\r
109 // Get the dimensions of the View window
\r
110 CRect rcView = m_View.GetClientRect();
\r
111 int Width = rcView.Width();
\r
112 int Height = rcView.Height();
\r
114 // Copy the bitmap from the View window
\r
115 CClientDC dcView(&m_View);
\r
116 CMemDC MemDC(&dcView);
\r
118 bmView.CreateCompatibleBitmap(&dcView, Width, Height);
\r
119 MemDC.SelectObject(&bmView);
\r
120 MemDC.BitBlt(0, 0, Width, Height, &dcView, 0, 0, SRCCOPY);
\r
122 // Bring up a dialog to choose the printer
\r
124 pd.lStructSize = sizeof( pd );
\r
125 pd.Flags = PD_RETURNDC;
\r
126 pd.hwndOwner = m_hWnd;
\r
128 // Retrieve the printer DC
\r
129 if( !PrintDlg( &pd ) )
\r
131 TRACE(_T("PrintDlg canceled"));
\r
135 // Zero and then initialize the members of a DOCINFO structure.
\r
137 memset( &di, 0, sizeof(DOCINFO) );
\r
138 di.cbSize = sizeof(DOCINFO);
\r
139 di.lpszDocName = _T("Scribble Printout");
\r
140 di.lpszOutput = (LPTSTR) NULL;
\r
141 di.lpszDatatype = (LPTSTR) NULL;
\r
144 // Begin a print job by calling the StartDoc function.
\r
145 if (SP_ERROR == StartDoc(pd.hDC, &di))
\r
146 throw CWinException(_T("Failed to start print job"));
\r
148 // Inform the driver that the application is about to begin sending data.
\r
149 if (0 > StartPage(pd.hDC))
\r
150 throw CWinException(_T("StartPage failed"));
\r
152 BITMAPINFOHEADER bi = {0};
\r
153 bi.biSize = sizeof(BITMAPINFOHEADER);
\r
154 bi.biHeight = Height;
\r
155 bi.biWidth = Width;
\r
157 bi.biBitCount = 24;
\r
158 bi.biCompression = BI_RGB;
\r
160 // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps
\r
161 // Get the size of the image data
\r
162 MemDC.GetDIBits(&bmView, 0, Height, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
\r
164 // Retrieve the image data
\r
165 std::vector<byte> vBits(bi.biSizeImage, 0); // a vector to hold the byte array
\r
166 byte* pByteArray = &vBits.front();
\r
167 MemDC.GetDIBits(&bmView, 0, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
\r
169 // Determine the scaling factors required to print the bitmap and retain its original proportions.
\r
170 float fLogPelsX1 = (float) dcView.GetDeviceCaps(LOGPIXELSX);
\r
171 float fLogPelsY1 = (float) dcView.GetDeviceCaps(LOGPIXELSY);
\r
172 float fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
\r
173 float fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);
\r
174 float fScaleX = MAX(fLogPelsX1, fLogPelsX2) / MIN(fLogPelsX1, fLogPelsX2);
\r
175 float fScaleY = MAX(fLogPelsY1, fLogPelsY2) / MIN(fLogPelsY1, fLogPelsY2);
\r
177 // Compute the coordinates of the upper left corner of the centered bitmap.
\r
178 int cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
\r
179 int xLeft = ((cWidthPels / 2) - ((int) (((float) Width) * fScaleX)) / 2);
\r
180 int cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
\r
181 int yTop = ((cHeightPels / 2) - ((int) (((float) Height) * fScaleY)) / 2);
\r
183 // Use StretchDIBits to scale the bitmap and maintain its original proportions
\r
184 if (GDI_ERROR == (UINT)StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) Width * fScaleX),
\r
185 (int) ((float) Height * fScaleY), 0, 0, Width, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY))
\r
187 throw CWinException(_T("Failed to resize image for printing"));
\r
190 // Inform the driver that the page is finished.
\r
191 if (0 > EndPage(pd.hDC))
\r
192 throw CWinException(_T("EndPage failed"));
\r
194 // Inform the driver that document has ended.
\r
195 if(0 > EndDoc(pd.hDC))
\r
196 throw CWinException(_T("EndDoc failed"));
\r
199 void CMainFrame::SetupToolBar()
\r
201 // Set the Resource IDs for the toolbar buttons
\r
202 AddToolBarButton( IDM_FILE_NEW );
\r
203 AddToolBarButton( IDM_FILE_OPEN );
\r
204 AddToolBarButton( IDM_FILE_SAVE );
\r
206 AddToolBarButton( 0 ); // Separator
\r
207 AddToolBarButton( IDM_EDIT_CUT );
\r
208 AddToolBarButton( IDM_EDIT_COPY );
\r
209 AddToolBarButton( IDM_EDIT_PASTE );
\r
211 AddToolBarButton( 0 ); // Separator
\r
212 AddToolBarButton( IDM_FILE_PRINT );
\r
214 AddToolBarButton( 0 ); // Separator
\r
215 AddToolBarButton ( IDM_PEN_RED );
\r
216 AddToolBarButton ( IDM_PEN_BLUE );
\r
217 AddToolBarButton ( IDM_PEN_GREEN );
\r
218 AddToolBarButton ( IDM_PEN_BLACK );
\r
219 AddToolBarButton ( IDM_HELP_ABOUT );
\r
222 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
227 // } // switch (uMsg)
\r
229 return WndProcDefault(uMsg, wParam, lParam);
\r
230 } // LRESULT CMainFrame::WndProc(...)
\r