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