home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / icon / icontest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  6.7 KB  |  241 lines

  1. /*
  2. Date: 7/19/88
  3. Function(s) demonstrated in this program: SetResourceHandler
  4. Compiler version: 5.1
  5. Description: This example is taken from Application Note 12 (Windows "Icon"
  6.              utility). The SetResourceHandler should only be used with
  7.              user defined resources. In this example a generic bitmap is
  8.              read through ICONLIB.LIB and ICONLIB.EXE (both of this files are
  9.              need) and displayed in ICONTEST as an icon. The function
  10.              SetResourceHandler is used to have the user defined function
  11.              ResourceHandler setup the bitmap as an icon when the bitmap
  12.              is loaded.
  13. */
  14.  
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include "icontest.h"
  18.  
  19. typedef struct {
  20.     short   csHotX;
  21.     short   csHotY;
  22.     short   csWidth;
  23.     short   csHeight;
  24.     short   csWidthBytes;
  25.     short   csColor;
  26.     char    csBits[ 1 ];
  27.     } CURSORSHAPE;
  28.  
  29. typedef struct {
  30.     short magic;
  31.     CURSORSHAPE info;
  32.     } CURSORFILEHEADER;
  33.  
  34. typedef CURSORFILEHEADER FAR *LPCURSOR;
  35.  
  36. static char szAppName [] = "ICONTEST" ;
  37. static char szFuncName [] = "SetResourceHandler" ;
  38. WORD hInst;
  39.  
  40. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  41. HANDLE hInstance;
  42. HANDLE hPrevInstance;
  43. LPSTR  lpszCmdLine;
  44. int    cmdShow;
  45. {
  46.     MSG        msg;
  47.     WNDCLASS   wndclass;
  48.     HWND       hWnd;
  49.     HMENU      hMenu;
  50.  
  51.     if ( !hPrevInstance )
  52.        {
  53.        wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  54.        wndclass.lpfnWndProc    = WndProc;
  55.        wndclass.cbWndExtra     = 0;
  56.        wndclass.cbClsExtra     = 0;
  57.        wndclass.hInstance      = hInstance;
  58.        wndclass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  59.        wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
  60.        wndclass.hbrBackground  = GetStockObject( WHITE_BRUSH );
  61.        wndclass.lpszMenuName   = "icontest";
  62.        wndclass.lpszClassName  = szAppName;
  63.     
  64.       if ( !RegisterClass( &wndclass ) )
  65.            return FALSE;
  66.        }
  67.  
  68.     hMenu = LoadMenu( hInstance, (LPSTR)"icontest" );
  69.     hInst = hInstance;
  70.  
  71.     hWnd = CreateWindow( szAppName,     /* window class name       */
  72.             szFuncName,                 /* window caption          */
  73.             WS_OVERLAPPEDWINDOW,        /* window style            */
  74.             CW_USEDEFAULT,              /* initial x position      */
  75.             0,                          /* initial y position      */
  76.             CW_USEDEFAULT,              /* initial x size          */
  77.             0,                          /* initial y size          */
  78.             NULL,                       /* parent window handle    */
  79.             hMenu,                      /* window menu handle      */
  80.             hInstance,                  /* program instance handle */
  81.             NULL );                     /* create parameters       */
  82.                             
  83.     ShowWindow( hWnd, cmdShow );
  84.     UpdateWindow( hWnd );
  85.  
  86.     while ( GetMessage( ( LPMSG )&msg, NULL, 0, 0 ) )
  87.        {
  88.        TranslateMessage( ( LPMSG )&msg );
  89.        DispatchMessage( ( LPMSG )&msg );
  90.        }
  91.     return( msg.wParam );
  92. }
  93.  
  94. long FAR PASCAL WndProc( hWnd, iMessage, wParam, lParam )
  95. HWND       hWnd;
  96. unsigned   iMessage;
  97. WORD       wParam;
  98. LONG       lParam;
  99. {
  100.     HDC          hDC;
  101.  
  102.     switch ( iMessage )
  103.     {
  104.     case WM_INITMENU:
  105.        InvalidateRect ( hWnd, NULL, TRUE );
  106.        break;
  107.       
  108.     case WM_CLOSE:
  109.        DestroyWindow ( hWnd );
  110.        break;
  111.  
  112.     case WM_DESTROY:
  113.        PostQuitMessage( 0 );
  114.        break;
  115.  
  116.     case WM_COMMAND:
  117.        IconTestCommand ( hWnd, wParam );
  118.        break;
  119.  
  120.     default:
  121.         return ( DefWindowProc( hWnd, iMessage, wParam, lParam ) );
  122.     }
  123.     return( 0L );
  124. }
  125.  
  126. /*
  127. **  ResourceHandler - routine to service "calculated" resources.
  128. **  In this case, Icons.
  129. **
  130. **  A resource handler can be installed for each resource TYPE.
  131. */
  132.  
  133. HANDLE FAR PASCAL ResourceHandler( hRes, hResFile, hResIndex )
  134. HANDLE hRes;
  135. HANDLE hResFile;
  136. HANDLE hResIndex;
  137. {
  138.     WORD       wSize;
  139.     BYTE FAR   *lpIcon;
  140.     int        nIndex;
  141.  
  142.     /* Process machine dependent ICON and CURSOR resources. */
  143.     wSize = ( WORD )SizeofResource( hResFile, hResIndex );
  144.     nIndex = 0;
  145.  
  146.     if ( !hRes )
  147.        {
  148.        if ( !( hRes = AllocResource( hResFile, hResIndex, 0L ) ) )
  149.            return NULL;
  150.        nIndex = -1;
  151.        }
  152.  
  153.     while ( !( lpIcon = ( BYTE FAR * )GlobalLock( hRes ) ) )
  154.     if ( !GlobalReAlloc( hRes, ( DWORD )wSize, 0 ) )
  155.         return NULL;
  156.     else
  157.         nIndex = -1;
  158.  
  159.     if ( nIndex )
  160.     if ( ( nIndex = AccessResource( hResFile, hResIndex ) ) != -1 &&
  161.         _lread( nIndex, lpIcon, wSize ) != -1 )
  162.         _lclose( nIndex );
  163.     else
  164.         {
  165.         if ( nIndex != -1 )
  166.         _lclose( nIndex );
  167.         GlobalUnlock( hRes );
  168.         return NULL;
  169.         }
  170.  
  171.     /* Remove magic word. */
  172.     wSize -= 2;
  173.     while ( wSize-- )
  174.        {
  175.        *lpIcon = *( lpIcon+2 );
  176.        lpIcon++;
  177.        }
  178.  
  179.     GlobalUnlock( hRes );
  180.     GlobalReAlloc( hRes, ( DWORD )wSize, 0 );
  181.  
  182.     return ( hRes );
  183. }
  184.  
  185. void IconTestCommand ( hWnd, wCommand )
  186. HWND hWnd;
  187. WORD wCommand;
  188. {
  189.     FARPROC lpResourceHandler;
  190.     FARPROC lpOld;
  191.     HDC     hDC;
  192.     HICON   hIcon;
  193.     HANDLE  hAppDLL;
  194.     HANDLE  hModule;
  195.     int     nDrawn;
  196.    
  197.  
  198.     switch ( wCommand )
  199.        {
  200.        case CMD_GETICON:
  201.           DummyEntry (  );
  202.           hDC = GetDC ( hWnd );
  203.           TextOut ( hDC, 10, 10, ( LPSTR ) "LibIcon", 7 );
  204.    
  205.           hAppDLL = LoadLibrary ( (LPSTR)"ICONLIB.LIB" );
  206.    
  207.           if ( hAppDLL < 32 )
  208.              MessageBox ( hWnd, (LPSTR)"Loading DLL failed!",
  209.                 (LPSTR)szFuncName, MB_OK );
  210.           else
  211.              {
  212.              hModule = GetModuleHandle ( ( LPSTR )"ICONLIB" );
  213.              if ( hModule != NULL )
  214.                 {
  215.                 lpResourceHandler = MakeProcInstance (
  216.                    (FARPROC)ResourceHandler, hInst );
  217.                                                     /* Install own handler. */
  218.                 lpOld = SetResourceHandler( hModule, RT_ICON,
  219.                    lpResourceHandler );
  220.    
  221.                 hIcon = LoadIcon ( hModule, ( LPSTR ) "LibIcon" );
  222.                 nDrawn = DrawIcon ( hDC, 10, 25, hIcon );
  223.    
  224.                 FreeProcInstance( SetResourceHandler( hModule, RT_ICON,
  225.                    ( FARPROC )lpOld ) );
  226.                 }
  227.              else 
  228.                 MessageBox ( hWnd, (LPSTR)"Resource Handlers not initialized",
  229.                    (LPSTR)szFuncName, MB_OK );
  230.    
  231.              FreeLibrary ( hAppDLL );
  232.              }
  233.           ReleaseDC ( hWnd, hDC );
  234.           break;
  235.        
  236.        default:
  237.           return;
  238.        }
  239. }
  240.  
  241.