-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouleur.h
571 lines (500 loc) · 17.6 KB
/
couleur.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Fonction permettant l'impression de caractères en couleur sous Windows
// développédans le cadre du mini-projet 1 du cours d'algorithmique/langage
// Département informatique - IUT d'Orsay
//
#ifndef _WINMP1_H
#define _WINMP1_H
void textcolor(int _color);
void imp(char c)
{
textcolor(7);
switch (c)
{
case 'R' :
textcolor(12);
break; // LIGHTRED
case 'V' :
textcolor(10);
break; // LGHTGREEN
case 'J' :
textcolor(14);
break; // YELLOW
case 'M' :
textcolor(13);
break; // LIGHTMAGENTA
case 'B' :
textcolor(9);
break; // LIGHTBLUE
case 'C' :
textcolor(11);
break; // LIGHTCYAN
}
cout << c << " ";
textcolor(7);
}
void impBis(string c)
{
textcolor(7);
if(c == "Rouge")
textcolor(12); // LIGHTRED
else if (c == "Vert")
textcolor(10); // LGHTGREEN
else if (c == "Jaune")
textcolor(14); // YELLOW
else if (c == "Magenta")
textcolor(13); // LIGHTMAGENTA
else if (c == "Bleu")
textcolor(9); // LIGHTBLUE
else if (c == "Cyan")
textcolor(11); // LIGHTCYAN
cout << c << " ";
textcolor(7);
}
// Nom du fichier : myconio.h
// Auteur : [email protected]
// Version : Novembre 2004
// Objectif : remédier à l'absence de certaines fonctions conio sous DevC++
/* Remarques :
- Dev-C++ possède déjà une bibliothèque conio dans laquelle les fonctions
« getch », « getche », « kbhit », « putch » et « ungetch » sont codées.
- « textmode n'a pas été réécrite car les modes d'affichage prévus sous DOS
n'ont pas de sens dans une fenêtre console Windows.
- « window » n'a pas pu être écrite... seule une fonction « MYclrwin » a été
codée pour remplacer l'appel de « window » suivi d'un « clrscr ».
Toutefois la zone d'écriture à l'écran n'est pas limitée.
- « MYwindow » redimensionne la fenêtre de la console.
A noter : le comportement de cette fonction n'a aucun rapport avec celui
de « window » de conio.
*/
/*#ifndef _MYCONIO_H_
#define _MYCONIO_H_
*/
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <wincon.h>
#ifdef __cplusplus
extern "C" {
#endif
struct text_info
{
unsigned char winleft;
unsigned char wintop;
unsigned char winright;
unsigned char winbottom;
unsigned char attribute;
unsigned char normattr;
unsigned char currmode;
unsigned char screenheight;
unsigned char screenwidth;
unsigned char curx;
unsigned char cury;
};
enum COLORS { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED,
LIGHTMAGENTA, YELLOW, WHITE
} ;
#define _NOCURSOR 0
#define _SOLIDCURSOR 1
#define _NORMALCURSOR 2
#define C40 1
void _setcursortype(int type);
char *cgets(char *str);
void clreol();
void clrscr();
void delline();
#define cprintf printf
#define cscanf scanf
/* Les fonctions « cprintf » et « cscanf » n'ont pas été réécrites, ceci est
peut-être une erreur...
En effet, « scanf » lit les données à partir de « stdin » et les fonctions
d'entrée de la console lisent les données à partir de « STD_INPUT_HANDLE ».
Or il semblerait que « fflush(stdin) ; » n'ait pas le même effet que
« FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)) ; »...
A suivre... */
int cputs(const char *str) ;
int Gettext(int left, int top, int right, int bottom, void *destin) ;
#define gettext Gettext
/* Cette astuce a dû être utilisée car la ligne
« int gettext(int left, int top, int right, int bottom, void *destin) ; »
produit un message d'erreur (sous Dev-C++) ! */
void gettextinfo(struct text_info *_r);
int MYgetch(void) ;
int MYgetche(void) ;
/* « getch », « getche » sont déjà définies dans <conio.h> (mingw). Cependant,
au même titre que « getchar » (stdio) ne consomme pas le caractère <CR>,
elles ne consomment pas le caractère saisi par l'utilisateur (c'est à dire :
elles n'enlèvent pas le caractère du buffer d'entrée). « MYgetch »,
« MYgetche » vident le buffer. Attention, « MYgetch » et « MYgetche »
n'enlèvent pas un caractère, mais tous les caractères disponibles. Grâce
aux directives « #define » inclus dans MYconio.cpp, ce sont les fonctions
« MYgetch » et « MYgetche » qui seront appelées si dans un source on fait
appel à « getch » ou « getche ». */
void gotoxy(int x, int y);
void highvideo();
void insline();
// kbhit : OK déjà défini dans <conio.h> (mingw)
void lowvideo();
int movetext(int left, int top, int right, int bottom, int destleft, int desttop);
void normvideo();
// putch : OK déjà défini dans <conio.h> (mingw)
int puttext(int left, int top, int right, int bottom, void *source);
void textattr(int _attr);
void textbackground(int _color);
//void textcolor(int _color);
/* BLINK (clignotement) ne fonctionne pas ! Par contre, il est possible
d'utiliser les oucleurs LIGHT pour le fond d'écran, ce qui n'est pas le cas
pour une application DOS.*/
/* « void textmode(int _mode); » n'a pas été réécrite, car les modes
d'affichage DOS n'ont aucun sens dans une application console Windows. */
// ungetch : OK déjà défini dans <conio.h> (mingw)
int wherex();
int wherey();
void MYclrwin(int left, int top, int right, int bottom); // efface une zone
/* La fonction « window » de conio est fréquemment utilisée (suivi de
« clrscr ») pour effacer une zone de l'écran et modifier sa couleur en
fonction des attribus mis à jour par « textcolor » ou « textbackground ».
« MYclrwin » permet cela, mais elle n'interdit pas d'écrire en dehors
de cette zone.*/
void MYwindow(int left, int top, int right, int bottom);
/* « MYwindow » permet de redimensionner la fenêtre de la console. Cependant,
ce comportement, n'est pas celui de la fonction « window » de conio */
// void window(int left, int top, int right, int bottom);
#ifdef __cplusplus
}
#endif
// #include "myconio.cpp"
// Nom du fichier : myconio.cpp
// Auteur : [email protected]
// Version : Novembre 2004
// Objectif : remédier à l'absence de certaines fonctions conio sous DevC++
/* Remarques :
- Ce fichier est inclus par myconio.h.
- Au sein d'un fichier source seule la directive « #include <myconio.h> »
est nécessaire. Elle remplace « #include <conio.h> »... La directive
« #include <conio.cpp> » est inutile.
- Pour obtenir des informations sur les fonctions de la bibliothèque window
(wincon, winbase) utilisés dans ce fichier,
il faut consulter « http://www.msdn.microsoft.com/library/default.asp ».
*/
//#ifndef _MYCONIO_C_
//#define _MYCONIO_C_
#include <iostream>
using namespace std;
void _setcursortype(int type)
{
CONSOLE_CURSOR_INFO Info;
Info.bVisible = TRUE;
if (type == _NOCURSOR) Info.bVisible = FALSE;
else if (type == _SOLIDCURSOR) Info.dwSize = 90 ;
else if (type == _NORMALCURSOR) Info.dwSize = 1;
SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &Info);
}
char *cgets(char *str)
{
int i=2 ;
while((str[i]=MYgetch()) != '\r')
{
switch(str[i])
{
case '\b' :
if(i<=2) break ; // pas de caractère à effacer
printf("\b \b") ;
i -- ;
break ;
case 0 :
case -32 :
MYgetch() ; // lire le 2ème caractère des codes étendus
case 9 : // ne pas tenir compte de la touche [TAB]
case 27 :
break ; // ne pas tenir compte de la touche [ESC]
default :
if(i<=str[0])
putch(str[i++]) ;
}
}
str[i] = 0 ;
str[1] = i - 2 ;
return str + 2 ;
}
void clreol()
{
COORD coord = {wherex() - 1 , wherey() - 1};
DWORD dwWritten;
HANDLE hScreen=GetStdHandle(STD_OUTPUT_HANDLE) ;
CONSOLE_SCREEN_BUFFER_INFO Info;
if(GetConsoleScreenBufferInfo(hScreen, &Info))
{
FillConsoleOutputCharacter (hScreen, ' ', Info.dwSize.X-coord.X, coord,
&dwWritten);
FillConsoleOutputAttribute (hScreen, Info.wAttributes,
Info.dwSize.X-coord.X, coord, &dwWritten);
gotoxy (coord.X, coord.Y) ;
}
}
void clrscr(void) // efface l'écran
{
// Première solution : OK !
COORD coord = { 0, 0 } ; // coord.X = 0; coord.Y = 0;
DWORD dwWritten, dwSize ;
HANDLE hScreen=GetStdHandle(STD_OUTPUT_HANDLE) ;
CONSOLE_SCREEN_BUFFER_INFO Info;
if(GetConsoleScreenBufferInfo(hScreen, &Info))
{
dwSize = Info.dwSize.X * Info.dwSize.Y ;
FillConsoleOutputCharacter (hScreen, ' ', dwSize, coord, &dwWritten);
FillConsoleOutputAttribute (hScreen, Info.wAttributes, dwSize, coord,
&dwWritten);
gotoxy (1, 1);
}
/* Seconde solution : moins efficace - nécessite plus de temps
- ne prend pas en compte les couleurs */
/*
system("cls") ;
*/
}
int cputs(const char *str)
{
return ((printf ("%s\n", str)==EOF)?EOF:(int)*(str+strlen(str)-1)) ;
}
void delline()
{
int y=wherey() ;
HANDLE hScreen = GetStdHandle(STD_OUTPUT_HANDLE) ;
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(hScreen, &Info) ;
{
COORD dwBufferSize= {Info.dwSize.X, Info.dwSize.Y - y} ;
COORD dwBufferCoord= {0, 0} ;
CHAR_INFO Buffer[dwBufferSize.Y][dwBufferSize.X] ;
SMALL_RECT ReadRegion[2] ;
ReadRegion[0].Left = 0 ;
ReadRegion[0].Right = Info.dwSize.X-1 ;
ReadRegion[0].Top = y ;
ReadRegion[0].Bottom = Info.dwSize.Y-1 ;
ReadConsoleOutput(hScreen, (CHAR_INFO*)Buffer, dwBufferSize,
dwBufferCoord, ReadRegion) ;
ReadRegion[0].Top = y-1 ;
ReadRegion[0].Bottom = Info.dwSize.Y-2 ;
WriteConsoleOutput(hScreen, (CHAR_INFO*)Buffer, dwBufferSize,
dwBufferCoord, ReadRegion) ;
}
}
int MYgetch(void)
{
char Temp[3] ;
int Ret=getch() ;
if (Ret==0x0D) gets(Temp) ;
/* Si la touche choisie par l'utilisateur est <CR> alors
« FlushConsoleInputBuffer » ne fonctionne pas d'où l'astuce avec gets. */
else FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)) ;
/* « fflush(stdin) ; » ne fonctionne pas ! Sans effet apparent ! */
return Ret ;
}
int MYgetche(void)
{
char Temp[3] ;
int Ret=getche() ;
if (Ret==0x0D) gets(Temp) ;
else FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)) ;
return Ret ;
}
#define getch MYgetch // permet de remplacer getch (conio, mingw) par MYgetch
#define getche MYgetche
int Gettext(int left, int top, int right, int bottom, void *destin)
{
int ValueReturn, x, y ;
HANDLE hScreen = GetStdHandle(STD_OUTPUT_HANDLE) ;
COORD dwBufferSize= {right-left+1, bottom-top+1} ;
COORD dwBufferCoord= {0,0} ;
CHAR_INFO Buffer[dwBufferSize.Y][dwBufferSize.X] ;
SMALL_RECT ReadRegion[2] ;
ReadRegion[0].Left = left-1 ;
ReadRegion[0].Right = right-1 ;
ReadRegion[0].Top = top-1 ;
ReadRegion[0].Bottom = bottom-1 ;
ValueReturn=ReadConsoleOutput(hScreen, (CHAR_INFO*) Buffer, dwBufferSize,
dwBufferCoord, ReadRegion) ;
for (x=0 ; x<dwBufferSize.X ; x++)
{
for (y=0 ; y<dwBufferSize.Y ; y++)
{
*((char*)destin+(2*(dwBufferSize.X*y+x)))=Buffer[y][x].Char.AsciiChar ;
*((char*)destin+(2*(dwBufferSize.X*y+x)+1))=Buffer[y][x].Attributes ;
}
}
return ValueReturn ;
}
void gettextInfo(struct text_info *_r)
{
CONSOLE_SCREEN_BUFFER_INFO Info;
GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &Info);
_r->winleft = Info.srWindow.Left ;
_r->winright = Info.srWindow.Right ;
_r->wintop = Info.srWindow.Top ;
_r->winbottom = Info.srWindow.Bottom ;
_r->attribute = Info.wAttributes ;
_r->normattr = LIGHTGRAY | (BLACK<<4) ;
_r->currmode = C40 ;
_r->screenheight = Info.dwSize.Y ;
_r->screenwidth = Info.dwSize.X ;
_r->curx = wherex () ;
_r->cury = wherey () ;
}
void gotoxy(int x, int y)
{
COORD c ;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void highvideo()
{
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info) ;
textattr(Info.wAttributes | 0x08);
}
void insline()
{
int y=wherey() ;
HANDLE hScreen = GetStdHandle(STD_OUTPUT_HANDLE) ;
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(hScreen, &Info) ;
{
COORD dwBufferSize= {Info.dwSize.X, Info.dwSize.Y - y} ;
COORD dwBufferCoord= {0,0} ;
COORD coord= {0, y-1} ; // pour FillConsoleOutputCharacter
CHAR_INFO Buffer[dwBufferSize.Y][dwBufferSize.X] ;
DWORD dwWritten;
SMALL_RECT ReadRegion[2] ;
ReadRegion[0].Left = 0 ;
ReadRegion[0].Right = Info.dwSize.X-1 ;
ReadRegion[0].Top = y-1 ;
ReadRegion[0].Bottom = Info.dwSize.Y-2 ;
ReadConsoleOutput(hScreen, (CHAR_INFO*)Buffer, dwBufferSize, dwBufferCoord, ReadRegion) ;
ReadRegion[0].Top = y ;
ReadRegion[0].Bottom = Info.dwSize.Y-1 ;
WriteConsoleOutput(hScreen, (CHAR_INFO*)Buffer, dwBufferSize, dwBufferCoord, ReadRegion) ;
FillConsoleOutputCharacter (hScreen, ' ', Info.dwSize.X, coord, &dwWritten); // Effacer ligne
FillConsoleOutputAttribute (hScreen, Info.wAttributes, Info.dwSize.X, coord, &dwWritten);
}
}
void lowvideo()
{
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info) ;
textattr(Info.wAttributes & 0xF7);
}
int movetext(int left, int top, int right, int bottom, int destleft, int desttop)
{
// int ValueReturn, x, y ;
HANDLE hScreen = GetStdHandle(STD_OUTPUT_HANDLE) ;
COORD dwBufferSize= {right-left+1, bottom-top+1} ;
COORD dwBufferCoord= {0,0} ;
CHAR_INFO Buffer[dwBufferSize.Y][dwBufferSize.X] ;
SMALL_RECT ReadRegion[2] ;
ReadRegion[0].Left = left-1 ;
ReadRegion[0].Right = right-1 ;
ReadRegion[0].Top = top-1 ;
ReadRegion[0].Bottom = bottom-1 ;
ReadConsoleOutput(hScreen, (CHAR_INFO*) Buffer, dwBufferSize, dwBufferCoord,
ReadRegion) ;
ReadRegion[0].Left = destleft-1 ;
ReadRegion[0].Right = destleft-1+(dwBufferSize.X-1) ;
ReadRegion[0].Top = desttop-1 ;
ReadRegion[0].Bottom = desttop-1+(dwBufferSize.Y-1) ;
return WriteConsoleOutput(hScreen, (CHAR_INFO*) Buffer, dwBufferSize,
dwBufferCoord, ReadRegion) ;
}
void normvideo()
{
textattr(0x07) ; // LIGHTGRAY
}
int puttext(int left, int top, int right, int bottom, void *source)
{
int x, y ;
HANDLE hScreen = GetStdHandle(STD_OUTPUT_HANDLE) ;
COORD dwBufferSize= {right-left+1, bottom-top+1} ;
COORD dwBufferCoord= {0,0} ;
CHAR_INFO Buffer[dwBufferSize.Y][dwBufferSize.X] ;
SMALL_RECT ReadRegion[2] ;
ReadRegion[0].Left = left-1 ;
ReadRegion[0].Right = right-1 ;
ReadRegion[0].Top = top-1 ;
ReadRegion[0].Bottom = bottom-1 ;
for (x=0 ; x<dwBufferSize.X ; x++)
{
for (y=0 ; y<dwBufferSize.Y ; y++)
{
Buffer[y][x].Char.AsciiChar = *((char*)source+(2*(dwBufferSize.X*y+x))) ;
Buffer[y][x].Attributes = *((char*)source+(2*(dwBufferSize.X*y+x)+1)) ;
}
}
return WriteConsoleOutput(hScreen, (CHAR_INFO*) Buffer, dwBufferSize,
dwBufferCoord, ReadRegion) ;
}
void textattr(int _attr)
{
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), _attr);
}
void textbackground(int _color)
{
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info) ;
textattr((Info.wAttributes & 0x0F) | (_color<<4));
}
void textcolor(int _color)
{
CONSOLE_SCREEN_BUFFER_INFO Info ;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info) ;
textattr((Info.wAttributes & 0xF0) | _color);
}
int wherex()
{
CONSOLE_SCREEN_BUFFER_INFO Info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
return Info.dwCursorPosition.X + 1 ;
}
int wherey()
{
CONSOLE_SCREEN_BUFFER_INFO Info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
return Info.dwCursorPosition.Y + 1 ;
}
void MYclrwin(int left, int top, int right, int bottom)
// efface une zone de l'écran
{
int i ;
COORD dwCoord = {0, 0} ;
COORD dwSize = {(right-left+1),(bottom-top+1)} ;
HANDLE hScreen=GetStdHandle(STD_OUTPUT_HANDLE) ;
CHAR_INFO Buffer[dwSize.X*dwSize.Y] ;
SMALL_RECT Region[2] ;
CONSOLE_SCREEN_BUFFER_INFO Info;
if(GetConsoleScreenBufferInfo(hScreen, &Info))
{
for(i=0 ; i < dwSize.X*dwSize.Y ; i++)
{
Buffer[i].Char.AsciiChar=' ' ;
Buffer[i].Attributes = Info.wAttributes ;
}
Region[0].Left = left-1 ;
Region[0].Right = right-1 ;
Region[0].Top = top-1 ;
Region[0].Bottom = bottom-1 ;
WriteConsoleOutput(hScreen, (CHAR_INFO*) Buffer, dwSize, dwCoord, Region) ;
gotoxy (left, top);
}
}
void MYwindow(int left, int top, int right, int bottom)
{
SMALL_RECT R = {left, top, right, bottom } ;
SetConsoleWindowInfo (GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &R);
}
//#endif //_MYCONIO_C_
// #endif //_MYCONIO_H_
#endif _WINMP1_H // _WINMP1_H_