Annotation of mstools/samples/console/create.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.