给个例子研究研究吧:<br>来自 :pitts 时间 :2000-11-6 21:15:00 <br>一个控制台程序,读一个二进制文件,处理后,向屏幕输出。。。 <br>关键的问题就是:如何向控制台输出流??? <br>搞不定啊:((( <br><br>来自 :xiao.lit 时间 :2000-11-06 21:18:00 <br>cout << "haha just kidding" << endl; <br><br>我想 write 和 writeln 应该可以的吧 <br><br>来自 :pitts 时间 :2000-11-06 21:29:00 <br>是 Delphi 的 {$APPTYPE CONSOLE} 控制台程序... <br>用 Write 不能写流,错误为:illegal type in Write/Writeln statement ... <br>想用 WriteConsole 试一试可是不会用
<br><br> <br><br>来自 :花花公子 时间 :2000-11-06 21:39:00 <br>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> <br><br>来自 :pitts 时间 :2000-11-07 19:56:00 <br>再简单说一下问题吧: <br><br>Delphi 写控制台程序,已知一个 TMemoryStream ,如何将其输出到控制台窗口(Dos)? <br><br>有 API WriteConsole 可以利用,但是不会用啊,好笨啊偶:(((( <br><br>The WriteConsole function writes a character string to a console screen buffer beginning at the current cursor location. <br><br>BOOL WriteConsole( <br><br>HANDLE hConsoleOutput, // handle to a console screen buffer <br>CONST VOID *lpBuffer, // pointer to buffer to write from <br>DWORD nNumberOfCharsToWrite, // number of characters to write <br>LPDWORD lpNumberOfCharsWritten, // pointer to number of characters written <br>LPVOID lpReserved // reserved <br>); <br><br><br>Parameters <br><br>hConsoleOutput <br><br>Identifies the console screen buffer to be written to. The handle must have GENERIC_WRITE access. <br><br>lpBuffer <br><br>Points to a buffer that contains characters to be written to the screen buffer. <br><br>nNumberOfCharsToWrite <br><br>Specifies the number of characters to write. <br><br>lpNumberOfCharsWritten <br><br>Points to a 32-bit variable that receives the number of characters actually written. <br><br>lpReserved <br><br>Reserved; must be NULL. <br><br><br><br>Return Values <br><br>If the function succeeds, the return value is nonzero. <br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks <br><br>WriteConsole writes characters to a console screen buffer. It behaves like the WriteFile function, except it can write in either Unicode (wide-character) or ANSI mode. To create an application that maintains a single set of sources compatible with both modes, use WriteConsole rather than WriteFile. Although WriteConsole can be used only with a console screen buffer handle, WriteFile can be used with other handles (such as files or pipes). WriteConsole fails if used with a standard handle that has been redirected to be something other than a console handle. <br><br>来自 :pitts 时间 :2000-11-09 20:13:00 <br>后来有人很简单就搞定了。。。 <br><br>program ShowStream; <br>{$APPTYPE CONSOLE} <br>uses <br>Windows, <br>SysUtils, <br>Classes; <br>var <br>sIn: TFileStream; <br>sOut: THandleStream; <br>begin <br>sIn := TFileStream.Create('f:/music1.gif', fmOpenRead); <br>try <br>sOut := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE)); <br>try <br>sOut.CopyFrom(sIn, sIn.Size); <br>finally <br>sOut.Free; <br>end; <br>finally <br>sIn.Free; <br>end; <br>end. <br> <br><br>来自 :pitts 时间 :2000-11-09 20:19:00 <br>多人接受答案了。 <br>