1 //* Written by Microsoft Product Support Services, Windows Developer Support. *
2 //*****************************************************************************
3 // (C) Copyright Microsoft Corp. 1993. All rights reserved.
4 // You have a royalty-free right to use, modify, reproduce and
5 // distribute the Sample Files (and/or any modified version) in
6 // any way you find useful, provided that you agree that
7 // Microsoft has no warranty obligations or liability for any
8 // Sample Application Files which are modified.
10 // Modified by Petr Smilauer, March 1994 for wxWin library purposes!
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/msw/gnuwin32/extra.h"
26 #include "wx/msw/curicop.h"
27 #include "wx/msw/curico.h"
29 //*****************************************************************************
30 //* Function : ReadIconFile() *
31 //* Purpose : Reads an icon resource file and creates an icon based on that *
33 //* Parameters : char *szFileName - The icon resource file. *
34 //* Returns : A handle to an icon. The handle will be NULL if an icon cannot *
35 //* be created for any reason. *
36 //*****************************************************************************
38 HICON
ReadIconFile( char *szFileName
, HINSTANCE hInst
, int *W
, int *H
)
42 if( (hDIB
= ReadIcon(szFileName
, W
, H
)) == NULL
)
43 // read the icon DIB from file
45 hIcon
= MakeIcon( hDIB
, hInst
); // create an icon from DIB
50 //*****************************************************************************
51 //* Function : CursorToIcon() *
52 //* Purpose : Reads a cursor resource file and creates an icon based on that *
54 //* Parameters : char *szFileName - The cursor resource file. *
55 //* Returns : A handle to an icon. The handle will be NULL if an icon cannot *
56 //* be created for any reason. *
57 //* Comments : A cursor is monochrome. So, the resulting icon will also be *
59 //*****************************************************************************
61 HICON
CursorToIcon( char *szFileName
, HINSTANCE hInst
, int *W
, int *H
)
62 { HANDLE hDIB
; // Handle to DIB memory
63 HICON hIcon
; // Handle to Icon
65 if( (hDIB
= ReadCur( szFileName
, NULL
, W
, H
)) == NULL
)
68 hIcon
= MakeIcon( hDIB
, hInst
); // make icon from cursor DIB
73 //*****************************************************************************
74 //* Function : ReadIcon() *
75 //* Purpose : Reads an icon resource file and extracts the DIB information. *
76 //* Parameters : char *szFileName - The icon resource file. *
77 //* Returns : A handle to a DIB. The handle will be NULL if the resource file*
78 //* is corrupt or if memory cannot be allocated for the DIB info. *
79 //*****************************************************************************
81 HANDLE
ReadIcon( char *szFileName
, int *W
, int *H
)
82 { ICONFILEHEADER iconFileHead
; // ICON file header structure
83 ICONFILERES iconFileRes
; // ICON file resource
86 cbBits
; // Used for reading in file
87 int hFile
; // File handle
88 LPBITMAPINFO lpDIB
; // Pointer to DIB memory
90 int nWidth
= GetSystemMetrics( SM_CXICON
),
91 nHeight
= GetSystemMetrics( SM_CYICON
),
94 // Open and read the .ICO file header and the first ICONFILERES
95 hFile
= _lopen( szFileName
, OF_READ
);
96 cbHead
= _lread( hFile
, (LPSTR
)&iconFileHead
, sizeof(ICONFILEHEADER
));
97 cbRes
= _lread( hFile
, (LPSTR
)&iconFileRes
, sizeof(ICONFILERES
));
100 if((cbHead
!= sizeof( ICONFILEHEADER
)) || (cbRes
!= sizeof( ICONFILERES
)))
103 // Verify that it's an .ICON file
104 if( iconFileHead
.wResourceType
!= 1)
108 while( (nDirEntries
< iconFileHead
.wResourceCount
) &&
109 ((iconFileRes
.bWidth
!= nWidth
) || (iconFileRes
.bHeight
!= nHeight
)))
111 cbRes
= _lread( hFile
, (LPSTR
)&iconFileRes
, sizeof( ICONFILERES
));
112 if(cbRes
!= sizeof( ICONFILERES
))
119 *W
= iconFileRes
.bWidth
;
121 *H
= iconFileRes
.bHeight
;
123 // Allocate and lock memory to read in the DIB
124 hDIB
= GlobalAlloc(GHND
, iconFileRes
.dwDIBSize
);
127 #ifdef __WINDOWS_386__
128 lpDIB
= (LPBITMAPINFO
)MK_FP32(GlobalLock(hDIB
));
130 lpDIB
= (LPBITMAPINFO
)GlobalLock(hDIB
);
133 // Now read the DIB portion of the file, which follows the
134 // end of icon resource table
135 _llseek( hFile
, iconFileRes
.dwDIBOffset
, 0);
136 cbBits
= _lread( hFile
, (LPSTR
)lpDIB
, (WORD
)iconFileRes
.dwDIBSize
);
143 if( (DWORD
)cbBits
!= iconFileRes
.dwDIBSize
)
151 //*****************************************************************************
152 //* Function : MakeIcon() *
153 //* Purpose : Creates an icon based on the DIB info. returned by ReadIcon. *
154 //* Parameters : HANDLE hDIB - A handle to the icon's DIB information. *
155 //* Returns : A handle to an Icon. NULL is returned if an icon cannot be *
156 //* successfully created. *
157 //* Comments : The steps involved in making an icon from a DIB are very *
158 //* similar to those involved in making a cursor from a DIB. *
159 //* Steps : 1) Obtain a pointer to the Icon's DIB bits. *
160 //* 2) Divide the DIB'd height with 2 to account for the fact that the*
161 //* DIB stores both the XOR and the AND masks, one after the other.*
162 //* 3) Determine the offset to the XOR bits. *
163 //* 4) Determine the offset to the AND bits. *
164 //* 5) Create a device dependent bitmap with the XOR bits. *
165 //* 6) Obtain the device dependent XOR bitmask and save in memory. *
166 //* The AND bitmask is monochrome. Monochrome bits are identical *
167 //* in both the device dependent bitmaps and device independent *
168 //* bitmaps. So, no need to convert the AND bitmask. *
169 //* 7) Since a DIB is stored upside down, flip the monochrome AND bits*
171 //* 8) Use the XOR and AND bits and create an icon with CreateIcon. *
172 //*****************************************************************************
174 HICON
MakeIcon( HANDLE hDIB
, HINSTANCE hInst
)
176 lpANDbits
; // Pointer to XOR and AND bits
177 HBITMAP hbmXor
; // handle to XOR bitmap
178 BITMAP bmpXor
; // Used to manipulate XOR bitmap
179 DWORD dwBmpSize
; // Size of XOR bitmap
189 // 1) Obtain a pointer to the Icon's DIB bits.
190 #ifdef __WINDOWS_386__
191 lpDIB
= (LPBITMAPINFO
)MK_FP32(GlobalLock( hDIB
));
193 lpDIB
= (LPBITMAPINFO
)GlobalLock( hDIB
);
196 // 2) Divide the DIB'd height with 2 to account for the fact that the
197 // DIB stores both the XOR and the AND masks, one after the other.
198 lpDIB
->bmiHeader
.biHeight
/= 2;
200 // 3) Determine the offset to the XOR bits.
201 // To obtain this value, we have to skip the header, and color table
202 lpXORbits
= (LPSTR
)lpDIB
+ (int )lpDIB
->bmiHeader
.biSize
+
203 (DIBNumColors( (LPSTR
)lpDIB
) * sizeof( RGBQUAD
));
205 // 4) Determine the offset to the AND bits.
206 // To obtain this value, skip the XOR bits
207 lpANDbits
= lpXORbits
+ (int )(lpDIB
->bmiHeader
.biHeight
*
208 (WIDTHBYTES ( lpDIB
->bmiHeader
.biWidth
*
209 lpDIB
->bmiHeader
.biBitCount
)));
211 // Get a hDC so we can create a bitmap compatible with it
212 hDC
= CreateDC( "DISPLAY", NULL
, NULL
, NULL
);
214 // 5) Create a device dependent bitmap with the XOR bits.
215 hbmXor
= CreateDIBitmap( hDC
, (LPBITMAPINFOHEADER
)&(lpDIB
->bmiHeader
),
216 CBM_INIT
, lpXORbits
, lpDIB
, DIB_RGB_COLORS
);
218 GetObject( hbmXor
, sizeof(BITMAP
), (LPSTR
)&bmpXor
);
220 dwBmpSize
= (DWORD
)(bmpXor
.bmWidthBytes
* bmpXor
.bmHeight
* bmpXor
.bmPlanes
);
221 hXorDDB
= GlobalAlloc( GHND
, dwBmpSize
);
224 // clean up before quitting
225 DeleteObject( hbmXor
);
231 #ifdef __WINDOWS_386__
232 lpXorDDB
= (LPSTR
)MK_FP32(GlobalLock( hXorDDB
));
234 lpXorDDB
= (LPSTR
)GlobalLock( hXorDDB
);
237 // 6) Obtain the device dependent XOR bitmask and save in memory.
238 // The AND bitmask is monochrome. Monochrome bits are identical
239 // in both the device dependent bitmaps and device independent
240 // bitmaps. So, no need to convert the AND bitmask.
241 GetBitmapBits( hbmXor
, dwBmpSize
, lpXorDDB
);
243 // 7) Since a DIB is stored upside down, flip the monochrome AND bits by scanlines.
244 k
= (int )lpDIB
->bmiHeader
.biHeight
;
245 for( j
= 0 ; j
< k
; j
++, lpANDbits
+= sizeof(DWORD
))
246 szFlip
[(k
- 1) - j
] = *(DWORD FAR
*)lpANDbits
;
248 // 8) Use the XOR and AND bits and create an icon with CreateIcon.
249 hIcon
= CreateIcon( hInst
, bmpXor
.bmWidth
, bmpXor
.bmHeight
, bmpXor
.bmPlanes
,
250 bmpXor
.bmBitsPixel
, (const BYTE
*)szFlip
, (const BYTE
*)lpXorDDB
);
252 // Clean up before exiting.
253 DeleteObject( hbmXor
);
254 GlobalUnlock( hXorDDB
);
255 GlobalFree( hXorDDB
);
262 // **************************************************************************
264 //*****************************************************************************
265 //* Function : ReadCursorFile() *
266 //* Purpose : Reads a cursor resource file and creates a cursor based on that*
268 //* Parameters : char *szFileName - The cursor resource file. *
269 //* Returns : A handle to a cursor. The handle will be NULL if a cursor can't*
270 //* be created for any reason. *
271 //*****************************************************************************
273 HCURSOR
ReadCursorFile( char *szFileName
, HINSTANCE hInst
, int *W
, int *H
,
274 int *XHot
, int *YHot
)
275 { HANDLE hDIB
; // Handle to DIB memory
279 // read cur DIB from file
280 if( (hDIB
= ReadCur( szFileName
, (LPPOINT
)&ptHotSpot
, W
, H
)) == NULL
)
282 hCursor
= MakeCursor( hDIB
, (LPPOINT
)&ptHotSpot
, hInst
);//create cur from DIB
291 //*****************************************************************************
292 //* Function : IconToCursor() *
293 //* Purpose : Reads an icon resource file and creates a cursor based on that *
295 //* Parameters : char *szFileName - The icon resource file. *
296 //* Returns : A handle to a cursor. The handle will be NULL if a cursor can't*
297 //* be created for any reason. *
298 //* Comments : An icon may be in color. So, the DIB has to be forced to be *
300 //*****************************************************************************
302 HCURSOR
IconToCursor( char *szFileName
, HINSTANCE hInst
, int XHot
, int YHot
,
308 if( (hDIB
= ReadIcon( szFileName
, W
, H
)) == NULL
)
309 //read icon file to get icon DIB
311 // Set the hot spot of the cursor
314 hCursor
= MakeCursor( hDIB
, (LPPOINT
)&ptHotSpot
, hInst
);
315 //create cursor from DIB
320 //*****************************************************************************
321 //* Function : ReadCur() *
322 //* Purpose : Reads a cursor resource file and extracts the DIB information. *
323 //* Parameters : LPSTR szFileName - The cursor resource file. *
324 //* Returns : A handle to a DIB. The handle will be NULL if the resource file*
325 //* is corrupt or if memory cannot be allocated for the DIB info. *
326 //*****************************************************************************
328 HANDLE
ReadCur( char *szFileName
, LPPOINT lpptHotSpot
, int *W
, int *H
)
329 { CURFILEHEADER curFileHead
; // CURSOR file header structure
330 CURFILERES curFileRes
; // CURSOR file resource
333 cbBits
; // Used for reading in file
334 LPBITMAPINFO lpDIB
; // Pointer to DIB memory
335 int hFile
; // Handle to File
337 int nWidth
= GetSystemMetrics( SM_CXCURSOR
),
338 nHeight
= GetSystemMetrics( SM_CYCURSOR
),
341 // Open and read the .ICO file header and the first ICONFILERES
342 hFile
= _lopen( szFileName
, OF_READ
);
343 cbHead
= _lread( hFile
, (LPSTR
)&curFileHead
, sizeof( CURFILEHEADER
));
344 cbRes
= _lread( hFile
, (LPSTR
)&curFileRes
, sizeof( CURFILERES
));
347 if((cbHead
!= sizeof( CURFILEHEADER
)) || (cbRes
!= sizeof( CURFILERES
)))
350 // Verify that it's an .CUR file
351 if ((curFileRes
.bReserved1
!= 0) || (curFileHead
.wResourceType
!= 2))
354 // following added by P.S.
355 while( (nDirEntries
< curFileHead
.wResourceCount
) &&
356 ((curFileRes
.bWidth
!= nWidth
) || (curFileRes
.bHeight
!= nHeight
)))
358 cbRes
= _lread( hFile
, (LPSTR
)&curFileRes
, sizeof( CURFILERES
));
359 if(cbRes
!= sizeof( CURFILERES
))
365 *W
= curFileRes
.bWidth
;
367 *H
= curFileRes
.bHeight
;
370 // Allocate & lock memory to read in the DIB
371 hDIB
= GlobalAlloc(GHND
, curFileRes
.dwDIBSize
);
375 #ifdef __WINDOWS_386__
376 lpDIB
= (LPBITMAPINFO
)MK_FP32(GlobalLock(hDIB
));
378 lpDIB
= (LPBITMAPINFO
)GlobalLock(hDIB
);
381 // Now read the DIB portion of the file, which follows the
382 // end of icon resource table
383 _llseek( hFile
, curFileRes
.dwDIBOffset
, 0);
384 cbBits
= _lread( hFile
, (LPSTR
)lpDIB
, (WORD
)curFileRes
.dwDIBSize
);
389 if((DWORD
)cbBits
!= curFileRes
.dwDIBSize
)
395 if(lpptHotSpot
!= NULL
) // If it is necessary to know the hot spot
397 lpptHotSpot
->x
= (int )curFileRes
.wXHotspot
;
398 lpptHotSpot
->y
= (int )curFileRes
.wYHotspot
;
404 //*****************************************************************************
405 //* Function : ColorDDBToMonoDDB() *
406 //* Purpose : Converts a color bitmap to a monochrome bitmap. *
407 //* Parameters : HBITMAP hbm - The color bitmap. *
408 //* Returns : A handle to a monochrome bitmap. *
409 //*****************************************************************************
410 HBITMAP
ColorDDBToMonoDDB ( HBITMAP hbm
)
413 LPBITMAPINFOHEADER lpbi
;
420 GetObject( hbm
, sizeof( bm
), (LPSTR
)&bm
);
422 bi
.biSize
= sizeof( BITMAPINFOHEADER
); // size of this structure
423 bi
.biWidth
= bm
.bmWidth
; // bitmap width in pixels
424 bi
.biHeight
= bm
.bmHeight
; // bitmap height in pixels
425 bi
.biPlanes
= 1; // # of planes always 1 for DIBs
426 bi
.biBitCount
= bm
.bmPlanes
* bm
.bmBitsPixel
; // color bits per pixel
427 bi
.biCompression
= BI_RGB
; // no compression
428 bi
.biSizeImage
= 0; // 0 means default size
429 bi
.biXPelsPerMeter
= 0; // not used
430 bi
.biYPelsPerMeter
= 0; // not used
431 bi
.biClrUsed
= 0; // 0 means default colors
432 bi
.biClrImportant
= 0; // 0 means defaults
434 dwLen
= bi
.biSize
+ PaletteSize((LPSTR
)&bi
);
438 hdib
= GlobalAlloc( GHND
, dwLen
);
441 ReleaseDC( NULL
, hdc
);
445 #ifdef __WINDOWS_386__
446 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock( hdib
));
448 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock( hdib
);
453 // Call GetDIBits with a NULL lpBits parameter; it will calculate
454 // the biSizeImage field.
455 GetDIBits( hdc
, hbm
, 0, (WORD
)bi
.biHeight
,
456 NULL
, (LPBITMAPINFO
)lpbi
, DIB_RGB_COLORS
);
461 // If the driver did not fill in the biSizeImage field, make one up.
462 if(bi
.biSizeImage
== 0)
463 bi
.biSizeImage
= WIDTHBYTES( (DWORD
)bm
.bmWidth
* bi
.biBitCount
) * bm
.bmHeight
;
465 // Reallocate the buffer big enough to hold all the bits.
466 dwLen
= bi
.biSize
+ PaletteSize((LPSTR
)&bi
) + bi
.biSizeImage
;
467 if( (h
= GlobalReAlloc( hdib
, dwLen
, 0)) != 0)
472 ReleaseDC( NULL
, hdc
);
476 // Call GetDIBits with a NON-NULL lpBits parameter, to actually
477 // get the bits this time.
479 #ifdef __WINDOWS_386__
480 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock( hdib
));
482 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock( hdib
);
485 if( GetDIBits( hdc
, hbm
, 0, (WORD
)bi
.biHeight
,
486 (LPSTR
)lpbi
+ (WORD
)lpbi
->biSize
+ PaletteSize((LPSTR
)lpbi
),
487 (LPBITMAPINFO
)lpbi
, DIB_RGB_COLORS
) == 0)
491 ReleaseDC( NULL
, hdc
);
495 // Finally, create a monochrome DDB, and put the DIB into
496 // it. SetDIBits does smart color conversion.
497 hbmMono
= CreateBitmap((WORD
)lpbi
->biWidth
, (WORD
)lpbi
->biHeight
, 1, 1, NULL
);
498 SetDIBits( hdc
, hbmMono
, (WORD
)0, (WORD
)lpbi
->biHeight
,
499 (LPSTR
)lpbi
+ (int )lpbi
->biSize
+ PaletteSize((LPSTR
)lpbi
),
500 (LPBITMAPINFO
)lpbi
, DIB_RGB_COLORS
);
506 ReleaseDC(NULL
, hdc
);
510 //*****************************************************************************
511 //* Function : MakeCursor() *
512 //* Purpose : Creates a cursor based on the DIB info. returned by ReadCursor.*
513 //* Parameters : HANDLE hDIB - A handle to the cursor's DIB information. *
514 //* LPPOINT lppt - A pointer to a point struct. indicating the *
515 //* location of the Cursor's hot spot. *
516 //* Returns : A handle to a cursor. NULL is returned if a cursor cannot be *
517 //* successfully created. *
518 //* Comments : The steps involved in making a cursor from a DIB are very *
519 //* similar to those involved in making an icon from a DIB. *
520 //* Steps : 1) Obtain a pointer to the Cursor's DIB bits. *
521 //* 2) Divide the DIB's height with 2 to account for the fact that the*
522 //* DIB stores both the XOR and the AND masks, one after the other.*
523 //* 3) Determine the offset to the XOR bits. *
524 //* 4) Determine the offset to the AND bits. *
525 //* 5) Create a device dependent bitmap with the XOR bits. *
526 //* 6) Obtain the device dependent XOR bitmask and save in memory. *
527 //* The AND bitmask is monochrome. Monochrome bits are identical *
528 //* in both the device dependent bitmaps and device independent *
529 //* bitmaps. So, no need to convert the AND bitmask. *
530 //* 7) Since a DIB is stored upside down, flip the monochrome AND bits*
532 //* 8) Use the XOR and AND bits and create a cursor with CreateCursor.*
533 //*****************************************************************************
535 HCURSOR
MakeCursor( HANDLE hDIB
, LPPOINT lpptHotSpot
, HINSTANCE hInst
)
537 lpANDbits
; // Pointer to XOR and AND bits
538 HBITMAP hbmXor
; // handle to XOR bitmap
539 BITMAP bmpXor
; // Used to manipulate XOR bitmap
540 DWORD dwBmpSize
; // Size of XOR bitmap
550 // 1) Obtain a pointer to the Cursor's DIB bits.
551 #ifdef __WINDOWS_386__
552 lpDIB
= (LPBITMAPINFO
)MK_FP32(GlobalLock( hDIB
));
554 lpDIB
= (LPBITMAPINFO
)GlobalLock( hDIB
);
557 // 2) Divide the DIB's height with 2 to account for the fact that the
558 // DIB stores both the XOR and the AND masks, one after the other.
559 lpDIB
->bmiHeader
.biHeight
/= 2;
561 // 3) Determine the offset to the XOR bits.
562 // To obtain this value, we have to skip the header, and color table
563 lpXORbits
= (LPSTR
)lpDIB
+ (int )lpDIB
->bmiHeader
.biSize
+
564 (DIBNumColors((LPSTR
)lpDIB
) * sizeof(RGBQUAD
));
566 // 4) Determine the offset to the AND bits
567 // To obtain this value, skip the XOR bits
568 lpANDbits
= lpXORbits
+ (int )( lpDIB
->bmiHeader
.biHeight
*
569 (WIDTHBYTES( lpDIB
->bmiHeader
.biWidth
*
570 lpDIB
->bmiHeader
.biBitCount
)));
572 // Get a hDC so we can create a bitmap compatible with it
573 hDC
= CreateDC( "DISPLAY", NULL
, NULL
, NULL
);
575 // 5) Create a device dependent bitmap with the XOR bits.
576 hbmXor
= CreateBitmap( (int )lpDIB
->bmiHeader
.biWidth
,
577 (int )lpDIB
->bmiHeader
.biHeight
, 1, 1, NULL
);
578 SetDIBits( hDC
, hbmXor
, 0, (WORD
)lpDIB
->bmiHeader
.biHeight
, lpXORbits
,
579 lpDIB
, DIB_RGB_COLORS
);
580 GetObject( hbmXor
, sizeof( BITMAP
), (LPSTR
)&bmpXor
);
582 dwBmpSize
= (DWORD
)(bmpXor
.bmWidthBytes
* bmpXor
.bmHeight
* bmpXor
.bmPlanes
);
583 hXorDDB
= GlobalAlloc( GHND
, dwBmpSize
);
585 { // clean up before quitting
586 DeleteObject( hbmXor
);
591 #ifdef __WINDOWS_386__
592 lpXorDDB
= (LPSTR
)MK_FP32(GlobalLock( hXorDDB
));
594 lpXorDDB
= (LPSTR
)GlobalLock( hXorDDB
);
597 // 6) Obtain the device dependent XOR bitmask and save in memory.
598 // The AND bitmask is monochrome. Monochrome bits are identical
599 // in both the device dependent bitmaps and device independent
600 // bitmaps. So, no need to convert the AND bitmask.
601 GetBitmapBits( hbmXor
, dwBmpSize
, lpXorDDB
);
603 // 7) Since a DIB is stored upside down, flip the monochrome AND bits by scanlines.
604 k
= (int)lpDIB
->bmiHeader
.biHeight
;
605 for( j
= 0 ; j
< k
; j
++, lpANDbits
+= sizeof( DWORD
))
606 szFlip
[(k
- 1) - j
] = *(DWORD FAR
*)lpANDbits
;
608 // 8) Use the XOR and AND bits and create a cursor with CreateCursor.
609 hCursor
= CreateCursor( hInst
, lpptHotSpot
->x
, lpptHotSpot
->y
,
610 bmpXor
.bmWidth
, bmpXor
.bmHeight
, (LPSTR
)szFlip
, lpXorDDB
);
612 // Clean up before exiting.
613 DeleteObject( hbmXor
);
614 GlobalUnlock( hXorDDB
);
615 GlobalFree( hXorDDB
);
622 //*****************************************************************************
623 //* Function : PaletteSize() *
624 //* Purpose : Calculates the palette size in bytes. If the info. block is of *
625 //* the BITMAPCOREHEADER type, the number of colors is multiplied *
626 //* by sizeof(RGBTRIPLE) to give the palette size, otherwise the *
627 //* number of colors is multiplied by sizeof(RGBQUAD). *
628 //* Parameters : LPSTR pv - pointer to the BITMAPINFOHEADER *
629 //* Returns : The size of the palette. *
630 //*****************************************************************************
632 WORD
PaletteSize( LPSTR pv
)
633 { LPBITMAPINFOHEADER lpbi
;
636 lpbi
= (LPBITMAPINFOHEADER
)pv
;
637 NumColors
= DIBNumColors((LPSTR
)lpbi
);
639 if(lpbi
->biSize
== sizeof( BITMAPCOREHEADER
)) // OS/2 style DIBs
640 return NumColors
* sizeof( RGBTRIPLE
);
642 return NumColors
* sizeof( RGBQUAD
);
645 //*****************************************************************************
646 //* Function : DIBNumColors() *
647 //* Purpose : This function calculates the number of colors in the DIB's *
648 //* color table by finding the bits per pixel for the DIB (whether *
649 //* Win3.0 or OS/2-style DIB). If bits per pixel is 1: colors=2, *
650 //* if 4: colors=16, if 8: colors=256, if 24, no colors in color *
652 //* Parameters : LPSTR lpbi - pointer to packed-DIB memory block. *
653 //* Returns : The number of colors in the color table. *
654 //*****************************************************************************
656 WORD
DIBNumColors ( LPSTR pv
)
658 BITMAPINFOHEADER
*lpbi
;
659 BITMAPCOREHEADER
*lpbc
;
661 lpbi
= ((BITMAPINFOHEADER
* )pv
); // assume win 3.0 style DIBs
662 lpbc
= ((BITMAPCOREHEADER
* )pv
); // assume OS/2 style DIBs
664 // With the BITMAPINFO format headers, the size of the palette
665 // is in biClrUsed, whereas in the BITMAPCORE - style headers, it
666 // is dependent on the bits per pixel ( = 2 raised to the power of
669 if(lpbi
->biSize
!= sizeof( BITMAPCOREHEADER
))
671 if(lpbi
->biClrUsed
!= 0)
672 return (WORD
)lpbi
->biClrUsed
;
673 bits
= lpbi
->biBitCount
;
676 bits
= lpbc
->bcBitCount
;
687 // A 24 bitcount DIB has no color table
693 // ******************************************************************
694 BOOL
fGetXPixmap( BOOL fIsIcon
, char *szFileName
, HINSTANCE hInst
,
695 char cData
[], int &width
, int &height
)
708 HCURSOR hIconOrCursor
= fIsIcon
?
709 IconToCursor( szFileName
, hInst
, 0, 0, &w
, &h
)
710 : ReadCursorFile( szFileName
, hInst
, &w
, &h
, 0, 0);
713 if(hIconOrCursor
== 0)
716 hdc
= GetDC( GetDesktopWindow());
717 hdcMemory
= CreateCompatibleDC( hdc
);
718 hbmp
= CreateCompatibleBitmap( hdc
, w
, h
);
719 holdbmp
= SelectObject( hdcMemory
, hbmp
);
720 PatBlt( hdcMemory
, 0, 0, w
, h
, BLACKNESS
); // or use WHITENESS??
721 DrawIcon( hdcMemory
, 0, 0, hIconOrCursor
); //using HCURSOR with DrawIcon is OK
723 // the data retrieval follows:
726 for( j
= 0, s
= (BYTE
*)cData
; j
< h
; ++j
)
727 for( i
= 0 ; i
< w
; ++i
, cMask
>>= 1)
734 rgb
= GetPixel( hdcMemory
, i
, j
);
735 sum
= (int )(rgb
& 0xFFL
);
736 sum
+= (int )((rgb
& 0xFF00L
) >> 8);
737 sum
+= (int )((rgb
& 0xFF0000L
) >> 16);
739 cByte
= cByte
| cMask
;
746 SelectObject( hdcMemory
, holdbmp
);
747 DeleteDC( hdcMemory
);
748 ReleaseDC( GetDesktopWindow(), hdc
);
749 DestroyCursor( hIconOrCursor
);
755 // Added from scavenged internet code, JACS 23/6/95
756 HCURSOR
MakeCursorFromBitmap(HINSTANCE hInst
, HBITMAP hBitmap
, POINT
*pPoint
)
758 HDC hDCColor
, hDCMono
;
770 hDCColor
= CreateCompatibleDC(hDC
);
771 hDCMono
= CreateCompatibleDC(hDC
);
772 hAndBmp
= CreateCompatibleBitmap(hDCMono
, 32, 32);
773 hXorBmp
= CreateCompatibleBitmap(hDCMono
, 32, 32);
775 hBmpOld
= SelectObject(hDCColor
, hBitmap
);
776 SelectObject(hDCMono
, hAndBmp
);
777 SetBkColor(hDCColor
, RGB(191, 191, 191));
779 BitBlt(hDCMono
, 0, 0, 32, 32, hDCColor
, 0, 0, SRCCOPY
);
781 // Now we have the AND Mask
783 GetObject(hAndBmp
, sizeof(BITMAP
), (LPSTR
) &bm
);
784 dwBytes
= (bm
.bmWidthBytes
* bm
.bmHeight
);
785 andBits
= (NPSTR
) LocalAlloc(LPTR
, dwBytes
);
786 GetBitmapBits(hAndBmp
, dwBytes
, andBits
);
788 SelectObject(hDCMono
, hXorBmp
);
789 SetBkColor(hDCColor
, RGB(0, 0, 0));
791 BitBlt(hDCMono
, 0, 0, 32, 32, hDCColor
, 0, 0, SRCCOPY
);
793 // Now we have the XOR Mask
795 GetObject(hXorBmp
, sizeof(BITMAP
), (LPSTR
) &bm
);
796 dwBytes
= (bm
.bmWidthBytes
* bm
.bmHeight
);
797 xorBits
= (NPSTR
) LocalAlloc(LPTR
, dwBytes
);
798 GetBitmapBits(hXorBmp
, dwBytes
, xorBits
);
805 hNewCursor
= CreateCursor(hInst
,
806 pPoint
->x
, pPoint
->y
, 32, 32, andBits
, xorBits
);
808 SelectObject(hDCColor
, hBmpOld
);
809 SelectObject(hDCMono
, hBmpOld
);
812 DeleteObject(hAndBmp
);
813 DeleteObject(hXorBmp
);
814 ReleaseDC(NULL
, hDC
);
816 LocalUnlock(LocalHandle((WORD
) andBits
));
817 LocalUnlock(LocalHandle((WORD
) xorBits
));
818 LocalFree(LocalHandle((WORD
) andBits
));
819 LocalFree(LocalHandle((WORD
) xorBits
));
821 LocalUnlock(LocalHandle((LPCVOID
) andBits
));
822 LocalUnlock(LocalHandle((LPCVOID
) xorBits
));
823 LocalFree(LocalHandle((LPCVOID
) andBits
));
824 LocalFree(LocalHandle((LPCVOID
) xorBits
));
830 * This doesn't work: just gives us a grey square. Ideas, anyone?
833 HICON
MakeIconFromBitmap(HINSTANCE hInst
, HBITMAP hBitmap
)
835 HDC hDCColor
, hDCMono
;
847 hDCColor
= CreateCompatibleDC(hDC
);
848 hDCMono
= CreateCompatibleDC(hDC
);
849 hAndBmp
= CreateCompatibleBitmap(hDCMono
, 32, 32);
850 hXorBmp
= CreateCompatibleBitmap(hDCMono
, 32, 32);
852 hBmpOld
= SelectObject(hDCColor
, hBitmap
);
853 SelectObject(hDCMono
, hAndBmp
);
854 SetBkColor(hDCColor
, RGB(191, 191, 191));
856 BitBlt(hDCMono
, 0, 0, 32, 32, hDCColor
, 0, 0, SRCCOPY
);
858 // Now we have the AND Mask
860 GetObject(hAndBmp
, sizeof(BITMAP
), (LPSTR
) &bm
);
861 dwBytes
= (bm
.bmWidthBytes
* bm
.bmHeight
);
862 andBits
= (NPSTR
) LocalAlloc(LPTR
, dwBytes
);
863 GetBitmapBits(hAndBmp
, dwBytes
, andBits
);
865 SelectObject(hDCMono
, hXorBmp
);
866 SetBkColor(hDCColor
, RGB(0, 0, 0));
868 BitBlt(hDCMono
, 0, 0, 32, 32, hDCColor
, 0, 0, SRCCOPY
);
870 // Now we have the XOR Mask
872 GetObject(hXorBmp
, sizeof(BITMAP
), (LPSTR
) &bm
);
873 dwBytes
= (bm
.bmWidthBytes
* bm
.bmHeight
);
874 xorBits
= (NPSTR
) LocalAlloc(LPTR
, dwBytes
);
875 GetBitmapBits(hXorBmp
, dwBytes
, xorBits
);
877 hNewIcon
= CreateIcon(hInst
, 1, 4, 32, 32, (unsigned char *)andBits
, (unsigned char *)xorBits
);
879 SelectObject(hDCColor
, hBmpOld
);
880 SelectObject(hDCMono
, hBmpOld
);
883 DeleteObject(hAndBmp
);
884 DeleteObject(hXorBmp
);
885 ReleaseDC(NULL
, hDC
);
887 LocalUnlock(LocalHandle((WORD
) andBits
));
888 LocalUnlock(LocalHandle((WORD
) xorBits
));
889 LocalFree(LocalHandle((WORD
) andBits
));
890 LocalFree(LocalHandle((WORD
) xorBits
));
892 LocalUnlock(LocalHandle((LPCVOID
) andBits
));
893 LocalUnlock(LocalHandle((LPCVOID
) xorBits
));
894 LocalFree(LocalHandle((LPCVOID
) andBits
));
895 LocalFree(LocalHandle((LPCVOID
) xorBits
));