الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

بعض التوابع التي تحتاجها في ال Console

مغلق
بدأه هاني الأتاسي في 30 نوفمبر 2001 · 1 رد · 1,075 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ،

لا توجد التوابع clrscr و setcolor و gotoxy و wherex و wherey في مكتبة conio.h في ال Visual C++ .

ولكن في الحقيقة توجد توابع WIN32 API يمكن تشغيلها من أجل فعل السابق . وأنا بزماناتي كاتب توابع من اجل اختيار لون النص وخلفية النص وايضا لتغير موقع المشير x, y و من أجل مسح الشاشة :

بالنسبة لروتين مسح الشاشة فهو مأخوذ من ال MSDN من المقالة : Q99261 (HOWTO: Performing Clear Screen (CLS) in a Console Application )

والبرنامج التالي يوجد فيه class قمت ببرمجة التوابع السابقة فيه . مثال عى استخدام الclass في التابع main حيث تجد مثال على استخدام ال printf وال cout .. لاحظ استخدام flush بعد cout (قم بازالتها ولاحظ الفرق) ..

  1.  
  2. // console.cpp : Defines the entry point for the console application.
  3. /////////////////////////////////////////////////////////////////////////
  4. //
  5. // This program demonstrates how to implement
  6. // the next functions in VC++ :
  7. //
  8. // void textbackground( COLORS newcolor ); // Set the text background color
  9. // void textcolor( COLORS newcolor ); // Set the text foreground color
  10. // int wherex( void ); // Return the current x location of the cursor
  11. // int wherey( void ); // Return the current y location of the cursor
  12. // void gotoxy( int x, int y ); // Set the coordinate of the cursor
  13. //
  14. // Programmed by : Hani Atassi (atassi@arabteam2000.com)
  15. //
  16.  
  17. #include < windows.h >
  18. #include < stdio.h >
  19. #include < io.h >
  20. #include < fcntl.h >
  21. #include < iostream.h > // for cout only
  22.  
  23. /* Standard error macro for reporting API errors */
  24. #define PERR(bSuccess, api){if(!(bSuccess))
  25. printf("%s:Error %d from %s on line %dn", __FILE__, GetLastError(), api, __LINE__);}
  26.  
  27. class MyConsole {
  28. public:
  29. MyConsole();
  30. ~MyConsole();
  31.  
  32. enum COLORS {
  33. /* dark colors */
  34. BLACK = 0,
  35. BLUE = FOREGROUND_BLUE,
  36. GREEN = FOREGROUND_GREEN,
  37. CYAN = FOREGROUND_BLUE|FOREGROUND_GREEN,
  38. RED = FOREGROUND_RED,
  39. MAGENTA = FOREGROUND_RED|FOREGROUND_BLUE,
  40. BROWN = FOREGROUND_RED|FOREGROUND_GREEN,
  41. LIGHTGRAY = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,
  42. /* light colors */
  43. DARKGRAY = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,
  44. LIGHTBLUE = FOREGROUND_INTENSITY|FOREGROUND_BLUE,
  45. LIGHTGREEN = FOREGROUND_INTENSITY|FOREGROUND_GREEN,
  46. LIGHTCYAN = FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN,
  47. LIGHTRED = FOREGROUND_INTENSITY|FOREGROUND_RED,
  48. LIGHTMAGENTA = FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE,
  49. YELLOW = FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN,
  50. WHITE = FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
  51. };
  52.  
  53. void clrscr( void ); // Clear the screen
  54.  
  55. void textbackground( COLORS newcolor ); // Set the text background color
  56. void textcolor( COLORS newcolor ); // Set the text foreground color
  57. int wherex( void ); // Return the current x location of the cursor
  58. int wherey( void ); // Return the current y location of the cursor
  59. void gotoxy( int x, int y ); // Set the coordinate of the cursor
  60.  
  61. private:
  62. WORD m_CurrentColor;
  63. HANDLE m_hConsole;
  64.  
  65. void RedirectStdOut();
  66. };
  67.  
  68. MyConsole::MyConsole()
  69. {
  70. m_CurrentColor = 0;
  71.  
  72. RedirectStdOut();
  73.  
  74. m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  75. }
  76.  
  77. MyConsole::~MyConsole()
  78. {
  79. // Do nothing
  80. }
  81.  
  82. void MyConsole::RedirectStdOut()
  83. {
  84. int hCrt;
  85. FILE *hf;
  86.  
  87. hCrt = _open_osfhandle(
  88. (long) GetStdHandle(STD_OUTPUT_HANDLE),
  89. _O_TEXT
  90. );
  91. hf = _fdopen( hCrt, "w" );
  92. *stdout = *hf;
  93. setvbuf(stdout, NULL, _IONBF, 0 );
  94. }
  95.  
  96. void MyConsole::clrscr( void )
  97. {
  98. COORD coordScreen = { 0, 0 }; /* here's where we'll home the
  99.   cursor */
  100. BOOL bSuccess;
  101. DWORD cCharsWritten;
  102. CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  103. DWORD dwConSize; /* number of character cells in
  104.   the current buffer */
  105.  
  106. /* get the number of character cells in the current buffer */
  107.  
  108. bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
  109. PERR( bSuccess, "GetConsoleScreenBufferInfo" );
  110. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  111.  
  112. /* fill the entire screen with blanks */
  113.  
  114. bSuccess = FillConsoleOutputCharacter( m_hConsole, (TCHAR) ' ',
  115. dwConSize, coordScreen, &cCharsWritten );
  116. PERR( bSuccess, "FillConsoleOutputCharacter" );
  117.  
  118. /* get the current text attribute */
  119.  
  120. bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
  121. PERR( bSuccess, "ConsoleScreenBufferInfo" );
  122.  
  123. /* now set the buffer's attributes accordingly */
  124.  
  125. bSuccess = FillConsoleOutputAttribute( m_hConsole, csbi.wAttributes,
  126. dwConSize, coordScreen, &cCharsWritten );
  127. PERR( bSuccess, "FillConsoleOutputAttribute" );
  128.  
  129. /* put the cursor at (0, 0) */
  130.  
  131. bSuccess = SetConsoleCursorPosition( m_hConsole, coordScreen );
  132. PERR( bSuccess, "SetConsoleCursorPosition" );
  133.  
  134. return;
  135. }
  136.  
  137. void MyConsole::textbackground( COLORS __newcolor )
  138. {
  139. BOOL bSuccess;
  140. m_CurrentColor &= ~0xf0;
  141. m_CurrentColor |= __newcolor << 4;
  142.  
  143. bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);
  144. PERR( bSuccess, "SetConsoleTextAttribute" );
  145. }
  146.  
  147. void MyConsole::textcolor( COLORS __newcolor )
  148. {
  149. BOOL bSuccess;
  150. m_CurrentColor &= ~0x0f;
  151. m_CurrentColor |= __newcolor;
  152.  
  153. bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);
  154. PERR( bSuccess, "SetConsoleTextAttribute" );
  155. }
  156.  
  157. int MyConsole::wherex( void )
  158. {
  159. BOOL bSuccess;
  160. CONSOLE_SCREEN_BUFFER_INFO cinfo;
  161.  
  162. bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
  163. PERR( bSuccess, "GetConsoleScreenBufferInfo" );
  164.  
  165. return cinfo.dwCursorPosition.X;
  166. }
  167.  
  168. int MyConsole::wherey( void )
  169. {
  170. BOOL bSuccess;
  171. CONSOLE_SCREEN_BUFFER_INFO cinfo;
  172.  
  173. bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
  174. PERR( bSuccess, "GetConsoleScreenBufferInfo" );
  175.  
  176. return cinfo.dwCursorPosition.Y;
  177. }
  178.  
  179. void MyConsole::gotoxy( int x, int y )
  180. {
  181. BOOL bSuccess;
  182. COORD pos = {x, y};
  183.  
  184. bSuccess = SetConsoleCursorPosition(m_hConsole, pos);
  185. PERR( bSuccess, "SetConsoleCursorPosition" );
  186. }
  187.  
  188. ///////////////////////////////////////////
  189. // The main program
  190.  
  191. int main(int argc, char* argv[])
  192. {
  193. MyConsole cons;
  194.  
  195. printf("123");
  196.  
  197. cons.clrscr();
  198.  
  199. cons.textcolor(MyConsole::BLUE);
  200. cons.textbackground(MyConsole::RED);
  201.  
  202. printf("test - BLUE ON REDn");
  203.  
  204. cons.textcolor(MyConsole::YELLOW);
  205. cons.textbackground(MyConsole::LIGHTBLUE);
  206.  
  207. cout << "test - cout" << endl;
  208.  
  209. for (int i = 0; i < 1000; i++)
  210. {
  211. cons.gotoxy(10, 10);
  212. cout << "test - YELLOW ON BLUE " << i << flush;
  213.  
  214. for (int j = 0; j < 0xFFFFFF; j++)
  215. ; // delay loop
  216. }
  217.  
  218. return 0;
  219. }
  220.  

تم تعديل هذه المشاركة بواسطة Xacker في 26 مارس 2009 في 22:23

Coding on the Cloud and for the Cloud!

My Blog

#2

هاني مو على أساس أنك مو فاضي ؟؟!!:P

الله يعطيك العافية على هال Class , بس ياريت تكون مبسوط هلأ.

هذا الموضوع مغلق.

مواضيع مشابهة