home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / ewdll011 / ewdll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  10.6 KB  |  314 lines

  1. /*---------------------------------------------------------------------------*/
  2. /* EWDLL.CPP - Borland C++ 4.0 - 256 Color BMP DLL for WinHelp 3.1           */
  3. /* Copyright (C) 1994 by R&R Engineering - All Rights Reserved               */
  4. /*                                                                           */
  5. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  6. /*                                                                           */
  7. /*  This library is free software; you can redistribute it and/or modify it  */
  8. /*  under the terms of the GNU Library General Public License as published   */
  9. /*  by the Free Software Foundation; either version 2 of the License, or     */
  10. /*  (at your option) any later version.                                      */
  11. /*                                                                           */
  12. /*  This library is distributed in the hope that it will be useful, but      */
  13. /*  WITHOUT ANY WARRANTY; without even the implied warranty of               */
  14. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        */
  15. /*  Library General Public License for more details.                         */
  16. /*                                                                           */
  17. /*  You should have received a copy of the GNU Library General Public        */
  18. /*  License along with this library; if not, write to the Free Software      */
  19. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  20. /*                                                                           */
  21. /*---------------------------------------------------------------------------*/
  22.  
  23. #define STRICT
  24.  
  25. #include <stdlib.h>
  26. #include <windowsx.h>
  27. #include "ewdll.h"
  28.  
  29. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  30.  
  31. // Define the default window procedure
  32. #define EWBMP_DefProc DefWindowProc
  33.  
  34. // Window class name
  35. static char ClassName[] = "ewBitmap";
  36.  
  37. // Define a structure for storing bitmap information for a window
  38. typedef struct {
  39.    HBITMAP hbmp;
  40.    HPALETTE hpal;
  41.    int width, height;
  42. } EWBMPINFO;
  43.  
  44. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  45.  
  46. LONG _lsize( HFILE hFile ) {
  47.  
  48.    /*---------------------------------------------------------------------*/
  49.    /*  Return the length (in bytes) of an open file.                      */
  50.    /*---------------------------------------------------------------------*/
  51.  
  52.    LONG p = _llseek( hFile, 0, 1 );  // Save position in the file
  53.    LONG s = _llseek( hFile, 0, 2 );  // Get the length
  54.    _llseek( hFile, p, 0 );           // Restore original position
  55.    return s;                         // Return length
  56. }
  57.  
  58. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  59.  
  60. HBITMAP LoadDIBitmap( char *filespec, HPALETTE *palette ) {
  61.  
  62.    /*---------------------------------------------------------------------*/
  63.    /*  Load a Windows 3.x device independent BMP file.  The return value  */
  64.    /*  is a handle to the logical bitmap if successful, or zero if an     */
  65.    /*  error occurred.  For all besides 24-bit bitmaps, the palette       */
  66.    /*  parameter is a handle to the palette required to properly display  */
  67.    /*  the bitmap.  For 24-bit bitmaps palette is set to zero.            */
  68.    /*---------------------------------------------------------------------*/
  69.  
  70.    BITMAPFILEHEADER bmfh;
  71.    BITMAPINFO *bmi;
  72.    BITMAPINFOHEADER *bmih;
  73.    LOGPALETTE *lp;
  74.    unsigned colors;
  75.    HBITMAP ldib;
  76.  
  77.    // Open the bitmap file for read-only access
  78.    HFILE hFile = _lopen( filespec, READ );
  79.    if (hFile == HFILE_ERROR) return 0;
  80.  
  81.    // Read the file header
  82.    if (_lread( hFile, &bmfh, sizeof( bmfh ) ) != sizeof( bmfh )) {
  83.       _lclose( hFile );
  84.       return 0;
  85.    }
  86.  
  87.    // Check for the Windows 3 bitmap file signature
  88.    if (bmfh.bfType != 0x4D42) {
  89.       _lclose( hFile );
  90.       return 0;
  91.    }
  92.  
  93.    // Allocate a buffer to contain the bitmap data
  94.    LONG bufsize = _lsize( hFile ) - sizeof( bmfh );
  95.    HGLOBAL buffer = GlobalAlloc( GMEM_MOVEABLE, bufsize );
  96.    if (buffer == 0) {
  97.       _lclose( hFile );
  98.       return 0;
  99.    }
  100.  
  101.    // Lock the buffer and read the bitmap data
  102.    bmi = (BITMAPINFO*)GlobalLock( buffer );
  103.    if (_hread( hFile, bmi, bufsize ) == bufsize) {
  104.  
  105.       // Check validity of BITMAPINFOHEADER structure
  106.       bmih = &bmi->bmiHeader;
  107.       if (bmih->biSize == sizeof( BITMAPINFOHEADER )) {
  108.  
  109.          // If this is not a 24-bit bitmap then create a palette
  110.          if (bmih->biPlanes * bmih->biBitCount != 24) {
  111.  
  112.             // Determine the number of colors used
  113.             if (bmih->biClrUsed != 0)
  114.                colors = (unsigned)bmih->biClrUsed;
  115.             else
  116.                colors = 1 << (bmih->biPlanes * bmih->biBitCount);
  117.  
  118.             // Allocate memory for a logical palette
  119.             lp = (LOGPALETTE*)malloc( sizeof( LOGPALETTE ) +
  120.                colors * sizeof( PALETTEENTRY ) );
  121.  
  122.             // Fill in the logical palette
  123.             lp->palVersion = 0x0300;
  124.             lp->palNumEntries = colors;
  125.  
  126.             // Copy the colors from the bitmap to the palette
  127.             for (int i = 0; i < colors; i++) {
  128.                lp->palPalEntry[ i ].peRed   = bmi->bmiColors[ i ].rgbRed;
  129.                lp->palPalEntry[ i ].peGreen = bmi->bmiColors[ i ].rgbGreen;
  130.                lp->palPalEntry[ i ].peBlue  = bmi->bmiColors[ i ].rgbBlue;
  131.                lp->palPalEntry[ i ].peFlags = 0;
  132.             }
  133.  
  134.             // Create the palette and free the logical palette
  135.             *palette = CreatePalette( lp );
  136.             free( lp );
  137.          }
  138.  
  139.          else
  140.  
  141.             // Set the palette handle to zero for 24-bit bitmaps
  142.             *palette = 0;
  143.  
  144.          // Create the bitmap
  145.          HDC DC = GetDC( 0 );
  146.          *palette = SelectPalette( DC, *palette, FALSE );
  147.          RealizePalette( DC );
  148.          ldib = CreateDIBitmap( DC, bmih, CBM_INIT,
  149.             ((LPSTR)(bmih + 1)) + (colors * sizeof( RGBQUAD )),
  150.             bmi, DIB_RGB_COLORS );
  151.          *palette = SelectPalette( DC, *palette, FALSE );
  152.          RealizePalette( DC );
  153.          ReleaseDC( 0, DC );
  154.       }
  155.    }
  156.  
  157.    // Close the file, release the global memory block, and return
  158.    _lclose( hFile );
  159.    GlobalUnlock( buffer );
  160.    GlobalFree( buffer );
  161.    return ldib;
  162. }
  163.  
  164. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  165.  
  166. HPALETTE EWBMP_OnAskPalette( HWND hWnd ) {
  167.  
  168.    EWBMPINFO *hbi;
  169.  
  170.    // Get the palette from the bitmap info stored with the window
  171.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  172.    return hbi->hpal;
  173. }
  174.  
  175. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  176.  
  177. BOOL EWBMP_OnQuerySize( HWND hWnd, HDC DC, POINT *size ) {
  178.  
  179.    EWBMPINFO *hbi;
  180.  
  181.    // Get the size from the bitmap info stored with the window
  182.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  183.    size->x = hbi->width;
  184.    size->y = hbi->height;
  185.    return TRUE;
  186. }
  187.  
  188. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  189.  
  190. BOOL EWBMP_OnCreate( HWND hWnd, CREATESTRUCT *lpCreateStruct ) {
  191.  
  192.    CREATEINFO *ci;
  193.    EWBMPINFO *hbi;
  194.    BITMAP bmp;
  195.  
  196.    // Add a border to the window
  197.    SetWindowLong( hWnd, GWL_STYLE, GetWindowLong( hWnd, GWL_STYLE ) | WS_BORDER );
  198.  
  199.    // Allocate structure for window bitmap information and save
  200.    hbi = (EWBMPINFO*)malloc( sizeof( EWBMPINFO ) );
  201.    SetWindowLong( hWnd, 0, (LONG)hbi );
  202.  
  203.    // Read the bitmap file
  204.    ci = (CREATEINFO*)lpCreateStruct->lpCreateParams;
  205.    hbi->hbmp = LoadDIBitmap( ci->lpstrAuthorData, &hbi->hpal );
  206.  
  207.    // If a bitmap was read then determine its size
  208.    if (hbi->hbmp != 0) {
  209.       GetObject( hbi->hbmp, sizeof( BITMAP ), &bmp );
  210.       hbi->width = bmp.bmWidth;
  211.       hbi->height = bmp.bmHeight;
  212.    }
  213.    else {
  214.       hbi->width = 0;
  215.       hbi->height = 0;
  216.    }
  217.    return TRUE;
  218. }
  219.  
  220. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  221.  
  222. void EWBMP_OnDestroy( HWND hWnd ) {
  223.  
  224.    EWBMPINFO *hbi;
  225.  
  226.    // Retrieve the structure allocated for storing bitmap information
  227.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  228.  
  229.    // Delete the bitmap and palette objects
  230.    DeleteObject( hbi->hbmp );
  231.    DeleteObject( hbi->hpal );
  232.  
  233.    // Free the structure used to store bitmap info with the window
  234.    free( hbi );
  235. }
  236.  
  237. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  238.  
  239. BOOL EWBMP_OnPaint( HWND hWnd ) {
  240.  
  241.    PAINTSTRUCT ps;
  242.    EWBMPINFO *hbi;
  243.    HDC memDC;
  244.  
  245.    // Get the bitmap information structure
  246.    hbi = (EWBMPINFO*)GetWindowLong( hWnd, 0 );
  247.  
  248.    // Paint the bitmap
  249.    BeginPaint( hWnd, &ps );
  250.    memDC = CreateCompatibleDC( ps.hdc );
  251.    SelectObject( memDC, hbi->hbmp );
  252.    BitBlt( ps.hdc, 0, 0, hbi->width, hbi->height, memDC, 0, 0, SRCCOPY );
  253.    DeleteDC( memDC );
  254.    EndPaint( hWnd, &ps );
  255.  
  256.    return 0;
  257. }
  258.  
  259. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  260.  
  261. LRESULT CALLBACK _export WndProc( HWND hWnd, UINT Message, WPARAM wParam,
  262.                                   LPARAM lParam ) {
  263.  
  264.    switch( Message ) {
  265.       HANDLE_MSG( hWnd, EWM_ASKPALETTE, EWBMP_OnAskPalette );
  266.       HANDLE_MSG( hWnd, EWM_QUERYSIZE, EWBMP_OnQuerySize );
  267.       HANDLE_MSG( hWnd, WM_CREATE, EWBMP_OnCreate );
  268.       HANDLE_MSG( hWnd, WM_DESTROY, EWBMP_OnDestroy );
  269.       HANDLE_MSG( hWnd, WM_PAINT, EWBMP_OnPaint );
  270.       default : return EWBMP_DefProc( hWnd, Message, wParam, lParam );
  271.    }
  272. }
  273.  
  274. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  275.  
  276. BOOL Register( HINSTANCE hInst ) {
  277.  
  278.    WNDCLASS WindowClass;
  279.  
  280.    WindowClass.style         = CS_GLOBALCLASS;
  281.    WindowClass.lpfnWndProc   = WndProc;
  282.    WindowClass.cbClsExtra    = 0;
  283.    WindowClass.cbWndExtra    = 4;
  284.    WindowClass.hInstance     = hInst;
  285.    WindowClass.hIcon         = NULL;
  286.    WindowClass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  287.    WindowClass.hbrBackground = GetStockBrush( WHITE_BRUSH );
  288.    WindowClass.lpszMenuName  = NULL;
  289.    WindowClass.lpszClassName = ClassName;
  290.  
  291.    return RegisterClass( &WindowClass );
  292. }
  293.  
  294. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  295.  
  296. int CALLBACK LibMain( HINSTANCE hInst, WORD wDataSeg, WORD wHeapSize,
  297.                               LPSTR lpszCmdLine ) {
  298.  
  299.    // Register the window class
  300.    if (!Register( hInst )) return 0;
  301.  
  302.    // Unlock the data segment
  303.    if (wHeapSize > 0) UnlockData( 0 );
  304.  
  305.    return TRUE;
  306. }
  307.  
  308. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  309.  
  310. int CALLBACK WEP( UINT nParam ) {
  311.  
  312.    return TRUE;
  313. }
  314.