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

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

unix.superglobalmegacorp.com

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