home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d020_1_4 / 6.ddi / SELECT / DEMO.C next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  6.8 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.     HANDLE hMemory;
  89.     PWNDCLASS pWndClass;
  90.     BOOL bSuccess;
  91.  
  92.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  93.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  94.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  95.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  96.     pWndClass->lpszMenuName = (LPSTR) "Menu";
  97.     pWndClass->lpszClassName = (LPSTR) "Demo";
  98.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  99.     pWndClass->hInstance = hInstance;
  100.     pWndClass->style = NULL;
  101.     pWndClass->lpfnWndProc = DemoWndProc;
  102.  
  103.     bSuccess = RegisterClass(pWndClass);
  104.  
  105.     LocalUnlock(hMemory);
  106.     LocalFree(hMemory);
  107.     return (bSuccess);
  108. }
  109.  
  110. /****************************************************************************
  111.  
  112.     FUNCTION: DemoWndProc(HWND, unsigned, WORD, LONG)
  113.  
  114.     PURPOSE:  Processes messages
  115.  
  116.     MESSAGES:
  117.  
  118.     WM_SYSCOMMAND - system menu (About dialog box)
  119.     WM_CREATE     - create window
  120.     WM_DESTROY    - destroy window
  121.     WM_LBUTTONDOWN - left mouse button
  122.     WM_MOUSEMOVE   - mouse movement
  123.     WM_LBUTTONUP   - left button released
  124.  
  125.     WM_COMMAND messages:
  126.         IDM_BOX    - use inverted box for selecting a region
  127.         IDM_BLOCK  - use empty box for selecting a region
  128.         IDM_RETAIN - retain/delete selection on button release
  129.  
  130.     COMMENTS:
  131.  
  132.     When the left mouse button is pressed, btrack is set to TRUE so that
  133.     the code for WM_MOUSEMOVE will keep track of the mouse and update the
  134.     box accordingly.  Once the button is released, btrack is set to
  135.     FALSE, and the current position is saved.  Holding the SHIFT key
  136.     while pressing the left button will extend the current box rather
  137.     then erasing it and starting a new one.     The exception is when the
  138.     retain shape option is enabled.     With this option, the rectangle is
  139.     zeroed whenever the mouse is released so that it can not be erased or
  140.     extended.
  141.  
  142. ****************************************************************************/
  143.  
  144. long FAR PASCAL DemoWndProc(hWnd, message, wParam, lParam)
  145. HWND hWnd;
  146. unsigned message;
  147. WORD wParam;
  148. LONG lParam;
  149. {
  150.     FARPROC lpProcAbout;
  151.     HMENU hMenu;
  152.  
  153.     switch (message) {
  154.  
  155.     case WM_COMMAND:
  156.         switch (wParam) {
  157.         case IDM_BOX:
  158.             Shape = SL_BOX;
  159.             hMenu = GetMenu(hWnd);
  160.             CheckMenuItem(hMenu, IDM_BOX, MF_CHECKED);
  161.             CheckMenuItem(hMenu, IDM_BLOCK, MF_UNCHECKED);
  162.             break;
  163.  
  164.         case IDM_BLOCK:
  165.             Shape = SL_BLOCK;
  166.             hMenu = GetMenu(hWnd);
  167.             CheckMenuItem(hMenu, IDM_BOX, MF_UNCHECKED);
  168.             CheckMenuItem(hMenu, IDM_BLOCK, MF_CHECKED);
  169.             break;
  170.  
  171.         case IDM_RETAIN:
  172.             if (RetainShape) {
  173.             hMenu = GetMenu(hWnd);
  174.             CheckMenuItem(hMenu, IDM_RETAIN, MF_UNCHECKED);
  175.             RetainShape = FALSE;
  176.             }
  177.             else {
  178.             hMenu = GetMenu(hWnd);
  179.             CheckMenuItem(hMenu, IDM_RETAIN, MF_CHECKED);
  180.             RetainShape = TRUE;
  181.             }
  182.             break;
  183.  
  184.         case IDM_ABOUT:
  185.             lpProcAbout = MakeProcInstance(About, hInst);
  186.             DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  187.             FreeProcInstance(lpProcAbout);
  188.             break;
  189.  
  190.         }
  191.         break;
  192.  
  193.     case WM_LBUTTONDOWN:
  194.  
  195.         bTrack = TRUE;         /* user has pressed the left button */
  196.  
  197.         /* If you don't want the shape cleared, you must clear the Rect
  198.          * coordinates before calling StartSelection
  199.          */
  200.  
  201.         if (RetainShape)
  202.         SetRectEmpty(&Rect);
  203.  
  204.         StartSelection(hWnd, MAKEPOINT(lParam), &Rect,
  205.         (wParam & MK_SHIFT) ? SL_EXTEND | Shape : Shape);
  206.         break;
  207.  
  208.     case WM_MOUSEMOVE:
  209.         if (bTrack)
  210.         UpdateSelection(hWnd, MAKEPOINT(lParam), &Rect, Shape);
  211.         break;
  212.  
  213.     case WM_LBUTTONUP:
  214.        if (bTrack) 
  215.            EndSelection(MAKEPOINT(lParam), &Rect);
  216.         bTrack = FALSE;
  217.         break;
  218.  
  219.    case WM_SIZE:
  220.       switch (wParam) {
  221.          case SIZEICONIC:
  222.  
  223.             /* If we aren't in retain mode we want to clear the 
  224.              * current rectangle now! 
  225.              */
  226.             if (!RetainShape)
  227.                SetRectEmpty(&Rect);
  228.       }
  229.       break;
  230.  
  231.     case WM_DESTROY:
  232.         PostQuitMessage(NULL);
  233.         break;
  234.  
  235.     default:
  236.         return (DefWindowProc(hWnd, message, wParam, lParam));
  237.     }
  238.     return (NULL);
  239. }
  240.  
  241. /****************************************************************************
  242.  
  243.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  244.  
  245.     PURPOSE:  Processes messages for "About" dialog box
  246.  
  247.     MESSAGES:
  248.  
  249.     WM_INITDIALOG - initialize dialog box
  250.     WM_COMMAND    - Input received
  251.  
  252. ****************************************************************************/
  253.  
  254. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  255. HWND hDlg;
  256. unsigned message;
  257. WORD wParam;
  258. LONG lParam;
  259. {
  260.     switch (message) {
  261.     case WM_INITDIALOG:
  262.         return (TRUE);
  263.  
  264.     case WM_COMMAND:
  265.         if (wParam == IDOK
  266.                 || wParam == IDCANCEL) {
  267.         EndDialog(hDlg, TRUE);
  268.         return (TRUE);
  269.         }
  270.         return (TRUE);
  271.     }
  272.     return (FALSE);
  273. }
  274. 
  275.