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

unix.superglobalmegacorp.com

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