home *** CD-ROM | disk | FTP | other *** search
- #define INCL_DOS
- #define INCL_PM
- #include <os2.h>
- #include <string.h>
- #include <stdlib.h>
- #include "bmload.ext"
- #include "bmload.ids"
- #include "bmload.h"
-
- /* this is a local function to obtain a bitmap from a file contents */
- static HBITMAP getBitmapHandle(char *bitmap) {
- BITMAPARRAYFILEHEADER2 *bafh2;
- BITMAPFILEHEADER2 *bfh2;
- BITMAPINFOHEADER2 bih2;
- HBITMAP hbm;
- HPS hps;
- /* 1. check if the file format is a bitmap array or just one bitmap */
- bafh2 = (BITMAPARRAYFILEHEADER2 *)bitmap;
- if(bafh2->usType == *(USHORT*)"BA") {
- bfh2 = &bafh2->bfh2;
- } else {
- bfh2 = (BITMAPFILEHEADER2 *)bitmap;
- }
- /* 2. check if the bitmap section has a bitmap */
- if(bfh2->usType != *(USHORT*)"BM") {
- return NULLHANDLE;
- }
- memcpy(&bih2,&bfh2->bmp2,sizeof(bih2));
- /* 3. create a bitmap handle */
- hps = WinGetPS(HWND_DESKTOP);
- hbm = GpiCreateBitmap(hps,&bih2, CBM_INIT,
- bitmap+bfh2->offBits,(BITMAPINFO2*)&bfh2->bmp2);
- WinReleasePS(hps);
- return hbm;
- }
-
- /* This is a USER FUNCTION to be attached to an anchor point(like a button)*/
- void UFLoadBitmap(PGPFPARMS pGpfParms) {
- FILESTATUS3 fStat;
- char * bitmap;
- HBITMAP hbm;
- HFILE file;
- ULONG action;
- APIRET rc;
- ULONG cnt;
- static FILEDLG dlg;
- HWND staticCtrl = WinWindowFromID(pGpfParms->hwnd,ID_MAIN_ST_BITMAP);
- HWND frame = GpfGetHwndFrame(pGpfParms->hwnd);
- /* 1. find a new bitmap file name */
- memset(&dlg,0,sizeof(FILEDLG));
- dlg.cbSize = sizeof(FILEDLG);
- dlg.fl = FDS_CENTER|FDS_OPEN_DIALOG;
- dlg.pszTitle = "Open New .BMP File";
- strcpy(dlg.szFullFile,"*.BMP");
- if(!WinFileDlg(HWND_DESKTOP,frame,&dlg)|| dlg.lReturn !=DID_OK)
- return;
- /* 2. open a bitmap file */
- rc = DosOpen(dlg.szFullFile,&file,&action,0L,FILE_ARCHIVED,
- OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
- OPEN_SHARE_DENYREADWRITE|OPEN_ACCESS_READONLY,0L);
- if(rc) return;
- /* 3. detect the file length and allocate memory */
- rc = DosQueryFileInfo(file,1,&fStat,sizeof(fStat));
- bitmap = malloc( fStat.cbFile);
- if(bitmap == NULL) {
- rc = DosClose(file);
- return;
- }
- /* 4. read the bitmap file to the memory */
- rc = DosRead(file,bitmap,fStat.cbFile,&cnt);
- rc = DosClose(file);
- /* 5. obtain a bitmap handle */
- hbm = getBitmapHandle(bitmap);
- free(bitmap);
- /* 6. set the bitmap to the static control */
- if(hbm)WinSendMsg(staticCtrl,SM_SETHANDLE,MPFROMLONG(hbm),(MPARAM)0);
- }
-