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

1.1     ! root        1: #include <windows.h>
        !             2: #include <stdio.h>
        !             3: #include "console.h"
        !             4: 
        !             5: /*********************************************************************
        !             6: * FUNCTION: demoCursor(HANDLE hConOut)                               *
        !             7: *                                                                    *
        !             8: * PURPOSE: demonstrate GetConsoleCursorInfo, SetConsoleCursorInfo,   *
        !             9: *          and SetConsoleCursorPosition. Show the current cursor     *
        !            10: *          information, then have the cursor follow the mouse around *
        !            11: *          the screen. Shrink the cursor size when the user clicks   *
        !            12: *          one button, and grow the cursor when he clicks the other. *
        !            13: *                                                                    *
        !            14: * INPUT: the console output handle to write to and manipulate the    *
        !            15: *        cursor for.                                                 *
        !            16: *********************************************************************/
        !            17: 
        !            18: void demoCursor(HANDLE hConOut)
        !            19: {
        !            20:   BOOL bSuccess;
        !            21:   /* to set initial size and visibility of cursor */
        !            22:   CONSOLE_CURSOR_INFO cci;
        !            23:   INPUT_RECORD inputBuffer;
        !            24:   DWORD dwInputEvents;
        !            25:   HANDLE hStdIn;
        !            26:   CHAR szTemp[128];
        !            27: 
        !            28:   setConTitle(__FILE__);
        !            29:   myPuts(hConOut, "Current cursor information, as reported by\n"
        !            30:                   "GetConsoleCursorInfo:\n");
        !            31:   bSuccess = GetConsoleCursorInfo(hConOut, &cci);
        !            32:   PERR(bSuccess, "GetConsoleCursorInfo");
        !            33:   sprintf(szTemp, "Cursor size (in percent maximum): %d", cci.dwSize);
        !            34:   myPuts(hConOut, szTemp);
        !            35:   sprintf(szTemp, "Cursor visible: %s", cci.bVisible ? "TRUE" : "FALSE");
        !            36:   myPuts(hConOut, szTemp);
        !            37:   myPuts(hConOut, "\nLet's use SetConsoleCursorPosition to have the console\n"
        !            38:                   "cursor follow the mouse pointer around on the screen.\n"
        !            39:                   "When the left mouse button is clicked, we'll use \n"
        !            40:                   "SetConsoleCursorInfo to increase the cursor size by 10%.\n"
        !            41:                   "When the right mouse button is clicked, the cursor size\n"
        !            42:                   "will be decreased by 10%. To return, hit ESC.");
        !            43:   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
        !            44:   for(;;)
        !            45:     {
        !            46:       /* get an input event */
        !            47:     bSuccess = ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);
        !            48:     PERR(bSuccess, "ReadConsoleInput");
        !            49:     switch (inputBuffer.EventType)
        !            50:       {
        !            51:       case KEY_EVENT:
        !            52:         /* is it a key-down event? Is it an ESC char? If so return */
        !            53:         if (inputBuffer.Event.KeyEvent.bKeyDown &&
        !            54:             inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
        !            55:           return;
        !            56:         break;
        !            57:       case MOUSE_EVENT:
        !            58:         /* if the mouse moved draw the cursor at the mouse position */
        !            59:         if (inputBuffer.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
        !            60:           {
        !            61:           bSuccess = SetConsoleCursorPosition(hConOut,
        !            62:               inputBuffer.Event.MouseEvent.dwMousePosition);
        !            63:           PERR(bSuccess, "SetConsoleCursorPosition");
        !            64:           }
        !            65:         /* if the mouse is clicked, increase/decrease cursor size. */
        !            66:         /* Consider a double click a single click for this sample */
        !            67:         if (!inputBuffer.Event.MouseEvent.dwEventFlags || /* a click */
        !            68:             inputBuffer.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
        !            69:           {
        !            70:           /* is the leftmost mouse button is down? If so, increase cursor */
        !            71:           if (inputBuffer.Event.MouseEvent.dwButtonState ==
        !            72:               FROM_LEFT_1ST_BUTTON_PRESSED)
        !            73:             {
        !            74:             /* if cursor size grows > 100, wrap around to small size */
        !            75:             cci.dwSize = (cci.dwSize + 10) % 100 + 1;
        !            76:             bSuccess = SetConsoleCursorInfo(hConOut, &cci);
        !            77:             PERR(bSuccess, "SetConsoleCursorInfo");
        !            78:             }
        !            79:           /* is the rightmost button is down? */
        !            80:           if (inputBuffer.Event.MouseEvent.dwButtonState ==
        !            81:               RIGHTMOST_BUTTON_PRESSED)
        !            82:             {
        !            83:             /* if cursor size < 0, wrap around to large size */
        !            84:             cci.dwSize -= 10;
        !            85:             if ((int) cci.dwSize < 1)
        !            86:               cci.dwSize = 100;
        !            87:             bSuccess = SetConsoleCursorInfo(hConOut, &cci);
        !            88:             PERR(bSuccess, "SetConsoleCursorInfo");
        !            89:             }
        !            90:           /* other buttons will be ignored */
        !            91:           }
        !            92:         break;
        !            93:       } /* switch */
        !            94:     }  /* while */
        !            95:   return;
        !            96: }

unix.superglobalmegacorp.com

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