|
|
1.1.1.2 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /**************************************************************************\ 13: * bitmap.c -- support for reading in and drawing bitmaps. 14: \**************************************************************************/ 15: 16: #include <windows.h> 17: #include <commdlg.h> 18: 19: 20: 21: /**************************************************************************\ 22: * 23: * function: DrawBitmap() 24: * 25: * input parameters: HDC, HBITMAP 26: * 27: * Draw the bitmap into the hdc. Source rectangle computed to include the 28: * whole bitmap. Destination location is 0,0. 29: * 30: * global variables: none. 31: * 32: \**************************************************************************/ 33: VOID DrawBitmap (HDC hdc, HBITMAP hbm) 34: { 35: BOOL f; 36: HDC hdcBits; 37: BITMAP bm; 38: 39: hdcBits = CreateCompatibleDC(hdc); 40: GetObject (hbm, sizeof(BITMAP), &bm); 41: SelectObject(hdcBits,hbm); 42: f = BitBlt(hdc,0,0,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY); 43: DeleteDC(hdcBits); 44: } 45: 46: 47: 48: /**************************************************************************\ 49: * 50: * function: GetBitmap() 51: * 52: * input parameters: 53: * hdc - hdc to make the bitmap compatible with. 54: * hInst - instance handle 55: * 56: * Put up a common dialog box to open a new *.BMP file. 57: * Once this is complete, open the file, read in the information, 58: * and create a compatible bitmap. 59: * 60: * returns: handle to the bitmap iff successful. NULL otherwise. 61: * 62: \**************************************************************************/ 63: HBITMAP GetBitmap (HDC hdc, HANDLE hInst, BOOL monochrome) 64: { 65: HBITMAP hbm; 66: PBITMAPFILEHEADER pbmfh; 67: PBITMAPINFOHEADER pbmih; 68: PBYTE pBits; 69: int fh; 70: int bfOffBits; 71: int nbytes; 72: OPENFILENAME of; 73: char buffer [MAX_PATH]; 74: 75: buffer[0] = 0; 76: 77: /* set up the OPENFILE structure, 78: * then use the appropriate common dialog 79: */ 80: of.lStructSize = sizeof (OPENFILENAME); 81: of.hwndOwner = NULL; 82: of.hInstance = hInst; 83: of.lpstrFilter = "Bitmaps\000 *.BMP\000\000"; 84: of.lpstrCustomFilter = NULL; 1.1.1.2 ! root 85: of.nMaxCustFilter = 0; ! 86: of.nFilterIndex = 0; 1.1 root 87: of.lpstrFile = buffer; 88: of.nMaxFile = MAX_PATH; 89: of.lpstrFileTitle = NULL; 1.1.1.2 ! root 90: of.nMaxFileTitle = 0; 1.1 root 91: of.lpstrInitialDir = "c:\\nt\\windows"; 92: of.lpstrTitle = NULL; 1.1.1.2 ! root 93: of.Flags = OFN_HIDEREADONLY; ! 94: of.nFileOffset = 0; ! 95: of.nFileExtension = 0; 1.1 root 96: of.lpstrDefExt = NULL; 1.1.1.2 ! root 97: of.lCustData = 0; 1.1 root 98: of.lpfnHook = NULL; 99: of.lpTemplateName = NULL; 100: if (!GetOpenFileName (&of)) return NULL; 101: 102: /* Try to open the file. If successful, then allocate space for it, 103: * and read in all of the bytes. 104: */ 105: fh = _lopen (buffer, OF_READ); 106: if (fh == -1) return NULL; 107: nbytes = GetFileSize ((HANDLE) fh, NULL); 108: 109: /* The contents of the file are read in here in three parts. First 110: * the bitmap file header is read, then the bitmap header along with 111: * the color table, then finally the actual bit data. I.e. from 112: * a total of nbytes... 113: * 1. sizeof (BITMAPFILEHEADER) 114: * 2. bfOffBits- sizeof (BITMAPFILEHEADER) 115: * 3. (nbytes - bfOffBits) 116: */ 117: 118: /* read in the bitmap file header. save the offset to bits. */ 119: if (!(pbmfh = (PBITMAPFILEHEADER)LocalAlloc (LPTR, sizeof (BITMAPFILEHEADER)))) 120: return NULL; 121: _lread (fh, (LPSTR)pbmfh, sizeof (BITMAPFILEHEADER)); 122: bfOffBits=pbmfh->bfOffBits; 123: 124: /* read in the bitmap info header and the color table right after it. 125: * both BITMAPINFOHEADER and BITMAPINFO needed for CreateDIBitmap() 126: */ 127: if (!(pbmih = (PBITMAPINFOHEADER)LocalAlloc (LPTR, bfOffBits- sizeof (BITMAPFILEHEADER)))) 128: return NULL; 129: _lread (fh, (LPSTR)pbmih, bfOffBits- sizeof (BITMAPFILEHEADER)); 130: 131: /* finally read in the bit data. */ 132: if (!(pBits = (PBYTE)LocalAlloc (LPTR, (nbytes - bfOffBits)))) return NULL; 133: _lread (fh, (LPSTR)pBits, (nbytes - bfOffBits)); 134: 135: 136: /* in the case of desiring a monochrome bitmap (input parameter), 1.1.1.2 ! root 137: * verify that is what we got. If it is, then use CreateBitmap, ! 138: * else use a device independent bitmap. 1.1 root 139: */ 1.1.1.2 ! root 140: if (monochrome) { ! 141: if (pbmih->biBitCount != 1) { 1.1 root 142: MessageBox (NULL, "Mask must be monochrome bitmap.", 143: "Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK); 144: hbm = NULL; 1.1.1.2 ! root 145: } else { ! 146: hbm = CreateBitmap (pbmih->biWidth, pbmih->biHeight, ! 147: pbmih->biPlanes, pbmih->biBitCount, ! 148: pBits); ! 149: } ! 150: } else /* bitmap is NOT monochrome, use DIB. */ ! 151: hbm = CreateDIBitmap (hdc, pbmih, CBM_INIT, 1.1 root 152: pBits, (PBITMAPINFO) pbmih, DIB_RGB_COLORS); 153: 1.1.1.2 ! root 154: ! 155: /* hbm is set... free up memory, and return */ 1.1 root 156: LocalFree (LocalHandle ((LPSTR)pBits)); 157: LocalFree (LocalHandle ((LPSTR)pbmih)); 158: LocalFree (LocalHandle ((LPSTR)pbmfh)); 159: _lclose (fh); 160: 161: return hbm; 162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.