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 / select.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  204 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: Select.c
  15.  
  16.     PURPOSE: Contains library routines for selecting a region
  17.  
  18.     FUNCTIONS:
  19.  
  20.         StartSelection(HWND, POINT, LPRECT, int) - begin selection area
  21.         UpdateSelection(HWND, POINT, LPRECT, int) - update selection area
  22.         EndSelection(POINT, LPRECT) - end selection area
  23.         ClearSelection(HWND, LPRECT, int) - clear selection area
  24.  
  25. *******************************************************************************/
  26.  
  27. #include "windows.h"
  28. #include "select.h"
  29.  
  30. /****************************************************************************
  31.    FUNCTION: DllMain(HANDLE, DWORD, LPVOID)
  32.  
  33.    PURPOSE:  DllMain is called by Windows when
  34.              the DLL is initialized, Thread Attached, and other times.
  35.              Refer to SDK documentation, as to the different ways this
  36.              may be called.
  37.              
  38.              The DllMain function should perform additional initialization
  39.              tasks required by the DLL.  In this example, no initialization
  40.              tasks are required.  DllMain should return a value of 1 if
  41.              the initialization is successful.
  42.            
  43. *******************************************************************************/
  44. BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved)
  45. {
  46.     return 1;
  47.         UNREFERENCED_PARAMETER(hInst);
  48.         UNREFERENCED_PARAMETER(ul_reason_being_called);
  49.         UNREFERENCED_PARAMETER(lpReserved);
  50. }
  51.  
  52.  
  53. /****************************************************************************
  54.  
  55.     FUNCTION: StartSelection(HWND, POINT, LPRECT, int)
  56.  
  57.     PURPOSE: Begin selection of region
  58.  
  59. ****************************************************************************/
  60.  
  61. INT APIENTRY StartSelection(
  62.     HWND hWnd,
  63.     MPOINT ptCurrent,
  64.     LPRECT lpSelectRect,
  65.     INT fFlags)
  66. {
  67.     if (lpSelectRect->left != lpSelectRect->right ||
  68.             lpSelectRect->top != lpSelectRect->bottom)
  69.         ClearSelection(hWnd, lpSelectRect, fFlags);
  70.  
  71.     lpSelectRect->right = ptCurrent.x;
  72.     lpSelectRect->bottom = ptCurrent.y;
  73.  
  74.     /* If you are extending the box, then invert the current rectangle */
  75.  
  76.     if ((fFlags & SL_SPECIAL) == SL_EXTEND)
  77.         ClearSelection(hWnd, lpSelectRect, fFlags);
  78.  
  79.     /* Otherwise, set origin to current location */
  80.  
  81.     else {
  82.         lpSelectRect->left = ptCurrent.x;
  83.         lpSelectRect->top = ptCurrent.y;
  84.     }
  85.     SetCapture(hWnd);
  86.     return 1;
  87. }
  88.  
  89. /****************************************************************************
  90.  
  91.     FUNCTION: UpdateSelection(HWND, POINT, LPRECT, int) - update selection area
  92.  
  93.     PURPOSE: Update selection
  94.  
  95. ****************************************************************************/
  96.  
  97. INT APIENTRY UpdateSelection(
  98.     HWND hWnd,
  99.     MPOINT ptCurrent,
  100.     LPRECT lpSelectRect,
  101.     INT fFlags)
  102. {
  103.     HDC hDC;
  104.     SHORT OldROP;
  105.  
  106.     hDC = GetDC(hWnd);
  107.  
  108.     switch (fFlags & SL_TYPE) {
  109.  
  110.         case SL_BOX:
  111.             OldROP = (SHORT)SetROP2(hDC, R2_NOTXORPEN);
  112.             MoveToEx(hDC, lpSelectRect->left, lpSelectRect->top, NULL);
  113.             LineTo(hDC, lpSelectRect->right, lpSelectRect->top);
  114.             LineTo(hDC, lpSelectRect->right, lpSelectRect->bottom);
  115.             LineTo(hDC, lpSelectRect->left, lpSelectRect->bottom);
  116.             LineTo(hDC, lpSelectRect->left, lpSelectRect->top);
  117.             LineTo(hDC, ptCurrent.x, lpSelectRect->top);
  118.             LineTo(hDC, ptCurrent.x, ptCurrent.y);
  119.             LineTo(hDC, lpSelectRect->left, ptCurrent.y);
  120.             LineTo(hDC, lpSelectRect->left, lpSelectRect->top);
  121.             SetROP2(hDC, OldROP);
  122.             break;
  123.     
  124.         case SL_BLOCK:
  125.             PatBlt(hDC,
  126.                 lpSelectRect->left,
  127.                 lpSelectRect->bottom,
  128.                 lpSelectRect->right - lpSelectRect->left,
  129.                 ptCurrent.y - lpSelectRect->bottom,
  130.                 DSTINVERT);
  131.             PatBlt(hDC,
  132.                 lpSelectRect->right,
  133.                 lpSelectRect->top,
  134.                 ptCurrent.x - lpSelectRect->right,
  135.                 ptCurrent.y - lpSelectRect->top,
  136.                 DSTINVERT);
  137.             break;
  138.     }
  139.     lpSelectRect->right = ptCurrent.x;
  140.     lpSelectRect->bottom = ptCurrent.y;
  141.     ReleaseDC(hWnd, hDC);
  142.     return 1;
  143. }
  144.  
  145. /****************************************************************************
  146.  
  147.     FUNCTION: EndSelection(POINT, LPRECT)
  148.  
  149.     PURPOSE: End selection of region, release capture of mouse movement
  150.  
  151. ****************************************************************************/
  152.  
  153. INT APIENTRY EndSelection(
  154.     MPOINT ptCurrent,
  155.     LPRECT lpSelectRect)
  156. {
  157.     lpSelectRect->right = ptCurrent.x;
  158.     lpSelectRect->bottom = ptCurrent.y;
  159.     ReleaseCapture();
  160.     return 1;
  161. }
  162.  
  163. /****************************************************************************
  164.  
  165.     FUNCTION: ClearSelection(HWND, LPRECT, int) - clear selection area
  166.  
  167.     PURPOSE: Clear the current selection
  168.  
  169. ****************************************************************************/
  170.  
  171. INT APIENTRY ClearSelection(
  172.     HWND hWnd,
  173.     LPRECT lpSelectRect,
  174.     INT fFlags)
  175. {
  176.     HDC hDC;
  177.     INT2DWORD OldROP;
  178.  
  179.     hDC = GetDC(hWnd);
  180.     switch (fFlags & SL_TYPE) {
  181.  
  182.         case SL_BOX:
  183.             OldROP = SetROP2(hDC, R2_NOTXORPEN);
  184.             MoveToEx(hDC, lpSelectRect->left, lpSelectRect->top, NULL);
  185.             LineTo(hDC, lpSelectRect->right, lpSelectRect->top);
  186.             LineTo(hDC, lpSelectRect->right, lpSelectRect->bottom);
  187.             LineTo(hDC, lpSelectRect->left, lpSelectRect->bottom);
  188.             LineTo(hDC, lpSelectRect->left, lpSelectRect->top);
  189.             SetROP2(hDC, OldROP);
  190.             break;
  191.  
  192.         case SL_BLOCK:
  193.             PatBlt(hDC,
  194.                 lpSelectRect->left,
  195.                 lpSelectRect->top,
  196.                 lpSelectRect->right - lpSelectRect->left,
  197.                 lpSelectRect->bottom - lpSelectRect->top,
  198.                 DSTINVERT);
  199.             break;
  200.     }
  201.     ReleaseDC(hWnd, hDC);
  202.     return 1;
  203. }
  204.