home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sb_bc.ddi / BC / CH6 / WBITMA-1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-20  |  6.1 KB  |  242 lines

  1. /***    Windows Application --- BITMAP example   ***/
  2. /***                                             ***/
  3. /***    WBITMA-1 includes                        ***/
  4. /***            (1) WBITMA-1.C                   ***/
  5. /***            (2) WBITMA-1.DEF                 ***/
  6. /***            (3) WBITMAP.RES                  ***/
  7.  
  8. #pragma hdrfile "windows.sym"
  9. #include <windows.h>
  10. #pragma hdrstop
  11.  
  12. #include "wmenu.h"
  13. #include "bitstr.h"
  14.  
  15. // Declaration
  16.  
  17. void InitString(HANDLE) ;
  18. void InitCursor(HANDLE) ;
  19. void InitIcon(HANDLE) ;
  20. void InitBitmap(HANDLE) ;
  21.  
  22. void InitFirstInstance(HANDLE) ;
  23. void InitEachInstance(HANDLE, int) ;
  24. long FAR PASCAL AppWndProc (HWND, WORD, WORD, LONG) ;
  25.  
  26. void ChangeCursor(HWND, WORD *, WORD, int) ;
  27. void ChangeIcon(HWND, WORD *, WORD, int) ;
  28. void ChangeBitmap(HWND, WORD *, WORD, int) ;
  29.  
  30. static char  szAppName[12] ;
  31. static char  szMenuName[12] ;
  32. static char  szAccelName[12] ;
  33. static char  szCursorName[3][12] ;
  34. static char  szIconName[2][12] ;
  35. static char  szBitmapName[3][12] ;
  36.  
  37. HWND  hwnd ;
  38.  
  39. HCURSOR hCursor[3] ;
  40. HICON   hIcon[2] ;
  41. HBITMAP hBitmap[3] ;
  42. HBRUSH hBrush[3] ;
  43.  
  44. // Definition
  45.  
  46. int PASCAL WinMain(HANDLE hInstance,
  47.            HANDLE hPrevInstance,
  48.            LPSTR  lpszCmdLine,
  49.            int    nCmdShow)
  50. { HANDLE hAccel ;
  51.   MSG msg ;
  52.  
  53.   InitString(hInstance) ;
  54.   InitCursor(hInstance) ;
  55.   InitIcon(hInstance) ;
  56.   InitBitmap(hInstance) ;
  57.  
  58.   if ( !hPrevInstance ) InitFirstInstance(hInstance) ;
  59.  
  60.   InitEachInstance(hInstance,nCmdShow) ;
  61.  
  62.   hAccel = LoadAccelerators(hInstance, szAccelName) ;
  63.  
  64.   while ( GetMessage(&msg, NULL, 0, 0) )
  65.     { if ( !TranslateAccelerator(hwnd, hAccel, &msg) )
  66.      { TranslateMessage(&msg) ;
  67.        DispatchMessage(&msg) ;
  68.      }
  69.     }
  70.   return msg.wParam ;
  71. }
  72.  
  73.  
  74. void InitString(HANDLE hInstance)
  75. {
  76.   LoadString(hInstance, IDS_APP, szAppName,11) ;
  77.   LoadString(hInstance, IDS_MENU, szMenuName,11) ;
  78.   LoadString(hInstance, IDS_ACCEL, szAccelName,11) ;
  79.  
  80.   LoadString(hInstance, IDS_CURSOR1, szCursorName[0],11) ;
  81.   LoadString(hInstance, IDS_CURSOR2, szCursorName[1],11) ;
  82.   LoadString(hInstance, IDS_CURSOR3, szCursorName[2],11) ;
  83.  
  84.   LoadString(hInstance, IDS_ICON1, szIconName[0],11) ;
  85.   LoadString(hInstance, IDS_ICON2, szIconName[1],11) ;
  86.  
  87.   LoadString(hInstance, IDS_BITMAP1, szBitmapName[0],11) ;
  88.   LoadString(hInstance, IDS_BITMAP2, szBitmapName[1],11) ;
  89.   LoadString(hInstance, IDS_BITMAP3, szBitmapName[2],11) ;
  90. }
  91.  
  92.  
  93. void InitCursor(HANDLE hInstance)
  94. { int i ;
  95.  
  96.   for (i=0; i<3; i++)
  97.       hCursor[i] = LoadCursor(hInstance, szCursorName[i]) ;
  98. }
  99.  
  100. void InitIcon(HANDLE hInstance)
  101. { int i ;
  102.  
  103.   for (i=0; i<2; i++)
  104.       hIcon[i] = LoadIcon(hInstance, szIconName[i]) ;
  105. }
  106.  
  107. void InitBitmap(HANDLE hInstance)
  108. { int i ;
  109.  
  110.   for (i=0; i<3; i++)
  111.       { hBitmap[i] = LoadBitmap(hInstance, szBitmapName[i]) ;
  112.     hBrush[i] = CreatePatternBrush(hBitmap[i]) ;
  113.       }
  114. }
  115.  
  116.  
  117. void InitFirstInstance(HANDLE hInstance)
  118. { WNDCLASS wndclass ;
  119.  
  120.   wndclass.style      = CS_HREDRAW | CS_VREDRAW ;
  121.   wndclass.lpfnWndProc      = AppWndProc ;
  122.   wndclass.cbClsExtra       = 0 ;
  123.   wndclass.cbWndExtra      = 0 ;
  124.   wndclass.hInstance      = hInstance ;
  125.   wndclass.hIcon             = hIcon[0] ;
  126.   wndclass.hCursor      = hCursor[0] ;
  127.   wndclass.hbrBackground  = hBrush[0] ;
  128.   wndclass.lpszMenuName      = szMenuName ;
  129.   wndclass.lpszClassName  = szAppName ;
  130.  
  131.   RegisterClass(&wndclass) ;
  132. }
  133.  
  134.  
  135. void InitEachInstance(HANDLE hInstance, int nCmdShow)
  136. {
  137.   hwnd = CreateWindow(szAppName,               // window class name
  138.               "Windows Application", // window caption
  139.               WS_OVERLAPPEDWINDOW ,
  140.               CW_USEDEFAULT,      // initial x position
  141.               0,                  // initial y position
  142.               CW_USEDEFAULT,      // initial x length
  143.               0,                  // initial y length
  144.               NULL,               // parent window handle
  145.               NULL,               // window menu handle
  146.               hInstance,          // program instance handle
  147.               NULL) ;             // parameters
  148.  
  149.   ShowWindow(hwnd,nCmdShow) ;
  150.   UpdateWindow(hwnd) ;
  151. }
  152.  
  153.  
  154. long FAR PASCAL AppWndProc (HWND hwnd,
  155.                 WORD message,
  156.                 WORD wParam,
  157.                 LONG lParam)
  158. { static WORD Cursor_Check = IDM_CURSOR1,
  159.           Icon_Check   = IDM_ICON1,
  160.           Bitmap_Check = IDM_BITMAP1 ;
  161.   int i ;
  162.  
  163.   switch (message)
  164.     { case WM_COMMAND :
  165.        switch(wParam)
  166.          { case IDM_CURSOR1 :
  167.             ChangeCursor(hwnd, &Cursor_Check, wParam, 0) ;
  168.             break ;
  169.  
  170.            case IDM_CURSOR2 :
  171.             ChangeCursor(hwnd, &Cursor_Check, wParam, 1) ;
  172.             break ;
  173.  
  174.            case IDM_CURSOR3 :
  175.             ChangeCursor(hwnd, &Cursor_Check, wParam, 2) ;
  176.             break ;
  177.  
  178.            case IDM_ICON1 :
  179.             ChangeIcon(hwnd, &Icon_Check, wParam, 0) ;
  180.             break ;
  181.  
  182.            case IDM_ICON2 :
  183.             ChangeIcon(hwnd, &Icon_Check, wParam, 1) ;
  184.             break ;
  185.  
  186.            case IDM_BITMAP1 :
  187.             ChangeBitmap(hwnd, &Bitmap_Check, wParam, 0) ;
  188.             break ;
  189.  
  190.            case IDM_BITMAP2 :
  191.             ChangeBitmap(hwnd, &Bitmap_Check, wParam, 1) ;
  192.             break ;
  193.  
  194.            case IDM_BITMAP3 :
  195.             ChangeBitmap(hwnd, &Bitmap_Check, wParam, 2) ;
  196.             break ;
  197.          }
  198.        break ;
  199.  
  200.       case WM_DESTROY :
  201.        for (i=0; i<3; i++)
  202.            { DeleteObject(hBitmap[i]) ;
  203.          DeleteObject(hBrush[i]) ;
  204.            }
  205.        PostQuitMessage(0) ;
  206.        break ;
  207.  
  208.       default :
  209.        return DefWindowProc(hwnd, message, wParam, lParam) ;
  210.     }
  211.  
  212.   return 0L ;
  213. }
  214.  
  215.  
  216. void ChangeCursor(HWND hwnd, WORD *wPrev, WORD wParam, int i)
  217. { HMENU hMenu = GetMenu(hwnd) ;
  218.  
  219.   SetClassWord(hwnd, GCW_HCURSOR, hCursor[i]) ;
  220.   CheckMenuItem(hMenu, *wPrev, MF_UNCHECKED) ;
  221.   CheckMenuItem(hMenu, wParam, MF_CHECKED) ;
  222.   *wPrev = wParam ;
  223. }
  224.  
  225. void ChangeIcon(HWND hwnd, WORD *wPrev, WORD wParam, int i)
  226. { HMENU hMenu = GetMenu(hwnd) ;
  227.  
  228.   SetClassWord(hwnd, GCW_HICON, hIcon[i]) ;
  229.   CheckMenuItem(hMenu, *wPrev, MF_UNCHECKED) ;
  230.   CheckMenuItem(hMenu, wParam, MF_CHECKED) ;
  231.   *wPrev = wParam ;
  232. }
  233.  
  234. void ChangeBitmap(HWND hwnd, WORD *wPrev, WORD wParam, int i)
  235. { HMENU hMenu = GetMenu(hwnd) ;
  236.  
  237.   SetClassWord(hwnd, GCW_HBRBACKGROUND, hBrush[i]) ;
  238.   CheckMenuItem(hMenu, *wPrev, MF_UNCHECKED) ;
  239.   CheckMenuItem(hMenu, wParam, MF_CHECKED) ;
  240.   *wPrev = wParam ;
  241. }
  242.