user-0 提问: 如何将两个运动的“点”显示不同的颜色

回复:
user: user-0
content: 引用 8 楼 u010165006 的回复:// 将Project—Properties—Configuration Properties—General—Character Set  // 设置为：Not Set 或者 Use Multi-byte Character Set // 这样就是使用ANSI字符集  	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); 	 	WORD wColors[1]={FOREGROUND_RED}; 	textout(handle,10,10,wColors,1,"甲方"); 	wColors[0]=FOREGROUND_BLUE; 	textout(handle,20,20,wColors,1,"乙方"); 谢谢，我想要的实现了。

user: user-0
content: 引用 1 楼 u010165006 的回复:#include "stdafx.h" #include "colorConsole.h"  /* HANDLE initiate() { 	HANDLE hOutput;  	hOutput = GetStdHandle(STD_OUTPUT_HANDLE); 	return hOutput; } */  BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString) {     DWORD cWritten;      BOOL fSuccess;      COORD coord;      	coord.X = x;              // 输出第一个字符所在的列即X坐标      coord.Y = y;              //   所在的行即Y坐标           fSuccess = WriteConsoleOutputCharacter(		//输出字符串         hOutput,              // 屏幕缓冲区的句柄         lpszString,           // 指向字符串的指针          lstrlen(lpszString),  // 字符串长度         coord,                // 输出第一个字符的坐标         &cWritten);           // 实际输出字符的个数     if (! fSuccess)          cout<<"error:WriteConsoleOutputCharacter"<<endl;      	     	for (;fSuccess && coord.X < lstrlenA((LPCSTR)lpszString)+x; coord.X += nColors)      {         fSuccess = WriteConsoleOutputAttribute(		//为已输出的字符串上色             hOutput,          // 屏幕缓冲区的句柄             wColors,          // 颜色数组             nColors,          // 颜色数目             coord,            // 输出第一个字符的坐标             &cWritten);       // 实际输出字符的个数     }     if (! fSuccess)      	cout<<"error:WriteConsoleOutputAttribute"<<endl;  	return 0; }  自己是个小白，关于API的东西不是特别懂，要是能指导一下textout函数调用的形参的声明（虽然你有注释，但是我还是有点不明白QAQ）就更好了，我看了你的这个感觉上可以完成我的想法

user: user-1
content: 颜色可以参考《C.C++控制台界面编程(V4).pdf》

user: user-1
content: // 将Project—Properties—Configuration Properties—General—Character Set  // 设置为：Not Set 或者 Use Multi-byte Character Set // 这样就是使用ANSI字符集  	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); 	 	WORD wColors[1]={FOREGROUND_RED}; 	textout(handle,10,10,wColors,1,"甲方"); 	wColors[0]=FOREGROUND_BLUE; 	textout(handle,20,20,wColors,1,"乙方");

user: user-0
content: 引用 2 楼 paschen 的回复:如果需要变换颜色，就再次调用该函数 这个方法在我这里感觉不合适，因为我需要两种颜色同时在屏幕中显示，而你说的再次调用该函数也只是同时改变这两个点的颜色，没有达到目的

user: user-2
content: 如果需要变换颜色，就再次调用该函数

user: user-1
content: #include "stdafx.h" #include "colorConsole.h"  /* HANDLE initiate() { 	HANDLE hOutput;  	hOutput = GetStdHandle(STD_OUTPUT_HANDLE); 	return hOutput; } */  BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString) {     DWORD cWritten;      BOOL fSuccess;      COORD coord;      	coord.X = x;              // 输出第一个字符所在的列即X坐标      coord.Y = y;              //   所在的行即Y坐标           fSuccess = WriteConsoleOutputCharacter(		//输出字符串         hOutput,              // 屏幕缓冲区的句柄         lpszString,           // 指向字符串的指针          lstrlen(lpszString),  // 字符串长度         coord,                // 输出第一个字符的坐标         &cWritten);           // 实际输出字符的个数     if (! fSuccess)          cout<<"error:WriteConsoleOutputCharacter"<<endl;      	     	for (;fSuccess && coord.X < lstrlenA((LPCSTR)lpszString)+x; coord.X += nColors)      {         fSuccess = WriteConsoleOutputAttribute(		//为已输出的字符串上色             hOutput,          // 屏幕缓冲区的句柄             wColors,          // 颜色数组             nColors,          // 颜色数目             coord,            // 输出第一个字符的坐标             &cWritten);       // 实际输出字符的个数     }     if (! fSuccess)      	cout<<"error:WriteConsoleOutputAttribute"<<endl;  	return 0; }

