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