home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / textfx / guide.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  5KB  |  124 lines

  1. /*****************************************************************************\
  2. *       This is a part of the Microsoft Source Code Samples.
  3. *       Copyright (C) 1992-1997 Microsoft Corporation.
  4. *       All rights reserved.
  5. *       This source code is only intended as a supplement to
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the
  8. *       Microsoft samples programs.
  9. \*****************************************************************************/
  10. /*****************************************************************************
  11.  *                                                                             *
  12.  *    FX.C                                                                      *
  13.  *                                                                             *
  14.  *    PURPOSE:  Routines for rendering text with stange effects                *
  15.  *                                                                             *
  16.  *  GetGuideLine       - Retrieves a guide line from the user.  A guide line *
  17.  *                         is just an array of line segments.                    *
  18.  *                                                                             *
  19.  *  ShowGuide           - Displays a guide line into a DC                     *
  20.  *                                                                             *
  21.  *****************************************************************************/
  22.  
  23. #include <windows.h>
  24. #include "guide.h"
  25.  
  26. /**********************************************************************
  27.  *                                                                    *
  28.  * FUNCTION:  GetGuideLine(HWND, LPPOINT, LPDWORD)                    *
  29.  *                                                                      *
  30.  * PURPOSE:   Gets a guide line that is entered by the user.  This is *
  31.  *            initiated on a WM_LBUTTONDOWN.                          *
  32.  *                                                                    *
  33.  *********************************************************************/
  34. BOOL GetGuideLine(HWND hWnd, LPPOINT *lpPoint, LPDWORD lpdwNumPts)
  35. {
  36.     MSG msg;
  37.     HDC hDC = GetDC(hWnd);
  38.     BOOL bFirstTime = TRUE;
  39.     DWORD dwPos = 0;
  40.     RECT rect;
  41.  
  42.     SetCapture(hWnd);
  43.     GetClientRect(hWnd, &rect);
  44.     
  45.     // Allocate enough room for a reasonable line
  46.     *lpPoint = (LPPOINT)GlobalAlloc(GPTR, MAXGUIDESEGMENTS * sizeof(POINT));
  47.  
  48.     /* Eat mouse messages until a WM_LBUTTONUP is encountered. Meanwhile
  49.      * continue to draw a line that follows the mouse
  50.      */
  51.     for (;;){
  52.         WaitMessage();
  53.         if (PeekMessage(&msg,NULL,WM_MOUSEFIRST,WM_MOUSELAST,PM_REMOVE)) {
  54.                         
  55.             // Make sure we are still tracking in our client area
  56.             if ((LOWORD(msg.lParam) < rect.right) && (HIWORD(msg.lParam) < rect.bottom)) {
  57.             
  58.                 // Set the CP to our starting position if we are starting
  59.                 if (bFirstTime) {
  60.                   bFirstTime = FALSE;
  61.                   MoveToEx(hDC, LOWORD(msg.lParam), HIWORD(msg.lParam), NULL);
  62.                 }                
  63.                           
  64.                 // If we dont have more points than we want...
  65.                 if (dwPos < MAXGUIDESEGMENTS) {
  66.                 
  67.                     // Store the point in our array
  68.                     (*lpPoint)[dwPos].x = LOWORD(msg.lParam);
  69.                     (*lpPoint)[dwPos].y = HIWORD(msg.lParam);
  70.                 
  71.                     // Draw from the last point to this one
  72.                     LineTo(hDC, (*lpPoint)[dwPos].x, (*lpPoint)[dwPos].y);
  73.         
  74.                     // Increment our "number of points" counter
  75.                     dwPos++;
  76.                 }
  77.             }
  78.  
  79.             // Bail when the user lets the left button up
  80.             if (msg.message == WM_LBUTTONUP)
  81.                break;                       
  82.         }
  83.         else
  84.             continue;
  85.     }
  86.  
  87.     *lpdwNumPts = dwPos;
  88.  
  89.     ReleaseDC(hWnd, hDC);
  90.    
  91.     ReleaseCapture();
  92.  
  93.     return TRUE;
  94. }
  95.  
  96. /**********************************************************************
  97.  *                                                                    *
  98.  * FUNCTION:  ShowGuide(HDC, LPPOINT, DWORD)                          *
  99.  *                                                                      *
  100.  * PURPOSE:   Draws a guide line into a DC                              *
  101.  *                                                                    *
  102.  *********************************************************************/
  103. BOOL ShowGuide(HDC hDC, LPPOINT lpPoints, DWORD dwNumPts)
  104. {
  105.   HPEN hOldBrush;
  106.    
  107.   // Draw the line
  108.   Polyline(hDC, lpPoints, dwNumPts);
  109.  
  110.   // Draw green circle to indicate line starting point
  111.   hOldBrush = SelectObject(hDC, CreateSolidBrush(RGB(0,255,0))); 
  112.   Ellipse(hDC, lpPoints[0].x-5, lpPoints[0].y-5, 
  113.                lpPoints[0].x+5, lpPoints[0].y+5);
  114.   
  115.   // Draw red circle to indicate line ending point
  116.   DeleteObject(SelectObject(hDC, CreateSolidBrush(RGB(255,0,0)))); 
  117.   Ellipse(hDC, lpPoints[dwNumPts-1].x-5, lpPoints[dwNumPts-1].y-5, 
  118.                lpPoints[dwNumPts-1].x+5, lpPoints[dwNumPts-1].y+5);
  119.   DeleteObject(SelectObject(hDC, hOldBrush)); 
  120.   
  121.   return TRUE;
  122. }
  123.  
  124.