home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / console / readchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  3.8 KB  |  94 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. #include <windows.h>
  13. #include <string.h>
  14. #include <malloc.h>
  15. #include "console.h"
  16.  
  17. /*********************************************************************
  18. * FUNCTION: demoReadConChar(HANDLE hConOut)                          *
  19. *                                                                    *
  20. * PURPOSE: demonstrate ReadConsoleOutputCharacter. Read the text on  *
  21. *          line that the user clicks on and output it to the console *
  22. *                                                                    *
  23. * INPUT: the console output handle to write to                       *
  24. *********************************************************************/
  25.  
  26. void demoReadConChar(HANDLE hConOut)
  27. {
  28.   BOOL bSuccess;
  29.   INPUT_RECORD inputBuffer;
  30.   DWORD dwStdInMode;
  31.   HANDLE hStdIn;
  32.   DWORD dwInputEvents;
  33.   COORD coordLine; /* coordinates of where to read characters from */
  34.   CHAR *szLine;  /* buffer to hold the line read from the console */
  35.   DWORD dwCharsRead;
  36.   int i;
  37.  
  38.   setConTitle(__FILE__);
  39.   myPuts(hConOut, "Click on any line containing characters. I will use\n"
  40.                   "ReadConsoleOutputCharacter to read that line of text into\n"
  41.                   "a buffer, then print that buffer to the console at the\n"
  42.                   "current cursor position. Hit ESC to return.\n\n");
  43.   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  44.   PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
  45.   /* save the console mode */
  46.   bSuccess = GetConsoleMode(hStdIn, &dwStdInMode);
  47.   PERR(bSuccess, "GetConsoleMode");
  48.   /* enable mouse input */
  49.   bSuccess = SetConsoleMode(hStdIn, dwStdInMode | ENABLE_MOUSE_INPUT);
  50.   PERR(bSuccess, "SetConsoleMode");
  51.   /* allocate space for one line */
  52.   szLine = (char *) malloc(getConX(hConOut));
  53.   PERR(szLine, "malloc");
  54.   for(;;)
  55.     {
  56.     /* get a single input event */
  57.     bSuccess = ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);
  58.     PERR(bSuccess, "ReadConsoleInput");
  59.     switch (inputBuffer.EventType)
  60.       {
  61.       case KEY_EVENT:
  62.         /* is it an ESC key? */
  63.         if (inputBuffer.Event.KeyEvent.bKeyDown &&
  64.             inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
  65.           {
  66.           /* set input mode back to what it was originally and return */
  67.           bSuccess = SetConsoleMode(hStdIn, dwStdInMode);
  68.           PERR(bSuccess, "SetConsoleMode");
  69.           free(szLine); /* free allocated space for a text line */
  70.           return;
  71.           }
  72.         break;
  73.       case MOUSE_EVENT:
  74.         /* was this was a click event? Is any button down or not? */
  75.         if (inputBuffer.Event.MouseEvent.dwEventFlags != MOUSE_MOVED &&
  76.             inputBuffer.Event.MouseEvent.dwButtonState)
  77.           {
  78.           /* read the line where the mouse is, starting at column 0 */
  79.           coordLine.X = 0;
  80.           coordLine.Y = inputBuffer.Event.MouseEvent.dwMousePosition.Y;
  81.           bSuccess = ReadConsoleOutputCharacter(hConOut, szLine,
  82.               getConX(hConOut), coordLine, &dwCharsRead);
  83.           PERR(bSuccess, "ReadConsoleOutputCharacter");
  84.           /* strip trailing spaces */
  85.           i = getConX(hConOut) - 1;
  86.           szLine[i--] = 0; /* null terminate */
  87.           while (szLine[i] == ' ')
  88.             szLine[i--] = 0;
  89.           myPuts(hConOut, szLine);
  90.           }
  91.       } /* switch */
  92.     } /* while */
  93. }
  94.