1 ////////////////////////////////////////////////////
\r
2 // Mainfrm.cpp - definitions for the CMainFrame class
\r
6 #include "resource.h"
\r
9 CMainFrame::CMainFrame()
\r
11 // Set m_View as the view window of the frame
\r
14 // Set the registry key name, and load the initial window position
\r
15 // Use a registry key name like "CompanyName\\Application"
\r
16 LoadRegistrySettings(_T("Win32++\\Scribble Sample"));
\r
18 // Load the settings from the registry with 4 MRU entries
\r
19 LoadRegistryMRUSettings(4);
\r
22 CMainFrame::~CMainFrame()
\r
26 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
\r
28 // Process the messages from the Menu and Tool Bar
\r
30 UNREFERENCED_PARAMETER(lParam);
\r
32 switch (LOWORD(wParam))
\r
35 m_View.ClearPoints();
\r
36 m_strPathName = _T("");
\r
44 case IDM_FILE_SAVEAS:
\r
47 case IDM_FILE_PRINT:
\r
51 TRACE(_T("Red pen selected\n"));
\r
52 m_View.SetPen(RGB(255,0,0));
\r
55 TRACE(_T("Blue pen selected\n"));
\r
56 m_View.SetPen(RGB(0,0,255));
\r
59 TRACE(_T("Green pen selected\n"));
\r
60 m_View.SetPen(RGB(0,196,0));
\r
63 TRACE(_T("Black pen selected\n"));
\r
64 m_View.SetPen(RGB(0,0,0));
\r
66 case IDW_VIEW_STATUSBAR:
\r
69 case IDW_VIEW_TOOLBAR:
\r
72 case IDM_HELP_ABOUT:
\r
76 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
\r
78 case IDW_FILE_MRU_FILE1:
\r
79 case IDW_FILE_MRU_FILE2:
\r
80 case IDW_FILE_MRU_FILE3:
\r
81 case IDW_FILE_MRU_FILE4:
\r
82 case IDW_FILE_MRU_FILE5:
\r
84 UINT nMRUIndex = LOWORD(wParam) - IDW_FILE_MRU_FILE1;
\r
85 CString strMRUText = GetMRUEntry(nMRUIndex);
\r
87 if (m_View.FileOpen(strMRUText))
\r
88 m_strPathName = strMRUText;
\r
90 RemoveMRUEntry(strMRUText);
\r
99 void CMainFrame::OnFileOpen()
\r
102 CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);
\r
104 if (!str.IsEmpty())
\r
106 // Retrieve the PlotPoint data
\r
107 if (m_View.FileOpen(str))
\r
109 // Save the filename
\r
110 m_strPathName = str;
\r
114 m_strPathName=_T("");
\r
118 void CMainFrame::OnFileSave()
\r
120 if (m_strPathName == _T(""))
\r
123 m_View.FileSave(m_strPathName);
\r
126 void CMainFrame::OnFileSaveAs()
\r
129 CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);
\r
131 // Store the PlotPoint data in the file
\r
132 if (!str.IsEmpty())
\r
134 m_strPathName = str;
\r
136 // Save the file name
\r
137 m_View.FileSave(str);
\r
142 // Sends the bitmap extracted from the View window to a printer of your choice
\r
143 // This function provides a useful reference for printing bitmaps in general
\r
144 void CMainFrame::OnFilePrint()
\r
146 // Get the dimensions of the View window
\r
147 CRect rcView = m_View.GetClientRect();
\r
148 int Width = rcView.Width();
\r
149 int Height = rcView.Height();
\r
151 // Copy the bitmap from the View window
\r
152 CClientDC ViewDC(&m_View);
\r
153 CMemDC MemDC(&ViewDC);
\r
155 bmView.CreateCompatibleBitmap(&ViewDC, Width, Height);
\r
156 MemDC.SelectObject(&bmView);
\r
157 BitBlt(MemDC, 0, 0, Width, Height, ViewDC, 0, 0, SRCCOPY);
\r
159 // Bring up a dialog to choose the printer
\r
161 pd.lStructSize = sizeof( pd );
\r
162 pd.Flags = PD_RETURNDC;
\r
163 pd.hwndOwner = m_hWnd;
\r
165 // Retrieve the printer DC
\r
166 if( !PrintDlg( &pd ) )
\r
168 TRACE(_T("PrintDlg canceled"));
\r
172 // Zero and then initialize the members of a DOCINFO structure.
\r
174 memset( &di, 0, sizeof(DOCINFO) );
\r
175 di.cbSize = sizeof(DOCINFO);
\r
176 di.lpszDocName = _T("Scribble Printout");
\r
177 di.lpszOutput = (LPTSTR) NULL;
\r
178 di.lpszDatatype = (LPTSTR) NULL;
\r
181 // Begin a print job by calling the StartDoc function.
\r
182 if (SP_ERROR == StartDoc(pd.hDC, &di))
\r
183 throw CWinException(_T("Failed to start print job"));
\r
185 // Inform the driver that the application is about to begin sending data.
\r
186 if (0 > StartPage(pd.hDC))
\r
187 throw CWinException(_T("StartPage failed"));
\r
189 BITMAPINFOHEADER bi = {0};
\r
190 bi.biSize = sizeof(BITMAPINFOHEADER);
\r
191 bi.biHeight = Height;
\r
192 bi.biWidth = Width;
\r
194 bi.biBitCount = 24;
\r
195 bi.biCompression = BI_RGB;
\r
197 // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps
\r
198 // Get the size of the image data
\r
199 MemDC.GetDIBits(&bmView, 0, Height, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
\r
201 // Retrieve the image data
\r
202 std::vector<byte> vBits(bi.biSizeImage, 0); // a vector to hold the byte array
\r
203 byte* pByteArray = &vBits.front();
\r
204 MemDC.GetDIBits(&bmView, 0, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
\r
206 // Determine the scaling factors required to print the bitmap and retain its original proportions.
\r
207 float fLogPelsX1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSX);
\r
208 float fLogPelsY1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSY);
\r
209 float fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
\r
210 float fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);
\r
211 float fScaleX = MAX(fLogPelsX1, fLogPelsX2) / MIN(fLogPelsX1, fLogPelsX2);
\r
212 float fScaleY = MAX(fLogPelsY1, fLogPelsY2) / MIN(fLogPelsY1, fLogPelsY2);
\r
214 // Compute the coordinates of the upper left corner of the centered bitmap.
\r
215 int cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
\r
216 int xLeft = ((cWidthPels / 2) - ((int) (((float) Width) * fScaleX)) / 2);
\r
217 int cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
\r
218 int yTop = ((cHeightPels / 2) - ((int) (((float) Height) * fScaleY)) / 2);
\r
220 // Use StretchDIBits to scale the bitmap and maintain its original proportions
\r
221 if (GDI_ERROR == (UINT)StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) Width * fScaleX),
\r
222 (int) ((float) Height * fScaleY), 0, 0, Width, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY))
\r
224 throw CWinException(_T("Failed to resize image for printing"));
\r
227 // Inform the driver that the page is finished.
\r
228 if (0 > EndPage(pd.hDC))
\r
229 throw CWinException(_T("EndPage failed"));
\r
231 // Inform the driver that document has ended.
\r
232 if(0 > EndDoc(pd.hDC))
\r
233 throw CWinException(_T("EndDoc failed"));
\r
236 void CMainFrame::OnInitialUpdate()
\r
238 // Here we process the command line arguments, and automatically load a file if one is specified.
\r
239 // GetCommandLineW retrieves our command line arguments.
\r
240 // CommandLineToArgvW parses the command line arguements in to an array of strings
\r
241 // The first string (lpArgv[0]) contains the name of our program
\r
242 // The second string (lpArg[1]) contains an additional parameter (presumably a filename to load).
\r
243 // CommandLineToArgvW is not supported in Win95, Win98 or WinME
\r
246 // CommandLineToArgvW might not be supported, so use run-time dynamic linking to call the function
\r
247 HMODULE hMod = LoadLibrary(_T("Shell32.dll"));
\r
250 // Get a pointer to the CommandLineToArgvW function
\r
251 LPWSTR* (WINAPI* fpGetCommandLineW)(LPCWSTR, int*);
\r
252 fpGetCommandLineW = (LPWSTR* (WINAPI*)(LPCWSTR, int*))::GetProcAddress(hMod, "CommandLineToArgvW");
\r
254 if (fpGetCommandLineW)
\r
257 LPWSTR* lpArgv = (*fpGetCommandLineW)(::GetCommandLineW(), &argCount);
\r
259 // The second argument (if any) contains our file name.
\r
262 m_View.FileOpen((W2T(lpArgv[1])));
\r
272 // This works on Win2000 and above
\r
274 LPWSTR* lpArgv = ::CommandLineToArgvW(::GetCommandLineW(), &argCount);
\r
276 // The second argument (if any) contains our file name.
\r
279 m_View.FileOpen((W2T(lpArgv[1])));
\r
284 void CMainFrame::SetupToolBar()
\r
286 // Define our toolbar
\r
287 AddToolBarButton( IDM_FILE_NEW );
\r
288 AddToolBarButton( IDM_FILE_OPEN );
\r
289 AddToolBarButton( IDM_FILE_SAVE );
\r
290 AddToolBarButton( 0 ); // Separator
\r
291 AddToolBarButton( IDM_EDIT_CUT, FALSE );
\r
292 AddToolBarButton( IDM_EDIT_COPY, FALSE );
\r
293 AddToolBarButton( IDM_EDIT_PASTE, FALSE );
\r
294 AddToolBarButton( IDM_FILE_PRINT );
\r
295 AddToolBarButton( 0 ); // Separator
\r
296 AddToolBarButton( IDM_PEN_RED );
\r
297 AddToolBarButton( IDM_PEN_BLUE );
\r
298 AddToolBarButton( IDM_PEN_GREEN );
\r
299 AddToolBarButton( IDM_PEN_BLACK );
\r
300 AddToolBarButton( 0 ); // Separator
\r
301 AddToolBarButton( IDM_HELP_ABOUT );
\r
304 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
309 // } // switch (uMsg)
\r
311 return WndProcDefault(uMsg, wParam, lParam);
\r
312 } // LRESULT CMainFrame::WndProc(...)
\r