BOOL WriteConsole(<br> HANDLE hConsoleOutput, // handle to screen buffer<br> CONST VOID *lpBuffer, // write buffer<br> DWORD nNumberOfCharsToWrite, // number of characters to write<br> LPDWORD lpNumberOfCharsWritten, // number of characters written<br> LPVOID lpReserved // reserved<br>);<br>这个API函数可以高定!注意是控制台程序,不是简单的C/c++程序<br>下面这个小程序在至于MSDN<br>#include <windows.h> <br> <br>void NewLine(void); <br>void ScrollScreenBuffer(HANDLE, INT); <br> <br>HANDLE hStdout, hStdin; <br>CONSOLE_SCREEN_BUFFER_INFO csbiInfo; <br> <br>void main(void) <br>{ <br> LPSTR lpszPrompt1 = "Type something and press Enter:/n"; <br> LPSTR lpszPrompt2 = "Type any key: "; <br> CHAR chBuffer[256]; <br> DWORD cRead, cWritten, fdwMode, fdwOldMode; <br> WORD wOldColorAttrs; <br><br> // Get handles to STDIN and STDOUT. <br><br> hStdin = GetStdHandle(STD_INPUT_HANDLE); <br> hStdout = GetStdHandle(STD_OUTPUT_HANDLE); <br> if (hStdin == INVALID_HANDLE_VALUE || <br> hStdout == INVALID_HANDLE_VALUE) <br> {<br> MyErrorExit("GetStdHandle"); <br> }<br><br> // Save the current text colors. <br><br> if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) <br> MyErrorExit("GetConsoleScreenBufferInfo"); <br><br> wOldColorAttrs = csbiInfo.wAttributes; <br><br> // Set the text attr. to draw red text on black background. <br><br> if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED)) <br> MyErrorExit("SetConsoleTextAttribute"); <br><br> // Write to STDOUT and read from STDIN by using the default <br> // modes. Input is echoed automatically, and ReadFile <br> // does not return until a carriage return is typed. <br> // <br> // The default input modes are line, processed, and echo. <br> // The default output modes are processed and wrap at EOL. <br><br> while (1) <br> { <br> if (! WriteFile( <br> hStdout, // output handle <br> lpszPrompt1, // prompt string <br> lstrlen(lpszPrompt1), // string length <br> &cWritten, // bytes written <br> NULL) ) // not overlapped <br> break; <br> if (! ReadFile( <br> hStdin, // input handle <br> chBuffer, // buffer to read into <br> 255, // size of buffer <br> &cRead, // actual bytes read <br> NULL) ) // not overlapped <br> break; <br> if (chBuffer[0] == 'q') break; <br> } <br><br> // Turn off the line input mode, and echo the input mode. <br><br> if (! GetConsoleMode(hStdin, &fdwOldMode)) <br> MyErrorExit("GetConsoleMode"); <br><br> fdwMode = fdwOldMode & <br> ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); <br> if (! SetConsoleMode(hStdin, fdwMode)) <br> MyErrorExit("SetConsoleMode"); <br><br> // Prompt for input. <br><br> if (! WriteFile( <br> hStdout, // output handle <br> lpszPrompt2, // prompt string <br> lstrlen(lpszPrompt2), // string length <br> &cWritten, // bytes written <br> NULL) ) // not overlapped <br> MyErrorExit("WriteFile"); <br><br> // Without line and echo input modes, ReadFile returns <br> // when any input is available. Carriage returns must <br> // be handled, and WriteFile is used to echo input. <br><br> while (1) <br> { <br> if (! ReadFile(hStdin, chBuffer, 1, &cRead, NULL)) <br> break; <br> if (chBuffer[0] == '/r') <br> NewLine(); <br> else if (! WriteFile(hStdout, chBuffer, cRead, <br> &cWritten, NULL)) break; <br> if (chBuffer[0] == 'q') break; <br> } <br><br> // Restore the original console mode. <br><br> if (! SetConsoleMode(hStdin, fdwOldMode)) <br> MyErrorExit("SetConsoleMode"); <br><br> // Restore the original text colors. <br><br> if (! SetConsoleTextAttribute(hStdout, wOldColorAttrs)) <br> MyErrorExit("SetConsoleTextAttribute"); <br>}<br><br>// The NewLine function handles carriage returns when the processed <br>// input mode is disabled. It gets the current cursor position <br>// and resets it to the first cell of the next row. <br> <br>void NewLine(void) <br>{ <br> if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) <br> MyErrorExit("GetConsoleScreenBufferInfo"); <br> csbiInfo.dwCursorPosition.X = 0; <br><br> // If it is the last line in the screen buffer, scroll <br> // the buffer up. <br><br> if ((csbiInfo.dwSize.Y-1) == csbiInfo.dwCursorPosition.Y) <br> { <br> ScrollScreenBuffer(hStdout, 1); <br> } <br><br> // Otherwise, advance the cursor to the next line. <br><br> else csbiInfo.dwCursorPosition.Y += 1; <br> <br> if (! SetConsoleCursorPosition(hStdout, <br> csbiInfo.dwCursorPosition)) <br> {<br> MyErrorExit("SetConsoleCursorPosition"); <br> }<br>} <br>