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

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

unix.superglobalmegacorp.com

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