home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_info / technote.arj / DIBIT / DIBIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-21  |  26.2 KB  |  812 lines

  1. /**************************************************************************** 
  2.     PROGRAM: dibit.c
  3.  
  4.     PURPOSE: dibit template for Windows applications
  5.  
  6.     FUNCTIONS:
  7.  
  8.     WinMain() - calls initialization function, processes message loop
  9.     InitApplication() - initializes window data and registers window
  10.     InitInstance() - saves instance handle and creates main window
  11.     MainWndProc() - processes messages
  12.     About() - processes messages for "About" dialog box
  13.  
  14.     COMMENTS:
  15.  
  16.         Windows can have several copies of your application running at the
  17.         same time.  The variable hInst keeps track of which instance this
  18.         application is so that processing will be to the correct window.
  19.  
  20. ****************************************************************************/
  21.  
  22. #include "windows.h"            /* required for all Windows applications */
  23. #include "dibit.h"            /* specific to this program             */
  24. #include "commdlg.h"
  25.  
  26. HANDLE hInst;                /* current instance                 */
  27.  
  28. char achFileName[128];
  29. char str[255];
  30.  
  31. WORD wOperation = IDM_SETDIB;    /* default to SetDIBits() */
  32. BOOL bDIBLoaded = FALSE;    /* initially no DIB is loaded */
  33. WORD offBits;            /* offset to the bits */
  34. HANDLE hDIBInfo = NULL;        /* the DIB header */
  35. HBITMAP hDDBitmap = NULL;    /* a device dependent copy of the DIB */
  36. HBITMAP hOldBitmap;
  37. HDC hMemDC;
  38.  
  39. WORD wPalOp = 0;        /* default to no palette stuff */
  40. HPALETTE hPalette = NULL;    /* palette used for display */
  41. HANDLE hPalHeader = NULL;    /* DIB header with indices for color table */
  42.  
  43. void PASCAL NEAR PaintDIB(HWND);
  44. DWORD PASCAL lread (int, VOID far *, DWORD);
  45. HPALETTE PASCAL NEAR MakeDIBPalette(LPBITMAPINFOHEADER);
  46. HANDLE PASCAL NEAR MakeIndexHeader(LPBITMAPINFOHEADER);
  47. int InitDIB(HWND);
  48.  
  49. /****************************************************************************
  50.  
  51.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  52.  
  53.     PURPOSE: calls initialization function, processes message loop
  54.  
  55.     COMMENTS:
  56.  
  57.         Windows recognizes this function by name as the initial entry point 
  58.         for the program.  This function calls the application initialization 
  59.         routine, if no other instance of the program is running, and always 
  60.         calls the instance initialization routine.  It then executes a message 
  61.         retrieval and dispatch loop that is the top-level control structure 
  62.         for the remainder of execution.  The loop is terminated when a WM_QUIT 
  63.         message is received, at which time this function exits the application 
  64.         instance by returning the value passed by PostQuitMessage(). 
  65.  
  66.         If this function must abort before entering the message loop, it 
  67.         returns the conventional value NULL.  
  68.  
  69. ****************************************************************************/
  70.  
  71. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  72. HANDLE hInstance;                 /* current instance         */
  73. HANDLE hPrevInstance;                 /* previous instance         */
  74. LPSTR lpCmdLine;                 /* command line             */
  75. int nCmdShow;                     /* show-window type (open/icon) */
  76. {
  77.     MSG msg;                     /* message                 */
  78.  
  79.     if (!hPrevInstance)             /* Other instances of app running? */
  80.     if (!InitApplication(hInstance)) /* Initialize shared things */
  81.         return (FALSE);         /* Exits if unable to initialize     */
  82.  
  83.     /* Perform initializations that apply to a specific instance */
  84.  
  85.     if (!InitInstance(hInstance, nCmdShow))
  86.         return (FALSE);
  87.  
  88.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  89.  
  90.     while (GetMessage(&msg,       /* message structure                 */
  91.         NULL,           /* handle of window receiving the message */
  92.         NULL,           /* lowest message to examine             */
  93.         NULL))           /* highest message to examine         */
  94.     {
  95.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  96.     DispatchMessage(&msg);       /* Dispatches message to window         */
  97.     }
  98.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  99. }
  100.  
  101.  
  102. /****************************************************************************
  103.  
  104.     FUNCTION: InitApplication(HANDLE)
  105.  
  106.     PURPOSE: Initializes window data and registers window class
  107.  
  108.     COMMENTS:
  109.  
  110.         This function is called at initialization time only if no other 
  111.         instances of the application are running.  This function performs 
  112.         initialization tasks that can be done once for any number of running 
  113.         instances.  
  114.  
  115.         In this case, we initialize a window class by filling out a data 
  116.         structure of type WNDCLASS and calling the Windows RegisterClass() 
  117.         function.  Since all instances of this application use the same window 
  118.         class, we only need to do this when the first instance is initialized.  
  119.  
  120.  
  121. ****************************************************************************/
  122.  
  123. BOOL InitApplication(hInstance)
  124. HANDLE hInstance;                   /* current instance         */
  125. {
  126.     WNDCLASS  wc;
  127.  
  128.     /* Fill in window class structure with parameters that describe the       */
  129.     /* main window.                                                           */
  130.  
  131.     wc.style = NULL;                    /* Class style(s).                    */
  132.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  133.                                         /* windows of this class.             */
  134.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  135.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  136.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  137.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  138.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  139.     wc.hbrBackground = COLOR_WINDOW+1;
  140.     wc.lpszMenuName =  "dibitMenu";   /* Name of menu resource in .RC file. */
  141.     wc.lpszClassName = "dibitWClass"; /* Name used in call to CreateWindow. */
  142.  
  143.     /* Register the window class and return success/failure code. */
  144.  
  145.     return (RegisterClass(&wc));
  146. }
  147.  
  148.  
  149. /****************************************************************************
  150.  
  151.     FUNCTION:  InitInstance(HANDLE, int)
  152.  
  153.     PURPOSE:  Saves instance handle and creates main window
  154.  
  155.     COMMENTS:
  156.  
  157.         This function is called at initialization time for every instance of 
  158.         this application.  This function performs initialization tasks that 
  159.         cannot be shared by multiple instances.  
  160.  
  161.         In this case, we save the instance handle in a static variable and 
  162.         create and display the main program window.  
  163.         
  164. ****************************************************************************/
  165.  
  166. BOOL InitInstance(hInstance, nCmdShow)
  167.     HANDLE          hInstance;          /* Current instance identifier.       */
  168.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  169. {
  170.     HWND            hWnd;               /* Main window handle.                */
  171.  
  172.     /* Save the instance handle in static variable, which will be used in  */
  173.     /* many subsequence calls from this application to Windows.            */
  174.  
  175.     hInst = hInstance;
  176.  
  177.     /* Create a main window for this application instance.  */
  178.  
  179.     hWnd = CreateWindow(
  180.         "dibitWClass",                /* See RegisterClass() call.          */
  181.         "dibit Sample Application",   /* Text for window title bar.         */
  182.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  183.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  184.         CW_USEDEFAULT,                  /* Default vertical position.         */
  185.         CW_USEDEFAULT,                  /* Default width.                     */
  186.         CW_USEDEFAULT,                  /* Default height.                    */
  187.         NULL,                           /* Overlapped windows have no parent. */
  188.         NULL,                           /* Use the window class menu.         */
  189.         hInstance,                      /* This instance owns this window.    */
  190.         NULL                            /* Pointer not needed.                */
  191.     );
  192.  
  193.     /* If window could not be created, return "failure" */
  194.  
  195.     if (!hWnd)
  196.         return (FALSE);
  197.  
  198.     /* Make the window visible; update its client area; and return "success" */
  199.  
  200.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  201.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  202.     return (TRUE);               /* Returns the value from PostQuitMessage */
  203.  
  204. }
  205.  
  206. /****************************************************************************
  207.  
  208.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  209.  
  210.     PURPOSE:  Processes messages
  211.  
  212.     MESSAGES:
  213.  
  214.     WM_COMMAND    - application menu (About dialog box)
  215.     WM_DESTROY    - destroy window
  216.  
  217.     COMMENTS:
  218.  
  219.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  220.     current instance address of the About() function.  Then call Dialog
  221.     box which will create the box according to the information in your
  222.     dibit.rc file and turn control over to the About() function.    When
  223.     it returns, free the intance address.
  224.  
  225. ****************************************************************************/
  226.  
  227. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  228. HWND hWnd;                  /* window handle             */
  229. unsigned message;              /* type of message             */
  230. WORD wParam;                  /* additional information         */
  231. LONG lParam;                  /* additional information         */
  232. {
  233.     FARPROC lpProcAbout;
  234.     OPENFILENAME    ofn;
  235.  
  236.     switch (message) {
  237.     case WM_CREATE:
  238.         hDIBInfo = GlobalAlloc(GMEM_MOVEABLE, 
  239.                 (DWORD)(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)));
  240.         break;
  241.  
  242.     case WM_INITMENU:
  243.         CheckMenuItem(wParam, wOperation, MF_CHECKED);
  244.         break;
  245.  
  246.     case WM_COMMAND:       /* message: command from application menu */
  247.         switch(wParam) {
  248.         case IDM_ABOUT:
  249.             lpProcAbout = MakeProcInstance(About, hInst);
  250.  
  251.             DialogBox(hInst,         /* current instance         */
  252.                 "AboutBox",             /* resource to use         */
  253.                 hWnd,             /* parent handle         */
  254.                 lpProcAbout);         /* About() instance address */
  255.  
  256.             FreeProcInstance(lpProcAbout);
  257.             break;
  258.  
  259.         case IDM_SETDIB:
  260.         case IDM_TODEV:
  261.         case IDM_STRETCH:
  262.             CheckMenuItem(GetMenu(hWnd), wOperation, MF_UNCHECKED);
  263.             wOperation = wParam;
  264.             CheckMenuItem(GetMenu(hWnd), wOperation, MF_CHECKED);
  265.             InvalidateRect(hWnd, NULL, TRUE);
  266.             break;
  267.  
  268.         case IDM_PALRGB:
  269.         case IDM_PALIND:
  270.             if (wPalOp == wParam)    /* turning off palette use */
  271.             {
  272.             CheckMenuItem(GetMenu(hWnd), wPalOp, MF_UNCHECKED);
  273.             wPalOp = 0;
  274.  
  275.             /* since palette use is being turned off, 
  276.             ** any device-dependent bitmap we have created
  277.             ** is no longer valid for use.  free it up
  278.             */
  279.             if (hDDBitmap)
  280.             {
  281.                 SelectObject(hMemDC, hOldBitmap);
  282.                 DeleteDC(hMemDC);
  283.                 DeleteObject(hDDBitmap);
  284.                 hDDBitmap = NULL;
  285.             }
  286.             }
  287.             else    /* changing palette use options */
  288.             {
  289.             if (wPalOp)
  290.                 CheckMenuItem(GetMenu(hWnd), wPalOp, MF_UNCHECKED);
  291.  
  292.             /* turning on palette use for the first time.
  293.             ** if we had a device-dependent bitmap, get rid
  294.             ** of it so that it will be rebuilt with palette.
  295.             */
  296.             else if (hDDBitmap)
  297.             {
  298.                 SelectObject(hMemDC, hOldBitmap);
  299.                 DeleteDC(hMemDC);
  300.                 DeleteObject(hDDBitmap);
  301.                 hDDBitmap = NULL;
  302.             }
  303.             wPalOp = wParam;
  304.             CheckMenuItem(GetMenu(hWnd), wPalOp, MF_CHECKED);
  305.             }
  306.             InvalidateRect(hWnd, NULL, TRUE);
  307.             break;
  308.  
  309.                 case IDM_OPEN:
  310.             ofn.lStructSize = sizeof(OPENFILENAME);
  311.             ofn.hwndOwner = hWnd;
  312.             ofn.lpstrFilter = NULL;
  313.                     ofn.lpstrFilter = "Bitmaps (*.BMP)\0*.BMP\0";
  314.             ofn.lpstrCustomFilter = NULL;
  315.             ofn.nFilterIndex = 1;
  316.             achFileName[0] = 0;        /* pass in NULL */
  317.             ofn.lpstrFile = (LPSTR)achFileName;
  318.             ofn.nMaxFile = 128;
  319.             ofn.lpstrInitialDir = NULL;
  320.             ofn.lpstrTitle = NULL;
  321.             ofn.lpstrFileTitle = NULL;
  322.             ofn.lpstrDefExt = NULL;
  323.                     ofn.Flags = 0;
  324.  
  325.                     if (GetOpenFileName((LPOPENFILENAME)&ofn))
  326.             if (InitDIB (hWnd))
  327.                 InvalidateRect (hWnd, NULL, FALSE);
  328.                     break;
  329.  
  330.         default:
  331.             return (DefWindowProc(hWnd, message, wParam, lParam));
  332.         }
  333.         break;
  334.  
  335.     /* if system palette change caused by someone else, force repaint */
  336.     case WM_PALETTECHANGED:
  337.         if (wParam != hWnd && bDIBLoaded)
  338.         InvalidateRect(hWnd, NULL, TRUE);
  339.         break;
  340.  
  341.     /* if doing stretching, resize the image */
  342.     case WM_SIZE:
  343.         if (wOperation == IDM_STRETCH && bDIBLoaded)
  344.         InvalidateRect(hWnd, NULL, TRUE);
  345.         break;
  346.  
  347.     case WM_PAINT:
  348.         if (!bDIBLoaded)        /* if no DIB loaded, nothing to draw */
  349.         return (DefWindowProc(hWnd, message, wParam, lParam));
  350.         else
  351.         PaintDIB(hWnd);
  352.         break;
  353.  
  354.     case WM_DESTROY:          /* message: window being destroyed */
  355.         PostQuitMessage(0);
  356.         break;
  357.  
  358.     default:              /* Passes it on if unproccessed    */
  359.         return (DefWindowProc(hWnd, message, wParam, lParam));
  360.     }
  361.     return (NULL);
  362. }
  363.  
  364. /****************************************************************************
  365.  *                                        *
  366.  *  FUNCTION   : MakeIndexHeader(lpInfo)                    *
  367.  *                                        *
  368.  *  PURPOSE    : Given a BITMAPINFOHEADER, create a new info header 
  369.  *         using the DIB_PAL_COLORS format.
  370.  *                                        *
  371.  *  RETURNS    : non-zero - global handle of a new header
  372.  *         zero - unable to create new header
  373.  *                                        *
  374.  ****************************************************************************/
  375. HANDLE PASCAL NEAR MakeIndexHeader(LPBITMAPINFOHEADER lpInfo)
  376. {
  377.     HANDLE hPalInfo;
  378.     LPBITMAPINFOHEADER lpPalInfo;
  379.     WORD FAR *lpTable;
  380.     WORD i;
  381.  
  382.     if (lpInfo->biClrUsed)
  383.     {
  384.     hPalInfo = GlobalAlloc(GMEM_MOVEABLE, lpInfo->biSize +
  385.                     lpInfo->biClrUsed * sizeof(WORD));
  386.     if (!hPalInfo)
  387.         return(NULL);
  388.     lpPalInfo = (LPBITMAPINFOHEADER)GlobalLock(hPalInfo);
  389.  
  390.     *lpPalInfo = *lpInfo;
  391.     lpTable = (WORD FAR *)((LPSTR)lpPalInfo + lpPalInfo->biSize);
  392.  
  393.         for (i = 0; i < (WORD)lpInfo->biClrUsed; i++)
  394.         *lpTable++ = i;
  395.  
  396.     GlobalUnlock(hPalInfo);
  397.     return(hPalInfo);
  398.     }
  399.     else
  400.     return(NULL);
  401. }
  402.  
  403. /****************************************************************************
  404.  *                                        *
  405.  *  FUNCTION   : MakeDIBPalette(lpInfo)                        *
  406.  *                                        *
  407.  *  PURPOSE    : Given a BITMAPINFOHEADER, create a palette based on 
  408.  *         the color table.
  409.  *         
  410.  *                                        *
  411.  *  RETURNS    : non-zero - handle of a corresponding palette 
  412.  *         zero - unable to create palette
  413.  *                                        *
  414.  ****************************************************************************/
  415. HPALETTE PASCAL NEAR MakeDIBPalette(LPBITMAPINFOHEADER lpInfo)
  416. {
  417.     NPLOGPALETTE npPal;
  418.     RGBQUAD far *lpRGB;
  419.     HPALETTE hLogPal; 
  420.     WORD i;
  421.  
  422.     /* since biClrUsed field was filled during the loading of the DIB,
  423.     ** we know it contains the number of colors in the color table.
  424.     */
  425.     if (lpInfo->biClrUsed)
  426.     {
  427.     npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + 
  428.                 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
  429.     if (!npPal)
  430.         return(FALSE);
  431.  
  432.     npPal->palVersion = 0x300;
  433.         npPal->palNumEntries = (WORD)lpInfo->biClrUsed;
  434.  
  435.     /* get pointer to the color table */
  436.     lpRGB = (RGBQUAD FAR *)((LPSTR)lpInfo + lpInfo->biSize);
  437.  
  438.     /* copy colors from the color table to the LogPalette structure */
  439.         for (i = 0; i < (WORD)lpInfo->biClrUsed; i++, lpRGB++)
  440.     {
  441.         npPal->palPalEntry[i].peRed = lpRGB->rgbRed;
  442.         npPal->palPalEntry[i].peGreen = lpRGB->rgbGreen;
  443.         npPal->palPalEntry[i].peBlue = lpRGB->rgbBlue;
  444.         npPal->palPalEntry[i].peFlags = 0;
  445.     }
  446.  
  447.     hLogPal = CreatePalette((LPLOGPALETTE)npPal);
  448.     LocalFree((HANDLE)npPal);
  449.     return(hLogPal);
  450.     }
  451.  
  452.     /* 24-bit DIB with no color table.  return default palette.  Another
  453.     ** option would be to create a 256 color "rainbow" palette to provide
  454.     ** some good color choices.
  455.     */
  456.     else
  457.     return(GetStockObject(DEFAULT_PALETTE));
  458. }
  459.  
  460. /****************************************************************************
  461.  *                                        *
  462.  *  FUNCTION   : PaintDIB(hWnd)                            *
  463.  *                                        *
  464.  *  PURPOSE    : Paint the currently loaded DIB using the options chosen
  465.  *         by the user.
  466.  *                                        *
  467.  *  RETURNS    : None.
  468.  *                                        *
  469.  ****************************************************************************/
  470. void PASCAL NEAR PaintDIB(HWND hWnd)
  471. {
  472.     PAINTSTRUCT ps;
  473.     HDC hDC;
  474.     RECT Rectangle;
  475.     LPBITMAPINFOHEADER lpInfo;
  476.     LPBITMAPINFOHEADER lpHeader;
  477.     HPALETTE hOldPal;
  478.     WORD wDIBUse;
  479.  
  480.     hDC = BeginPaint(hWnd, &ps);
  481.     lpInfo = (LPBITMAPINFOHEADER) GlobalLock(hDIBInfo);
  482.  
  483.     /* if using a palette for drawing and there is actually 
  484.     ** a color table, then get a palette and realize it.
  485.     */
  486.     if (wPalOp && lpInfo->biClrUsed)
  487.     {
  488.     if (!hPalette)
  489.         hPalette = MakeDIBPalette(lpInfo);
  490.     if (!hPalette)
  491.     {
  492.         MessageBox(hWnd, "Can't create palette", "Error", MB_ICONSTOP | MB_OK);
  493.         goto ExitTime;
  494.     }
  495.     hOldPal = SelectPalette(hDC, hPalette, FALSE);
  496.     RealizePalette(hDC);
  497.  
  498.     /* if using DIB_PAL_COLORS and the bitmap is not 24-bit,
  499.     ** then use a header with a color table of indices.
  500.     */
  501.     if (wPalOp == IDM_PALIND && (lpInfo->biBitCount != 24))
  502.     {
  503.         if (!hPalHeader)
  504.         {
  505.         hPalHeader = MakeIndexHeader(lpInfo);
  506.         }
  507.         if (!hPalHeader)
  508.         {
  509.         MessageBox(hWnd, "Can't create Indexed color table", "Error", MB_ICONSTOP | MB_OK);
  510.         goto ExitTime;
  511.         }
  512.         lpHeader = (LPBITMAPINFOHEADER) GlobalLock(hPalHeader);
  513.         wDIBUse = DIB_PAL_COLORS;
  514.     }
  515.     else
  516.     {
  517.         lpHeader = lpInfo;
  518.         wDIBUse = DIB_RGB_COLORS;
  519.     }
  520.     }
  521.     else
  522.     {
  523.     lpHeader = lpInfo;
  524.     wDIBUse = DIB_RGB_COLORS;
  525.     }
  526.  
  527.     switch (wOperation)
  528.     {
  529.     case IDM_SETDIB:
  530.         //error message if can't create
  531.             
  532.         if (!hDDBitmap)
  533.         {
  534.         hMemDC = CreateCompatibleDC(hDC);
  535.         hDDBitmap = CreateCompatibleBitmap(hDC, 
  536.                 (WORD)lpInfo->biWidth, (WORD)lpInfo->biHeight);
  537.         SetDIBits(hDC, hDDBitmap, 0, (WORD)lpInfo->biHeight, 
  538.                     (LPSTR)lpInfo + offBits, 
  539.                     (LPBITMAPINFO)lpHeader, wDIBUse);
  540.         hOldBitmap = SelectObject(hMemDC, hDDBitmap);
  541.         }
  542.         BitBlt(hDC, 0, 0, (WORD)lpInfo->biWidth, (WORD)lpInfo->biHeight,
  543.                 hMemDC, 0, 0, SRCCOPY);
  544.         break;
  545.     case IDM_TODEV:
  546.         SetDIBitsToDevice(hDC, 0, 0, (WORD)lpInfo->biWidth, (WORD)lpInfo->biHeight,
  547.                 0, 0, 0, (WORD)lpInfo->biHeight, 
  548.             (LPSTR)lpInfo + offBits, (LPBITMAPINFO)lpHeader, wDIBUse);
  549.         break;
  550.     case IDM_STRETCH:
  551.     /* get dimensions of window and stretch to fit */
  552.         GetClientRect(hWnd, (LPRECT)&Rectangle);
  553.         StretchDIBits(hDC, 0, 0, Rectangle.right, Rectangle.bottom,
  554.                 0, 0, (WORD)lpInfo->biWidth, (WORD)lpInfo->biHeight,
  555.                 (LPSTR)lpInfo + offBits, 
  556.                 (LPBITMAPINFO)lpHeader, wDIBUse, SRCCOPY);
  557.  
  558.     /* NOTE: because driver does not do the StretchDIB
  559.     **       itself, this is not a fast operation.
  560.     **       internally it converts to CreateDIBitmap
  561.     **       followed by a StretchBlt
  562.     */
  563.         break;
  564.     }
  565.  
  566.     /* clean up any palette work */
  567.     if (wPalOp)
  568.     {
  569.     SelectPalette(hDC, hOldPal, FALSE);
  570.     if (wPalOp == IDM_PALIND)
  571.         GlobalUnlock(hPalHeader);
  572.     }
  573.  
  574. ExitTime:
  575.     GlobalUnlock(hDIBInfo);
  576.     EndPaint(hWnd, &ps);
  577. }
  578.  
  579.  
  580. /****************************************************************************
  581.  *                                        *
  582.  *  FUNCTION   : ReadDIB(hWnd)                            *
  583.  *                                        *
  584.  *  PURPOSE    : Reads a DIB from a file, obtains a handle to it's          *
  585.  *         BITMAPINFO struct. and loads the DIB.                      *
  586.  *                                        *
  587.  *  RETURNS    : TRUE  - DIB loads ok                        *
  588.  *         FALSE - otherwise                        *
  589.  *                                        *
  590.  ****************************************************************************/
  591. int ReadDIB(HWND hWnd)
  592. {
  593.     unsigned           fh;
  594.     LPBITMAPINFOHEADER lpbi;
  595.     OFSTRUCT           of;
  596.     BITMAPFILEHEADER   bf;
  597.     WORD        nNumColors;
  598.     WORD        result = FALSE;        /* assume failure */
  599.  
  600.     /* Open the file and get a handle to it's BITMAPINFO */
  601.  
  602.     fh = OpenFile (achFileName, &of, OF_READ);
  603.     if (fh == -1) {
  604.         wsprintf(str,"Can't open file '%ls'", (LPSTR)achFileName);
  605.     MessageBox(hWnd, str, "Error", MB_ICONSTOP | MB_OK);
  606.     return (FALSE);
  607.     }
  608.  
  609.     lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBInfo);
  610.  
  611.     /* read the BITMAPFILEHEADER */
  612.     if (sizeof (bf) != _lread (fh, (LPSTR)&bf, sizeof (bf)))
  613.     goto ErrExit;
  614.  
  615.     if (bf.bfType != 0x4d42)    /* 'BM' */
  616.     goto ErrExit;
  617.  
  618.     if (sizeof(BITMAPINFOHEADER) != _lread (fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)))
  619.     goto ErrExit;
  620.  
  621.     /* !!!!! for now, don't even deal with CORE headers */
  622.     if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
  623.     goto ErrExit;
  624.  
  625.     if (!(nNumColors = (WORD)lpbi->biClrUsed))
  626.     {
  627.     /* no color table for 24-bit, default size otherwise */
  628.     if (lpbi->biBitCount != 24)
  629.         nNumColors = 1 << lpbi->biBitCount;    /* standard size table */
  630.     }
  631.  
  632.     /*    fill in some default values if they are zero */
  633.     if (lpbi->biClrUsed == 0)
  634.         lpbi->biClrUsed = (DWORD)nNumColors;
  635.  
  636.     if (lpbi->biSizeImage == 0)
  637.     {
  638.     lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) + 31) & ~31) >> 3)
  639.              * lpbi->biHeight;
  640.     }
  641.  
  642.     /* get a proper-sized buffer for header, color table and bits */ 
  643.     GlobalUnlock(hDIBInfo);
  644.     hDIBInfo = GlobalReAlloc(hDIBInfo, lpbi->biSize +
  645.                         nNumColors * sizeof(RGBQUAD) +
  646.                     lpbi->biSizeImage, 0);
  647.     if (!hDIBInfo)    /* can't resize buffer for loading */
  648.     goto ErrExit2;
  649.  
  650.     lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBInfo);
  651.  
  652.     /* read the color table */
  653.     _lread (fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBQUAD));
  654.  
  655.     /* offset to the bits from start of DIB header */
  656.     offBits = (WORD)lpbi->biSize + nNumColors * sizeof(RGBQUAD);
  657.  
  658.     if (bf.bfOffBits != 0L)
  659.     {
  660.         _llseek(fh,bf.bfOffBits,SEEK_SET);
  661.     }
  662.     if (lpbi->biSizeImage == lread(fh, (LPSTR)lpbi + offBits, lpbi->biSizeImage))
  663.     result = TRUE;
  664.  
  665. ErrExit:
  666.     _lclose(fh);
  667.     GlobalUnlock(hDIBInfo);
  668. ErrExit2:
  669.     return(result);
  670. }
  671.  
  672. /**************** PRIVATE ROUTINE TO READ MORE THAN 64K *********************/
  673. /****************************************************************************
  674.  *                                        *
  675.  *  FUNCTION   : lread(int fh, VOID FAR *pv, DWORD ul)                *
  676.  *                                        *
  677.  *  PURPOSE    : Reads data in steps of 32k till all the data has been read.*
  678.  *                                        *
  679.  *  RETURNS    : 0 - If read did not proceed correctly.             *
  680.  *         number of bytes read otherwise.                *
  681.  *                                        *
  682.  ****************************************************************************/
  683. DWORD PASCAL lread (int fh, VOID far *pv, DWORD ul)
  684. {
  685.     DWORD     ulT = ul;
  686.     BYTE huge *hp = pv;
  687.  
  688.     while (ul > (DWORD)MAXREAD) {
  689.     if (_lread(fh, (LPSTR)hp, (WORD)MAXREAD) != MAXREAD)
  690.         return 0;
  691.     ul -= MAXREAD;
  692.     hp += MAXREAD;
  693.     }
  694.     if (_lread(fh, (LPSTR)hp, (WORD)ul) != (WORD)ul)
  695.     return 0;
  696.     return ulT;
  697. }
  698. /**************** PRIVATE ROUTINE TO READ MORE THAN 64K *********************/
  699.  
  700. /****************************************************************************
  701.  *                                        *
  702.  *  FUNCTION   : InitDIB(hWnd)                            *
  703.  *                                        *
  704.  *  PURPOSE    : cleans up old DIB info, reads new DIB from file,
  705.  *         resizes window for new DIB         
  706.  *                                        *
  707.  *  RETURNS    : TRUE  - DIB loads ok                        *
  708.  *         FALSE - otherwise                        *
  709.  *                                        *
  710.  ****************************************************************************/
  711. int InitDIB(HWND hWnd)
  712. {
  713.     LPBITMAPINFOHEADER lpbi;
  714.     RECT        Rectangle;
  715.  
  716.     /* if there was an old DIB, free it up */
  717.     if (bDIBLoaded)
  718.     {
  719.     if (hDDBitmap)
  720.     {
  721.         SelectObject(hMemDC, hOldBitmap);
  722.         DeleteDC(hMemDC);
  723.         DeleteObject(hDDBitmap);
  724.         hDDBitmap = NULL;
  725.     }
  726.     bDIBLoaded = FALSE;
  727.     if (hPalette)
  728.     {
  729.         DeleteObject(hPalette);
  730.         hPalette = 0;
  731.     }
  732.     if (hPalHeader)
  733.     {
  734.         GlobalFree(hPalHeader);
  735.         hPalHeader = NULL;
  736.     }
  737.     }
  738.  
  739.  
  740.     /* load the DIB from the file */
  741.     if (!ReadDIB(hWnd))
  742.     {
  743.     MessageBox(hWnd, "Error attempting to read DIB", "Error", MB_ICONSTOP | MB_OK);
  744.     return(FALSE);
  745.     }
  746.  
  747.     bDIBLoaded = TRUE;        /* there is a DIB loaded now */
  748.  
  749.     lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBInfo);
  750.     SetWindowText(hWnd, achFileName);
  751.     Rectangle.left     = 0;
  752.     Rectangle.top     = 0;
  753.     Rectangle.right  = (WORD)lpbi->biWidth;
  754.     Rectangle.bottom = (WORD)lpbi->biHeight;
  755.     GlobalUnlock(hDIBInfo);
  756.  
  757.     /* Compute the size of the window rectangle based on the given
  758.      * client rectangle size and the window style, then size the
  759.      * window.  Do not deal with possibility of more than one menu line.
  760.      */
  761.     AdjustWindowRect (&Rectangle, WS_OVERLAPPEDWINDOW, TRUE);
  762.     SetWindowPos (hWnd, (HWND)NULL, 0, 0,
  763.               Rectangle.right  - Rectangle.left,
  764.               Rectangle.bottom - Rectangle.top + 1,
  765.               SWP_NOMOVE | SWP_NOZORDER);
  766.  
  767.     GetClientRect(hWnd, &Rectangle);
  768.  
  769.     return(TRUE);
  770. }
  771.  
  772. /****************************************************************************
  773.  
  774.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  775.  
  776.     PURPOSE:  Processes messages for "About" dialog box
  777.  
  778.     MESSAGES:
  779.  
  780.     WM_INITDIALOG - initialize dialog box
  781.     WM_COMMAND    - Input received
  782.  
  783.     COMMENTS:
  784.  
  785.     No initialization is needed for this particular dialog box, but TRUE
  786.     must be returned to Windows.
  787.  
  788.     Wait for user to click on "Ok" button, then close the dialog box.
  789.  
  790. ****************************************************************************/
  791.  
  792. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  793. HWND hDlg;                                /* window handle of the dialog box */
  794. unsigned message;                         /* type of message                 */
  795. WORD wParam;                              /* message-specific information    */
  796. LONG lParam;
  797. {
  798.     switch (message) {
  799.     case WM_INITDIALOG:           /* message: initialize dialog box */
  800.         return (TRUE);
  801.  
  802.     case WM_COMMAND:              /* message: received a command */
  803.         if (wParam == IDOK                /* "OK" box selected?         */
  804.                 || wParam == IDCANCEL) {      /* System menu close command? */
  805.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  806.         return (TRUE);
  807.         }
  808.         break;
  809.     }
  810.     return (FALSE);                  /* Didn't process a message    */
  811. }
  812.