السلام عليكم ،
لا توجد التوابع 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 (قم بازالتها ولاحظ الفرق) ..
- // console.cpp : Defines the entry point for the console application.
- /////////////////////////////////////////////////////////////////////////
- //
- // This program demonstrates how to implement
- // the next functions in VC++ :
- //
- // void textbackground( COLORS newcolor ); // Set the text background color
- // void textcolor( COLORS newcolor ); // Set the text foreground color
- // int wherex( void ); // Return the current x location of the cursor
- // int wherey( void ); // Return the current y location of the cursor
- // void gotoxy( int x, int y ); // Set the coordinate of the cursor
- //
- // Programmed by : Hani Atassi (atassi@arabteam2000.com)
- //
- #include < windows.h >
- #include < stdio.h >
- #include < io.h >
- #include < fcntl.h >
- #include < iostream.h > // for cout only
- /* Standard error macro for reporting API errors */
- #define PERR(bSuccess, api){if(!(bSuccess))
- class MyConsole {
- public:
- MyConsole();
- ~MyConsole();
- enum COLORS {
- /* dark colors */
- BLACK = 0,
- BLUE = FOREGROUND_BLUE,
- GREEN = FOREGROUND_GREEN,
- CYAN = FOREGROUND_BLUE|FOREGROUND_GREEN,
- RED = FOREGROUND_RED,
- MAGENTA = FOREGROUND_RED|FOREGROUND_BLUE,
- BROWN = FOREGROUND_RED|FOREGROUND_GREEN,
- LIGHTGRAY = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,
- /* light colors */
- DARKGRAY = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,
- LIGHTBLUE = FOREGROUND_INTENSITY|FOREGROUND_BLUE,
- LIGHTGREEN = FOREGROUND_INTENSITY|FOREGROUND_GREEN,
- LIGHTCYAN = FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN,
- LIGHTRED = FOREGROUND_INTENSITY|FOREGROUND_RED,
- LIGHTMAGENTA = FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE,
- YELLOW = FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN,
- WHITE = FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
- };
- void clrscr( void ); // Clear the screen
- void textbackground( COLORS newcolor ); // Set the text background color
- void textcolor( COLORS newcolor ); // Set the text foreground color
- int wherex( void ); // Return the current x location of the cursor
- int wherey( void ); // Return the current y location of the cursor
- void gotoxy( int x, int y ); // Set the coordinate of the cursor
- private:
- WORD m_CurrentColor;
- HANDLE m_hConsole;
- void RedirectStdOut();
- };
- MyConsole::MyConsole()
- {
- m_CurrentColor = 0;
- RedirectStdOut();
- m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- }
- MyConsole::~MyConsole()
- {
- // Do nothing
- }
- void MyConsole::RedirectStdOut()
- {
- int hCrt;
- hCrt = _open_osfhandle(
- (long) GetStdHandle(STD_OUTPUT_HANDLE),
- _O_TEXT
- );
- hf = _fdopen( hCrt, "w" );
- *stdout = *hf;
- setvbuf(stdout, NULL, _IONBF, 0 );
- }
- void MyConsole::clrscr( void )
- {
- COORD coordScreen = { 0, 0 }; /* here's where we'll home the
- cursor */
- BOOL bSuccess;
- DWORD cCharsWritten;
- CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
- DWORD dwConSize; /* number of character cells in
- the current buffer */
- /* get the number of character cells in the current buffer */
- bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
- PERR( bSuccess, "GetConsoleScreenBufferInfo" );
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- /* fill the entire screen with blanks */
- bSuccess = FillConsoleOutputCharacter( m_hConsole, (TCHAR) ' ',
- dwConSize, coordScreen, &cCharsWritten );
- PERR( bSuccess, "FillConsoleOutputCharacter" );
- /* get the current text attribute */
- bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
- PERR( bSuccess, "ConsoleScreenBufferInfo" );
- /* now set the buffer's attributes accordingly */
- bSuccess = FillConsoleOutputAttribute( m_hConsole, csbi.wAttributes,
- dwConSize, coordScreen, &cCharsWritten );
- PERR( bSuccess, "FillConsoleOutputAttribute" );
- /* put the cursor at (0, 0) */
- bSuccess = SetConsoleCursorPosition( m_hConsole, coordScreen );
- PERR( bSuccess, "SetConsoleCursorPosition" );
- return;
- }
- void MyConsole::textbackground( COLORS __newcolor )
- {
- BOOL bSuccess;
- m_CurrentColor &= ~0xf0;
- m_CurrentColor |= __newcolor << 4;
- bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);
- PERR( bSuccess, "SetConsoleTextAttribute" );
- }
- void MyConsole::textcolor( COLORS __newcolor )
- {
- BOOL bSuccess;
- m_CurrentColor &= ~0x0f;
- m_CurrentColor |= __newcolor;
- bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);
- PERR( bSuccess, "SetConsoleTextAttribute" );
- }
- int MyConsole::wherex( void )
- {
- BOOL bSuccess;
- CONSOLE_SCREEN_BUFFER_INFO cinfo;
- bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
- PERR( bSuccess, "GetConsoleScreenBufferInfo" );
- return cinfo.dwCursorPosition.X;
- }
- int MyConsole::wherey( void )
- {
- BOOL bSuccess;
- CONSOLE_SCREEN_BUFFER_INFO cinfo;
- bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
- PERR( bSuccess, "GetConsoleScreenBufferInfo" );
- return cinfo.dwCursorPosition.Y;
- }
- void MyConsole::gotoxy( int x, int y )
- {
- BOOL bSuccess;
- PERR( bSuccess, "SetConsoleCursorPosition" );
- }
- ///////////////////////////////////////////
- // The main program
- int main(int argc, char* argv[])
- {
- MyConsole cons;
- cons.clrscr();
- cons.textcolor(MyConsole::BLUE);
- cons.textbackground(MyConsole::RED);
- cons.textcolor(MyConsole::YELLOW);
- cons.textbackground(MyConsole::LIGHTBLUE);
- cout << "test - cout" << endl;
- for (int i = 0; i < 1000; i++)
- {
- cons.gotoxy(10, 10);
- for (int j = 0; j < 0xFFFFFF; j++)
- ; // delay loop
- }
- return 0;
- }