home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / begin / dll / demo.c next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  8.8 KB  |  300 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /****************************************************************************
  13.  
  14.     PROGRAM: Demo.c
  15.  
  16.     PURPOSE: Demonstrates how to manipulate a cursor and select a region
  17.  
  18.     FUNCTIONS:
  19.  
  20.         WinMain() - calls initialization function, processes message loop
  21.         DemoInit() - initializes window data and registers window
  22.         DemoWndProc() - processes messages
  23.         About() - processes messages for "About" dialog box
  24.  
  25.     COMMENTS:
  26.         This code is a modified version of the CURSOR.C program.  Instead of
  27.         using inline code for drawing the shape, the routines from the Select
  28.         library are called.
  29.  
  30. ****************************************************************************/
  31.  
  32. #include "windows.h"
  33.  
  34. #include "demo.h"
  35. #include "select.h"
  36.  
  37. HANDLE hInst;
  38. BOOL bTrack = FALSE;
  39. INT OrgX = 0, OrgY = 0;
  40. INT PrevX = 0, PrevY = 0;
  41. INT X = 0, Y = 0;
  42.  
  43. RECT Rect;
  44.  
  45. INT Shape = SL_BLOCK;                          /* Shape to use for rectangle */
  46. BOOL RetainShape = FALSE;                      /* Retain or destroy shape    */
  47.  
  48. int APIENTRY WinMain(HINSTANCE hInstance,
  49.                      HINSTANCE hPrevInstance,
  50.                      LPSTR lpCmdLine,
  51.                      int nCmdShow)
  52. {
  53.  
  54.     HWND hWnd;
  55.     MSG msg;
  56.     CHAR szAppName[64];
  57.  
  58.     UNREFERENCED_PARAMETER(lpCmdLine);
  59.  
  60.     if (!hPrevInstance)
  61.         if (!DemoInit(hInstance))
  62.             return (0);
  63.  
  64.     hInst = hInstance;
  65.     LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
  66.  
  67.     hWnd = CreateWindow("Demo",
  68.         szAppName,
  69.         WS_OVERLAPPEDWINDOW,
  70.         CW_USEDEFAULT,
  71.         CW_USEDEFAULT,
  72.         CW_USEDEFAULT,
  73.         CW_USEDEFAULT,
  74.         NULL,
  75.         NULL,
  76.         hInstance,
  77.         NULL);
  78.  
  79.     if (!hWnd)
  80.         return (0);
  81.  
  82.     ShowWindow(hWnd, nCmdShow);
  83.     UpdateWindow(hWnd);
  84.  
  85.     while (GetMessage(&msg, NULL, 0, 0)) {
  86.         TranslateMessage(&msg);
  87.         DispatchMessage(&msg);
  88.     }
  89.     return (msg.wParam);
  90. }
  91.  
  92. /****************************************************************************
  93.  
  94.     FUNCTION: DemoInit(HANDLE)
  95.  
  96.     PURPOSE: Initializes window data and registers window class
  97.  
  98. ****************************************************************************/
  99.  
  100. BOOL DemoInit(HANDLE hInstance)
  101. {
  102.     HANDLE hMemory;
  103.     PWNDCLASS pWndClass;
  104.     BOOL bSuccess;
  105.     CHAR lpBuffer[128];
  106.  
  107.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  108.     if(!hMemory){
  109.         LoadString(hInst, IDS_NOMEM, lpBuffer, sizeof(lpBuffer));
  110.         MessageBox(NULL, lpBuffer, NULL, MB_OK | MB_ICONHAND);
  111.         return(FALSE);
  112.     }
  113.  
  114.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  115.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  116.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  117.     pWndClass->lpszMenuName = (LPSTR) "Menu";
  118.     pWndClass->lpszClassName = (LPSTR) "Demo";
  119.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  120.     pWndClass->hInstance = hInstance;
  121.     pWndClass->style = 0;
  122.     pWndClass->lpfnWndProc = (WNDPROC)DemoWndProc;
  123.  
  124.     bSuccess = RegisterClass(pWndClass);
  125.  
  126.     LocalUnlock(hMemory);
  127.     LocalFree(hMemory);
  128.     return (bSuccess);
  129. }
  130.  
  131. /****************************************************************************
  132.  
  133.     FUNCTION: DemoWndProc(HWND, unsigned, WORD, LONG)
  134.  
  135.     PURPOSE:  Processes messages
  136.  
  137.     MESSAGES:
  138.  
  139.         WM_SYSCOMMAND - system menu (About dialog box)
  140.         WM_CREATE     - create window
  141.         WM_DESTROY    - destroy window
  142.         WM_LBUTTONDOWN - left mouse button
  143.         WM_MOUSEMOVE   - mouse movement
  144.         WM_LBUTTONUP   - left button released
  145.  
  146.         WM_COMMAND messages:
  147.             IDM_BOX    - use inverted box for selecting a region
  148.             IDM_BLOCK  - use empty box for selecting a region
  149.             IDM_RETAIN - retain/delete selection on button release
  150.  
  151.     COMMENTS:
  152.  
  153.         When the left mouse button is pressed, btrack is set to TRUE so that
  154.         the code for WM_MOUSEMOVE will keep track of the mouse and update the
  155.         box accordingly.  Once the button is released, btrack is set to
  156.         FALSE, and the current position is saved.  Holding the SHIFT key
  157.         while pressing the left button will extend the current box rather
  158.         then erasing it and starting a new one.  The exception is when the
  159.         retain shape option is enabled.  With this option, the rectangle is
  160.         zeroed whenever the mouse is released so that it can not be erased or
  161.         extended.
  162.  
  163. ****************************************************************************/
  164.  
  165. LONG APIENTRY DemoWndProc(
  166.     HWND hWnd,
  167.     UINT message,
  168.     UINT wParam,
  169.     LONG lParam)
  170. {
  171.     FARPROC lpProcAbout;
  172.     HMENU hMenu;
  173.  
  174.     switch (message) {
  175.  
  176.         case WM_COMMAND:
  177.  
  178.             // LOWORD added for portability
  179.  
  180.             switch (LOWORD(wParam)) {
  181.                 case IDM_BOX:
  182.                     Shape = SL_BOX;
  183.                     hMenu = GetMenu(hWnd);
  184.                     CheckMenuItem(hMenu, IDM_BOX, MF_CHECKED);
  185.                     CheckMenuItem(hMenu, IDM_BLOCK, MF_UNCHECKED);
  186.                     break;
  187.  
  188.                 case IDM_BLOCK:
  189.                     Shape = SL_BLOCK;
  190.                     hMenu = GetMenu(hWnd);
  191.                     CheckMenuItem(hMenu, IDM_BOX, MF_UNCHECKED);
  192.                     CheckMenuItem(hMenu, IDM_BLOCK, MF_CHECKED);
  193.                     break;
  194.  
  195.                 case IDM_RETAIN:
  196.                     if (RetainShape) {
  197.                         hMenu = GetMenu(hWnd);
  198.                         CheckMenuItem(hMenu, IDM_RETAIN, MF_UNCHECKED);
  199.                         RetainShape = FALSE;
  200.                     }
  201.                     else {
  202.                         hMenu = GetMenu(hWnd);
  203.                         CheckMenuItem(hMenu, IDM_RETAIN, MF_CHECKED);
  204.                         RetainShape = TRUE;
  205.                     }
  206.                     break;
  207.  
  208.                 case IDM_ABOUT:
  209.                     lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
  210.                     DialogBox(hInst, "AboutBox", hWnd, (DLGPROC)lpProcAbout);
  211.                     FreeProcInstance(lpProcAbout);
  212.                     break;
  213.  
  214.             }
  215.             break;
  216.  
  217.         case WM_LBUTTONDOWN:
  218.  
  219.             bTrack = TRUE;               /* user has pressed the left button */
  220.  
  221.             /* If you don't want the shape cleared, you must clear the Rect
  222.              * coordinates before calling StartSelection
  223.              */
  224.  
  225.             if (RetainShape)
  226.                 SetRectEmpty(&Rect);
  227.  
  228.             StartSelection(hWnd, MAKEMPOINT(lParam), &Rect,
  229.                 (wParam & MK_SHIFT) ? SL_EXTEND | Shape : Shape);
  230.             break;
  231.  
  232.         case WM_MOUSEMOVE:
  233.             if (bTrack)
  234.                 UpdateSelection(hWnd, MAKEMPOINT(lParam), &Rect, Shape);
  235.             break;
  236.  
  237.         case WM_LBUTTONUP:
  238.        if (bTrack) 
  239.                EndSelection(MAKEMPOINT(lParam), &Rect);
  240.          bTrack = FALSE;
  241.             break;
  242.  
  243.    case WM_SIZE:
  244.       switch (wParam) {
  245.          case SIZEICONIC:
  246.  
  247.             /* If we aren't in retain mode we want to clear the 
  248.              * current rectangle now! 
  249.              */
  250.             if (!RetainShape)
  251.                SetRectEmpty(&Rect);
  252.       }
  253.       break;
  254.  
  255.         case WM_DESTROY:
  256.             PostQuitMessage(0);
  257.             break;
  258.  
  259.         default:
  260.             return (DefWindowProc(hWnd, message, wParam, lParam));
  261.     }
  262.     return (0);
  263. }
  264.  
  265. /****************************************************************************
  266.  
  267.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  268.  
  269.     PURPOSE:  Processes messages for "About" dialog box
  270.  
  271.     MESSAGES:
  272.  
  273.         WM_INITDIALOG - initialize dialog box
  274.         WM_COMMAND    - Input received
  275.  
  276. ****************************************************************************/
  277.  
  278. BOOL APIENTRY About(
  279.     HWND hDlg,
  280.     UINT message,
  281.     UINT wParam,
  282.     LONG lParam)
  283. {
  284.     switch (message) {
  285.         case WM_INITDIALOG:
  286.             return (TRUE);
  287.  
  288.         case WM_COMMAND:
  289.             // LOWORD added for portability
  290.             if (LOWORD(wParam) == IDOK
  291.              || LOWORD(wParam) == IDCANCEL) {
  292.                 EndDialog(hDlg, TRUE);
  293.                 return (TRUE);
  294.             }
  295.             return (TRUE);
  296.     }
  297.     return (FALSE);
  298.         UNREFERENCED_PARAMETER(lParam);
  299. }
  300.