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