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 / create.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  125 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 <stdio.h>
  14. #include "console.h"
  15.  
  16. /*********************************************************************
  17. * FUNCTION: demoCreate(HANDLE hConOld)                               *
  18. *                                                                    *
  19. * PURPOSE: demonstrate CreateConsoleScreenBuffer,                    *
  20. *          SetConsoleTextAttribute, and                              *
  21. *          SetConsoleActiveScreenBuffer. Create a 'help' screen on a *
  22. *          new buffer and quickly switch between the main buffer and *
  23. *          the help buffer on command without redrawing the entire   *
  24. *          screens. SetConsoleTextAttribute will be used to make the *
  25. *          help screen a different color.                            *
  26. *                                                                    *
  27. * INPUT: the output handle to write to                               *
  28. **********************************************************************/
  29.  
  30. void demoCreate(HANDLE hConOld)
  31. {
  32.   BOOL bSuccess;
  33.   HANDLE hConHelp; /* console for the help screen */
  34.   COORD dwWriteCoord = {0, 0}; /* where to write the screen attributes */
  35.   DWORD cCharsWritten;
  36.   HANDLE hStdIn; /* standard input handle */
  37.   CHAR c = 0; /* virtual key code that we will read */
  38.   HANDLE hConCurrent; /* keep track of the current visible console buffer */
  39.   INPUT_RECORD inputBuf; /* console input event record */
  40.   DWORD cInputEvents;
  41.  
  42.   setConTitle(__FILE__);
  43.   myPuts(hConOld, "Let's create a help screen in another buffer. Then\n"
  44.                   "when the user hits F1, we can easily flip to the help\n"
  45.                   "screen and back without recreating the text on the\n"
  46.                   "screens by simply changing the active buffer.\n"
  47.                   "We'll change the default text attribute with\n"
  48.                   "SetConsoleTextAttribute before creating the help screen\n"
  49.                   "so that the sample text is in a different color.\n"
  50.                   "Hit F1 now for help, and esc to return here.\n"
  51.                   "Hit esc from this screen to return to API list.");
  52.   /* create a separate console buffer for the help screen */
  53.   hConHelp = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  54.       FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER,
  55.       NULL);
  56.   PERR(hConHelp != INVALID_HANDLE_VALUE, "CreateConsoleScreenBuffer");
  57.   /* change the color of the help screen */
  58.   bSuccess = FillConsoleOutputAttribute(hConHelp, BACKGROUND_BLUE,
  59.       getConX(hConHelp) * getConY(hConHelp), dwWriteCoord, &cCharsWritten);
  60.   PERR(bSuccess, "FillConsoleOutputAttribute");
  61.   /* set the color for future text output */
  62.   bSuccess = SetConsoleTextAttribute(hConHelp, FOREGROUND_YELLOW |
  63.       FOREGROUND_INTENSITY | BACKGROUND_BLUE);
  64.   PERR(bSuccess, "SetConsoleTextAttribute");
  65.   myPuts(hConHelp, "                S u p e r D u p e r B a s e   H e l p\n"
  66.                    "\n\n                  F1: search        F6: window\n"
  67.                    "                  F2: extract       F7: initialize\n"
  68.                    "                  F3: sort          F8: exit\n"
  69.                    "                  F4: query         F9: save\n"
  70.                    "                  F5: execute       F10: explode\n"
  71.                    "                  ESC: exit help");
  72.   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  73.   PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
  74.   /* keep track of the currently visible console */
  75.   hConCurrent = hConOld;
  76.   /* switch between the help and previous buffer when user hits F1 or ESC */
  77.   for(;;)
  78.     {
  79.     do
  80.       {
  81.       /* throw away any non-keystroke events or any key-up events */
  82.       bSuccess = ReadConsoleInput(hStdIn, &inputBuf, 1, &cInputEvents);
  83.       PERR(bSuccess, "ReadConsoleInput");
  84.       } while (inputBuf.EventType != KEY_EVENT ||
  85.           !inputBuf.Event.KeyEvent.bKeyDown);
  86.     /* get the virtual scan code of the key-down event */
  87.     c = (char) inputBuf.Event.KeyEvent.wVirtualKeyCode;
  88.     switch(c)
  89.       {
  90.       case VK_F1:
  91.         /* if the current buffer is the original buffer, switch to the */
  92.         /* help buffer */
  93.         if (hConCurrent == hConOld)
  94.           {
  95.           bSuccess = SetConsoleActiveScreenBuffer(hConHelp);
  96.           PERR(bSuccess, "SetConsoleActiveScreenBuffer");
  97.           hConCurrent = hConHelp;
  98.           }
  99.         break;
  100.       case VK_ESCAPE:
  101.         /* if the current buffer is the help buffer, switch to the */
  102.         /* original buffer. Otherwise, clean up and return */
  103.         if (hConCurrent == hConHelp)
  104.           {
  105.           bSuccess = SetConsoleActiveScreenBuffer(hConOld);
  106.           PERR(bSuccess, "SetConsoleActiveScreenBuffer");
  107.           hConCurrent = hConOld;
  108.           }
  109.         else
  110.           {
  111.           CloseHandle(hConHelp);
  112.           return;
  113.           }
  114.         break;
  115.       case VK_F10:
  116.         if (hConCurrent == hConHelp)
  117.           myPuts(hConHelp, "BOOM!");
  118.         break;
  119.       default:
  120.         break;
  121.       }  /* switch */
  122.     }  /* while */
  123.   return;
  124. }
  125.