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

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