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