home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 9B < prev    next >
Encoding:
Text File  |  1991-10-24  |  6.7 KB  |  275 lines

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