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

1.1       root        1: #include <windows.h>
                      2: #include <stdio.h>
                      3: #include <string.h>
                      4: #include "console.h"
                      5: 
1.1.1.2 ! root        6: /* Microsoft Developer Support
        !             7:    Copyright (c) 1992 Microsoft Corporation */
        !             8: 
1.1       root        9: /* number of colors in the color bar */
                     10: #define MAXCOLORS 16
                     11: /* horizontal size in characters of the sample text area */
                     12: #define SAMPTEXTX 33
                     13: /* veritcal size in characters of the sample text area */
                     14: #define SAMPTEXTY 5
                     15: 
                     16: /********************************************************************
                     17: * FUNCTION: demoFillAtt(HANDLE hConOut)                             *
                     18: *                                                                   *
                     19: * PURPOSE: demonstrate FillConsoleOutputAttribute and               *
                     20: *          WriteConsoleOutputCharacter. Create a console version of *
                     21: *          the "ScreenSize..." menu item from the console system    *
                     22: *          menu. Allow the user to select a foreground and          *
                     23: *          background color, which will be immediately displayed in *
                     24: *          the sample text box                                      *
                     25: *                                                                   *
                     26: * INPUT: the console output handle to write to                      *
                     27: ********************************************************************/
                     28: 
                     29: void demoFillAtt(HANDLE hConOut)
                     30: {
                     31:   COORD foregLoc = {5, 5}; /* foreground 'button' location */
                     32:   COORD backgLoc = {5, 7}; /* background 'button' location */
                     33:   COORD okLoc = {20, 5}; /* ok 'button' location */
                     34:   COORD colorbarLoc = {6, 10};  /* loc of first color in colorbar */
                     35:   COORD sampleLoc = {5, 13}; /* sample text location */
                     36:   COORD dwBufCoord; /* temp COORD structure */
                     37:   BOOL bSuccess;
                     38:   PCHAR szForeg = "� Screen Text"; /* foreground 'button' and text */
                     39:   PCHAR szBackg = "� Screen Background"; /* background 'button' */
                     40:   PCHAR szOk = "� OK"; /* OK 'button' */
                     41:   /* these strings are the box around the color bar */
                     42:   PCHAR szColors1 = "��������������������������������Ŀ";
                     43:   PCHAR szColors2 = "�                                �";
                     44:   PCHAR szColors3 = "����������������������������������";
                     45:   WORD wButtonColor; /* holds the button 'color' */
                     46:   DWORD dwCharsWritten;
                     47:   WORD i;
                     48:   INPUT_RECORD inputBuf;
                     49:   DWORD cInputEvents;
                     50:   BOOL bForeground; /* state flag: foreground button active? */
                     51:   COORD wCurPos;
                     52:   HANDLE hStdIn;
                     53:   /* arraw of attributes to put in the colorbar, each attribute in 2 spaces */
                     54:   WORD szAttr[MAXCOLORS * 2];
                     55:   PCHAR szSampText = "A console consists of a keyboard and mouse input buffer"
                     56:                   " and one or more screen buffers.  \"CONIN$\" refers to the"
                     57:                   " input buffer. Stdin is a handle to \"CONIN$\".  \"CONOUT$\""
                     58:                   " refers to a screen buffer.";
                     59:   PCHAR p;
                     60:   WORD wCurSampAttr; /* current attribute of the sample text area */
                     61: 
                     62:   setConTitle(__FILE__);
                     63:   /* place our "buttons" and text on the screen. First the 'foreground' */
                     64:   /* button... */
                     65:   bSuccess = WriteConsoleOutputCharacter(hConOut, szForeg, strlen(szForeg),
                     66:       foregLoc, &dwCharsWritten);
                     67:   /* now the 'background' button */
                     68:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     69:   bSuccess = WriteConsoleOutputCharacter(hConOut, szBackg, strlen(szBackg),
                     70:       backgLoc, &dwCharsWritten);
                     71:   /* place the 'OK' button */
                     72:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     73:   bSuccess = WriteConsoleOutputCharacter(hConOut, szOk, strlen(szOk),
                     74:       okLoc, &dwCharsWritten);
                     75:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     76:   /* now let's draw a boxed colorbar */
                     77:   dwBufCoord = colorbarLoc;
                     78:   /* move up and left one to draw a box around the colorbar */
                     79:   dwBufCoord.X--;
                     80:   dwBufCoord.Y--;
                     81:   /* the top of the box */
                     82:   bSuccess = WriteConsoleOutputCharacter(hConOut, szColors1, strlen(szColors1),
                     83:       dwBufCoord, &dwCharsWritten);
                     84:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     85:   dwBufCoord.Y++;
                     86:   /* the middle of the box */
                     87:   bSuccess = WriteConsoleOutputCharacter(hConOut, szColors2, strlen(szColors2),
                     88:       dwBufCoord, &dwCharsWritten);
                     89:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     90:   dwBufCoord.Y++;
                     91:   /* the bottom of the box */
                     92:   bSuccess = WriteConsoleOutputCharacter(hConOut, szColors3, strlen(szColors3),
                     93:       dwBufCoord, &dwCharsWritten);
                     94:   PERR(bSuccess, "WriteConsoleOutputCharacter");
                     95: 
                     96:   /* color the "buttons" a different color */
                     97:   wButtonColor = FOREGROUND_WHITE | BACKGROUND_WHITE;
                     98:   /* color the 'backgroud' button */
                     99:   bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    100:       1, backgLoc, &dwCharsWritten);
                    101:   PERR(bSuccess, "WriteConsoleOutputAttribute");
                    102:   /* color the 'OK' button */
                    103:   bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    104:       1, okLoc, &dwCharsWritten);
                    105:   PERR(bSuccess, "WriteConsoleOutputAttribute");
                    106:   /* color the 'foreground' button, but with a different color */
                    107:   wButtonColor = BACKGROUND_WHITE;
                    108:   bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    109:       1, foregLoc, &dwCharsWritten);
                    110:   PERR(bSuccess, "WriteConsoleOutputAttribute");
                    111:   bForeground = TRUE; /* the 'foreground' button is 'highlighted' */
                    112: 
                    113:   /* construct the attribute string */
                    114:   for (i = 0; i < MAXCOLORS; i++)
                    115:     /* for each set of two, the color attributes are in the high byte */
                    116:     /* of the word - shift them into the high byte */
                    117:     szAttr[i * 2] = szAttr[(i * 2) + 1] = i << 4;
                    118:   /* write out the attributes at the colorbar location */
                    119:   dwBufCoord = colorbarLoc;
                    120:   bSuccess = WriteConsoleOutputAttribute(hConOut, szAttr, MAXCOLORS * 2,
                    121:       dwBufCoord, &dwCharsWritten);
                    122:   PERR(bSuccess, "WriteConsoleOutputAttribute");
                    123: 
                    124:   /* put up a sample text area */
                    125:   p = szSampText;
                    126:   wCurPos = sampleLoc;
                    127:   wCurSampAttr = BACKGROUND_WHITE;
                    128:   for (i = 0; i < SAMPTEXTY; i++)
                    129:     {
                    130:     bSuccess = WriteConsoleOutputCharacter(hConOut, p,
                    131:         min(strlen(p), SAMPTEXTX), wCurPos, &dwCharsWritten);
                    132:     PERR(bSuccess, "WriteConsoleOutputCharacter");
                    133:     /* color the text with the current sample attribute */
                    134:     bSuccess = FillConsoleOutputAttribute(hConOut, wCurSampAttr, SAMPTEXTX,
                    135:         wCurPos, &dwCharsWritten);
                    136:     PERR(bSuccess, "FillConsoleOutputAttribute");
                    137:     wCurPos.Y++;
                    138:     /* advance pointer to the next row of characters in the sample string */
                    139:     p += min(strlen(p), SAMPTEXTX);
                    140:     }
                    141: 
                    142:   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.2 ! root      143:   PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
