home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / cwin / c.exe / $INSTDIR / include / conio.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-12-15  |  4.2 KB  |  201 lines

  1. /* A conio implementation for Mingw/Dev-C++.
  2.  *
  3.  * Written by:
  4.  * Hongli Lai <hongli@telekabel.nl>
  5.  * tkorrovi <tkorrovi@altavista.net> on 2002/02/26. 
  6.  * Andrew Westcott <ajwestco@users.sourceforge.net>
  7.  *
  8.  * Offered for use in the public domain without any warranty.
  9.  */
  10.  
  11. #ifndef _CONIO_C_
  12. #define _CONIO_C_
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <windows.h>
  18. #include <string.h>
  19. #include "conio.h"
  20.  
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24.  
  25. static int __BACKGROUND = BLACK;
  26. static int __FOREGROUND = LIGHTGRAY;
  27.  
  28.  
  29. void
  30. clrscr ()
  31. {
  32.     DWORD written;
  33.  
  34.     FillConsoleOutputAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
  35.       __FOREGROUND + (__BACKGROUND << 4), 2000, (COORD) {0, 0},
  36.       &written);
  37.       FillConsoleOutputCharacter (GetStdHandle
  38.       (STD_OUTPUT_HANDLE), ' ',
  39.       2000, (COORD) {0, 0}, &written);
  40.     gotoxy (1, 1);
  41. }
  42.  
  43.  
  44. void
  45. clreol ()
  46. {
  47.     COORD coord;
  48.     DWORD written;
  49.     CONSOLE_SCREEN_BUFFER_INFO info;
  50.  
  51.     GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),
  52.       &info);
  53.     coord.X = info.dwCursorPosition.X;
  54.     coord.Y = info.dwCursorPosition.Y;
  55.  
  56.     FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE),
  57.       ' ', info.dwSize.X - info.dwCursorPosition.X, coord, &written);
  58.     gotoxy (coord.X, coord.Y);
  59. }
  60.  
  61.  
  62. void
  63. delline()
  64. {
  65.     COORD coord;
  66.     DWORD written;
  67.     CONSOLE_SCREEN_BUFFER_INFO info;
  68.  
  69.     GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),
  70.       &info);
  71.     coord.X = info.dwCursorPosition.X;
  72.     coord.Y = info.dwCursorPosition.Y;
  73.  
  74.     FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE),
  75.       ' ', info.dwSize.X * info.dwCursorPosition.Y, coord, &written);
  76.     gotoxy (info.dwCursorPosition.X + 1,
  77.     info.dwCursorPosition.Y + 1);
  78. }
  79.  
  80.  
  81. int
  82. _conio_gettext (int left, int top, int right, int bottom,
  83.   char *str)
  84. {
  85.     int i, j, n;
  86.     SMALL_RECT r;
  87.     CHAR_INFO buffer[25][80];
  88.  
  89.     r = (SMALL_RECT) {left - 1, top - 1, right - 1, bottom - 1};
  90.     ReadConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
  91.       (PCHAR_INFO) buffer, (COORD) {80, 25}, (COORD) {0, 0}, &r);
  92.  
  93.     lstrcpy (str, "");
  94.     for (i = n = 0; i <= bottom - top; i++)
  95.     for (j = 0; j <= right - left; j++)
  96.     {
  97.         str[n] = buffer[i][j].Char.AsciiChar;
  98.         n++;
  99.     }
  100.     str[n] = 0;
  101.     return 1;
  102. }
  103.  
  104.  
  105. void
  106. gotoxy(int x, int y)
  107. {
  108.   COORD c;
  109.  
  110.   c.X = x - 1;
  111.   c.Y = y - 1;
  112.   SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
  113. }
  114.  
  115.  
  116. void
  117. puttext (int left, int top, int right, int bottom, char *str)
  118.     int i, j, n;
  119.     SMALL_RECT r;
  120.     CHAR_INFO buffer[25][80];
  121.  
  122.     memset (buffer, 0, sizeof (buffer));
  123.     r = (SMALL_RECT) {left - 1, top - 1, right - 1, bottom - 1};
  124.  
  125.     for (i = n = 0; i <= bottom - top; i++)
  126.     for (j = 0; j <= right - left && str[n] != 0; j++)
  127.     {
  128.         buffer[i][j].Char.AsciiChar = str[n];
  129.         buffer[i][j].Attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
  130.         n++;
  131.     }
  132.  
  133.     WriteConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
  134.       (CHAR_INFO *) buffer, (COORD) {80, 25},
  135.       (COORD) {0, 0}, &r);
  136. }
  137.  
  138.  
  139. void
  140. _setcursortype (int type)
  141. {
  142.     CONSOLE_CURSOR_INFO Info;
  143.  
  144.     Info.dwSize = type;
  145.     SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE),
  146.       &Info);
  147. }
  148.  
  149.  
  150. void
  151. textattr (int _attr)
  152. {
  153.     SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), _attr);
  154. }
  155.  
  156.  
  157. void
  158. textbackground (int color)
  159. {
  160.     __BACKGROUND = color;
  161.     SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
  162.       __FOREGROUND + (color << 4));
  163. }
  164.  
  165.  
  166. void
  167. textcolor (int color)
  168. {
  169.     __FOREGROUND = color;
  170.     SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
  171.       color + (__BACKGROUND << 4));
  172. }
  173.  
  174.  
  175. int
  176. wherex ()
  177. {
  178.     CONSOLE_SCREEN_BUFFER_INFO info;
  179.  
  180.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
  181.     return info.dwCursorPosition.X + 1;
  182. }
  183.  
  184.  
  185. int
  186. wherey ()
  187. {
  188.     CONSOLE_SCREEN_BUFFER_INFO info;
  189.  
  190.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
  191.     return info.dwCursorPosition.Y + 1;
  192. }
  193.  
  194.  
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198.  
  199. #endif /* _CONIO_C_ */
  200.