user: user-3
content: 再供参考：#include <windows.h> #include <stdio.h>  void ConPrint(char *CharBuffer, int len); void ConPrintAt(int x, int y, char *CharBuffer, int len); void gotoXY(int x, int y); void ClearConsole(void); void ClearConsoleToColors(int ForgC, int BackC); void SetColorAndBackground(int ForgC, int BackC); void SetColor(int ForgC); void HideTheCursor(void); void ShowTheCursor(void);  int main(int argc, char* argv[]) {    HideTheCursor();    ClearConsoleToColors(15, 1);    ClearConsole();    gotoXY(1, 1);    SetColor(14);    printf("This is a test...\n");    Sleep(5000);    ShowTheCursor();    SetColorAndBackground(15, 12);    ConPrint("This is also a test...\n", 23);    SetColorAndBackground(1, 7);    ConPrintAt(22, 15, "This is also a test...\n", 23);    gotoXY(0, 24);    SetColorAndBackground(7, 1);    return 0; }  //This will clear the console while setting the forground and //background colors. void ClearConsoleToColors(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //not used but we need to capture this since it will be    //written anyway (passing NULL causes an access violation).    DWORD count;     //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    SetConsoleTextAttribute(hStdOut, wColor);    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);        FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will clear the console. void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will set the position of the cursor void gotoXY(int x, int y) {    //Initialize the coordinates    COORD coord = {x, y};    //Set the position    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }  //This will set the forground color for printing in a console window. void SetColor(int ForgC) {    WORD wColor;    //We will need this handle to get the current background attribute    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    CONSOLE_SCREEN_BUFFER_INFO csbi;     //We use csbi for the wAttributes word.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //Mask out all but the background attribute, and add in the forgournd color       wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);       SetConsoleTextAttribute(hStdOut, wColor);    } }  //This will set the forground and background color for printing in a console window. void SetColorAndBackground(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); }  //Direct console output void ConPrint(char *CharBuffer, int len) {    DWORD count;    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL); }  //Direct Console output at a particular coordinate. void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); }  //Hides the console cursor void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  //Shows the console cursor void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }

user: user-3
content: 仅供参考：// processor: x86 #include <windows.h> #include <process.h>    /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h>  void Bounce( void *ch ); void CheckKey( void *dummy );  /* GetRandom returns a random integer between min and max. */ #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))  BOOL repeat = TRUE;     /* Global repeat flag and video variable */ HANDLE hStdOut;         /* Handle for console window */ CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */  int main() {     CHAR    ch = 'A';      hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );      /* Get display screen's text row and column information. */    GetConsoleScreenBufferInfo( hStdOut, &csbi );      /* Launch CheckKey thread to check for terminating keystroke. */     _beginthread( CheckKey, 0, NULL );      /* Loop until CheckKey terminates program. */     while( repeat )     {         /* On first loops, launch character threads. */         _beginthread( Bounce, 0, (void *) (ch++)  );          /* Wait one second between loops. */         Sleep( 1000L );     }     return 0; }  /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */ void CheckKey( void *dummy ) {     _getch();     repeat = 0;    /* _endthread implied */  }  /* Bounce - Thread to create and and control a colored letter that moves  * around on the screen.  *  * Params: ch - the letter to be moved  */ void Bounce( void *ch ) {     /* Generate letter and color attribute from thread argument. */     char    blankcell = 0x20;     char    blockcell = (char) ch;     BOOL    first = TRUE;    COORD   oldcoord, newcoord;    DWORD   result;       /* Seed random number generator and get initial location. */     srand( _threadid );     newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );     newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );     while( repeat )     {         /* Pause between loops. */         Sleep( 100L );          /* Blank out our old position on the screen, and draw new letter. */         if( first )             first = FALSE;         else          WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );          WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );          /* Increment the coordinate for next placement of the block. */         oldcoord.X = newcoord.X;         oldcoord.Y = newcoord.Y;         newcoord.X += GetRandom( -1, 1 );         newcoord.Y += GetRandom( -1, 1 );          /* Correct placement (and beep) if about to go off the screen. */         if( newcoord.X < 0 )             newcoord.X = 1;         else if( newcoord.X == csbi.dwSize.X )             newcoord.X = csbi.dwSize.X - 2;         else if( newcoord.Y < 0 )             newcoord.Y = 1;         else if( newcoord.Y == csbi.dwSize.Y )             newcoord.Y = csbi.dwSize.Y - 2;          /* If not at a screen border, continue, otherwise beep. */         else             continue;         Beep( ((char) ch - 'A') * 1000, 500 );     }     /* _endthread given to terminate */     _endthread(); }

user: user-3
content: console屏幕处理例子程序。终端窗口屏幕处理相关API使用例子。来自MSVC20\SAMPLES\win32\console\ http://download.csdn.net/detail/zhao4zhong1/3461309