1.1       root      144:   for(;;)
                    145:     {
                    146:     /* get an input event */
                    147:     bSuccess = ReadConsoleInput(hStdIn, &inputBuf, 1, &cInputEvents);
                    148:     /* if it's a mouse event but not a mouse move, it's a click */
                    149:     PERR(bSuccess, "ReadConsoleInput");
                    150:     if (inputBuf.EventType == MOUSE_EVENT &&
                    151:         inputBuf.Event.MouseEvent.dwEventFlags != MOUSE_MOVED)
                    152:       {
                    153:       wCurPos = inputBuf.Event.MouseEvent.dwMousePosition;
                    154:       /* is the mouse on the 'foreground' button? */
                    155:       if (wCurPos.X == foregLoc.X && wCurPos.Y == foregLoc.Y)
                    156:         {
                    157:         bForeground = TRUE;
                    158:         /* recolor the background color and foreground color */
                    159:         /* of the foreground to show the foreground button active */
                    160:         wButtonColor = BACKGROUND_WHITE;
                    161:         bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    162:             1, foregLoc, &dwCharsWritten);
                    163:         PERR(bSuccess, "WriteConsoleOutputAttribute");
                    164:         /* recolor the background color to show as inactive */
                    165:         wButtonColor = FOREGROUND_WHITE | BACKGROUND_WHITE;
                    166:         bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    167:             1, backgLoc, &dwCharsWritten);
                    168:         PERR(bSuccess, "WriteConsoleOutputAttribute");
                    169:         }
                    170:       /* is the mouse on the 'background' button? */
                    171:       if (wCurPos.X == backgLoc.X && wCurPos.Y == backgLoc.Y)
                    172:         {
                    173:         bForeground = FALSE;
                    174:         /* recolor the background button to show as active */
                    175:         wButtonColor = BACKGROUND_WHITE;
                    176:         bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    177:             1, backgLoc, &dwCharsWritten);
                    178:         PERR(bSuccess, "WriteConsoleOutputAttribute");
                    179:         /* recolor the foreground button to show as inactive */
                    180:         wButtonColor = FOREGROUND_WHITE | BACKGROUND_WHITE;
                    181:         bSuccess = WriteConsoleOutputAttribute(hConOut, &wButtonColor,
                    182:             1, foregLoc, &dwCharsWritten);
                    183:         PERR(bSuccess, "WriteConsoleOutputAttribute");
                    184:         }
                    185:       /* is the mouse on the 'OK' button? */
                    186:       if (wCurPos.X == okLoc.X && wCurPos.Y == okLoc.Y)
                    187:         break;
                    188:       /* is the mouse in the colorbar? */
                    189:       if (wCurPos.Y == colorbarLoc.Y && wCurPos.X >= colorbarLoc.X &&
                    190:           wCurPos.X < colorbarLoc.X + (SHORT) (MAXCOLORS * 2))
                    191:         {
                    192:         /* get the screen attribute at the mouse position */
                    193:         bSuccess = ReadConsoleOutputAttribute(hConOut, &wButtonColor,
                    194:             sizeof(wButtonColor), wCurPos, &dwCharsWritten);
                    195:         PERR(bSuccess, "ReadConsoleOutputAttribute");
                    196:         /* if the foreground button is active, set the foreground */
                    197:         /* attribute for the sample text */
                    198:         if (bForeground)
                    199:           /* mask off the foreground color and 'or' it with the color */
                    200:           /* we just got from the screen, shifted up to make it a */
                    201:           /* foreground color (it's actually a background attribute when */
                    202:           /* read from the console buffer). */
                    203:           wCurSampAttr = (wCurSampAttr & (WORD) 0xF0) | (wButtonColor >> 4);
                    204:         /* otherwise set the background attribute for the sample text */
                    205:         else
                    206:           /* mask off the background color and 'or'  it with the background */
                    207:           /* color we just read from the console. */
                    208:           wCurSampAttr = (wCurSampAttr & (WORD) 0x0F) | wButtonColor;
                    209:         /* now set the attribute of the sample text to the new attribute */
                    210:         wCurPos = sampleLoc;
                    211:         for (i = 0; i < SAMPTEXTY; i++)
                    212:           {
                    213:           bSuccess = FillConsoleOutputAttribute(hConOut, wCurSampAttr,
                    214:               SAMPTEXTX, wCurPos, &dwCharsWritten);
                    215:           PERR(bSuccess, "FillConsoleOutputAttribute");
                    216:           wCurPos.Y++;
                    217:           }
                    218:         }  /* if */
                    219:       }  /* if */
                    220:     }  /* while */
                    221:   return;
                    222: }

unix.superglobalmegacorp.com

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