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