home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / graphic / bmload / bmloaduf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  2.5 KB  |  78 lines

  1. #define INCL_DOS
  2. #define INCL_PM
  3. #include <os2.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "bmload.ext"
  7. #include "bmload.ids"
  8. #include "bmload.h"
  9.  
  10. /* this is a local function to obtain a bitmap from a file contents */
  11. static HBITMAP getBitmapHandle(char *bitmap) {
  12.    BITMAPARRAYFILEHEADER2 *bafh2;
  13.    BITMAPFILEHEADER2 *bfh2;
  14.    BITMAPINFOHEADER2 bih2;
  15.    HBITMAP hbm;
  16.    HPS hps;
  17. /* 1. check if the file format is a bitmap array or just one bitmap */
  18.    bafh2 = (BITMAPARRAYFILEHEADER2 *)bitmap;
  19.    if(bafh2->usType == *(USHORT*)"BA") {
  20.       bfh2 = &bafh2->bfh2;
  21.    } else {
  22.       bfh2 = (BITMAPFILEHEADER2 *)bitmap;
  23.    }
  24. /* 2. check if the bitmap section has a bitmap */
  25.    if(bfh2->usType != *(USHORT*)"BM") {
  26.       return NULLHANDLE;
  27.    }
  28.    memcpy(&bih2,&bfh2->bmp2,sizeof(bih2));
  29. /* 3. create a bitmap handle */
  30.    hps = WinGetPS(HWND_DESKTOP);
  31.    hbm = GpiCreateBitmap(hps,&bih2, CBM_INIT,
  32.              bitmap+bfh2->offBits,(BITMAPINFO2*)&bfh2->bmp2);
  33.    WinReleasePS(hps);
  34.    return hbm;
  35. }
  36.  
  37. /* This is a USER FUNCTION to be attached to an anchor point(like a button)*/
  38. void UFLoadBitmap(PGPFPARMS pGpfParms) {
  39.    FILESTATUS3 fStat;
  40.    char * bitmap;
  41.    HBITMAP hbm;
  42.    HFILE file;
  43.    ULONG action;
  44.    APIRET rc;
  45.    ULONG cnt;
  46.    static FILEDLG dlg;
  47.    HWND staticCtrl = WinWindowFromID(pGpfParms->hwnd,ID_MAIN_ST_BITMAP);
  48.    HWND frame = GpfGetHwndFrame(pGpfParms->hwnd);
  49. /* 1. find a new bitmap file name */
  50.    memset(&dlg,0,sizeof(FILEDLG));
  51.    dlg.cbSize = sizeof(FILEDLG);
  52.    dlg.fl = FDS_CENTER|FDS_OPEN_DIALOG;
  53.    dlg.pszTitle = "Open New .BMP File";
  54.    strcpy(dlg.szFullFile,"*.BMP");
  55.    if(!WinFileDlg(HWND_DESKTOP,frame,&dlg)|| dlg.lReturn !=DID_OK)
  56.       return;
  57. /* 2. open a bitmap file */
  58.    rc = DosOpen(dlg.szFullFile,&file,&action,0L,FILE_ARCHIVED,
  59.         OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
  60.         OPEN_SHARE_DENYREADWRITE|OPEN_ACCESS_READONLY,0L);
  61.    if(rc) return;
  62. /* 3. detect the file length and allocate memory */
  63.    rc = DosQueryFileInfo(file,1,&fStat,sizeof(fStat));
  64.    bitmap = malloc( fStat.cbFile);
  65.    if(bitmap == NULL) {
  66.       rc = DosClose(file);
  67.       return;
  68.    }
  69. /* 4. read the bitmap file to the memory */
  70.    rc = DosRead(file,bitmap,fStat.cbFile,&cnt);
  71.    rc = DosClose(file);
  72. /* 5. obtain a bitmap handle */
  73.    hbm = getBitmapHandle(bitmap);
  74.    free(bitmap);
  75. /* 6. set the bitmap to the static control */
  76.    if(hbm)WinSendMsg(staticCtrl,SM_SETHANDLE,MPFROMLONG(hbm),(MPARAM)0);
  77. }
  78.