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