|
|
7.0. The Scope Program - Part B |
Figure 7.9b. Scope display was pasted into this document from the clipboard |
Figure 7.10a. User clicks [Export] | Figure 7.10b. Save dialogue box appears |
Figure 7.10c. Screen dump of exported file scope.bmp |
HANDLE CScopeView::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal) { /* ----------------------------------------------------------- | Fuction: HANDLE CScopeView::DDBToDIB(...) | | Description: Convert Device-Dependent Bitmap (DDB) to | | Device-independent Bitmap (DIB). | | Return: Void. | | Date: 16/02/2002 | | Verison: 1.3 | | By: Colin K McCord | ----------------------------------------------------------- */ // DDBToDIB - Creates a DIB from a DDB // bitmap - Device dependent bitmap // dwCompression- Type of compression - see BITMAPINFOHEADER // pPal - Logical palette BITMAP bm; BITMAPINFOHEADER bi; LPBITMAPINFOHEADER lpbi; DWORD dwLen; HANDLE hDIB; HANDLE handle; HDC hDC; HPALETTE hPal; ASSERT( bitmap.GetSafeHandle() ); // The function has no arg for bitfields if( dwCompression == BI_BITFIELDS ) return NULL; // If a palette has not been supplied use defaul palette hPal = (HPALETTE) pPal->GetSafeHandle(); if (hPal==NULL) hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information bitmap.GetObject(sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bm.bmWidth; bi.biHeight = bm.bmHeight; bi.biPlanes = 1; bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel; bi.biCompression = dwCompression; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // Compute the size of the infoheader and the color table int nColors = (1 << bi.biBitCount); dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from hDC = ::GetDC(NULL); hPal = SelectPalette(hDC,hPal,FALSE); RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB) { SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return NULL; } lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver // will calculate the biSizeImage field GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight, (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it // Each scan line of the image is aligned on a DWORD (32bit) boundary if (bi.biSizeImage == 0){ bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) * bi.biHeight; // If a compression scheme is used the result may infact be larger // Increase the size to account for this. if (dwCompression != BI_RGB) bi.biSizeImage = (bi.biSizeImage * 3) / 2; } // Realloc the buffer so that it can hold all the bits dwLen += bi.biSizeImage; if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE)) hDIB = handle; else{ GlobalFree(hDIB); // Reselect the original palette SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return NULL; } // Get the bitmap bits lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, // Start scan line (DWORD)bi.biHeight, // # of scan lines (LPBYTE)lpbi // address for bitmap bits + (bi.biSize + nColors * sizeof(RGBQUAD)), (LPBITMAPINFO)lpbi, // address of bitmapinfo (DWORD)DIB_RGB_COLORS); // Use RGB for colour table if( !bGotBits ) { GlobalFree(hDIB); SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return NULL; } SelectPalette(hDC,hPal,FALSE); ::ReleaseDC(NULL,hDC); return hDIB; }
|
void CScopeView::OnFileExport() { /* ----------------------------------------------------------- | Fuction: void CScopeView::OnFileExport() | | Description: User specifies filename using CFileDialog, | | then the scope display is save to that | | filename using the standard bitmap | | graphic format. | | Return: Void. | | Date: 16/02/2002 | | Verison: 1.1 | | By: Colin K McCord | ----------------------------------------------------------- */ HANDLE hDIB; CRect rect; CClientDC dc(this); CDC memDC; CBitmap bitmap; GetClientRect(&rect); // Create memDC memDC.CreateCompatibleDC(&dc); bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); CBitmap* pOldBitmap = memDC.SelectObject(&bitmap); // Fill in memDC memDC.FillSolidRect(rect, dc.GetBkColor()); // Redraw grid to memory DC if (memDC.GetSafeHdc() != NULL) { // first drop the grid on the memory dc memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcGrid, 0, 0, SRCCOPY); // now add the plot on top as a "pattern" via SRCPAINT. // works well with dark background and a light plot memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcCH1, 0, 0, SRCPAINT); //SRCPAINT memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcCH2, 0, 0, SRCPAINT); //SRCPAINT memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcCH3, 0, 0, SRCPAINT); //SRCPAINT memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcCH4, 0, 0, SRCPAINT); //SRCPAINT } /* Convert Device-Dependent Bitmap (DDB) to Device-independent Bitmap (DIB) */ hDIB = DDBToDIB(bitmap, BI_RGB, NULL); CFileDialog dlg (FALSE, _T("*.bmp"), _T("Scope"), OFN_HIDEREADONLY,_T("Windows or OS/2 Bitmap (*.bmp)|*.bmp||")); dlg.m_ofn.lpstrTitle = "Export Scope Display"; if (IDOK == dlg.DoModal()) { /* Check for existence */ if( (_access(dlg.GetPathName(), 0 )) != -1 ) { if(MessageBox("File '" + dlg.GetPathName() + "' Already exists. Do you want to replace the existing file ?","Export Scope Display", MB_YESNO | MB_ICONINFORMATION) != IDYES) return; } WriteDIB(dlg.GetPathName(),hDIB); // Save as Bitmap } } |
BOOL CScopeView::WriteDIB(CString szFile, HANDLE hDIB) { /* ----------------------------------------------------------- | Fuction: BOOL CScopeView::WriteDIB(........) | | Description: Writes bitmap to specified file. | | Return: Void. | | Date: 16/02/2002 | | Verison: 1.1 | | By: Colin K McCord | ----------------------------------------------------------- */ // WriteDIB - Writes a DIB to file // Returns - TRUE on success // szFile - Name of file to write to // hDIB - Handle of the DIB BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; if (!hDIB) return FALSE; CFile file; if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) ) return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr ); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize + nColors * sizeof(RGBQUAD)); // Write the file header file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits file.Write( lpbi, GlobalSize(hDIB) ); return TRUE; } |
Figure 7.11a. Microsoft Word XP file menu | Figure 7.11b. Scope Ver1.036a file Menu |
Figure 7.12b: Screen dump of Windows 98 running ‘Scope’ V1.008a taken after 40 seconds |
Figure 7.12c: Screen dump of Windows 2000 running ‘Scope’ V1.008a |
Figure 7.12d: Screen dump of Windows 98 running ‘Scope’ V1.010a | Figure 7.12e: Screen dump of Windows 2000 running ‘Scope’ V1.010a |
Figure 7.13a. Screen dump of Compatibility option in Win XP | Figure 7.13b. Screen dump of display properties in Win XP |
Figure 7.14b. nView Menu Extension Options | Figure 7.14c. nView Desktop Manager |
This Web Page was last updated on Friday June 28, 2002
Home About me National Record Of Achievement Hobbies / Interests Guest Book Contact Me Links Snooker Amateur Radio Site Map
© 2002 Designed by Colin K McCord |
|
This website is best viewed by Microsoft Internet Explorer 6.0 at a resolution of 1024 x 768