该代码改自msdn
// crt_BEGTHRD.C // compile with: /MT /D "_X86_" /c // processor: x86 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h> #include <atlbase.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 */ /* record the count of sub thread and show it on the console screen */ int RecordSubThreadCount( int count, CHAR ch ) { COORD coord; coord.X = 2; coord.Y = 2; DWORD result; COORD coord2; coord2 = coord; coord2.X += 4; char oldChar; char szCount[256]; char szCountOld[256]; itoa(count, szCount, 10); if (0 != count){ WriteConsoleOutputCharacter( hStdOut, szCountOld, strlen(szCountOld), coord, &result ); WriteConsoleOutputCharacter( hStdOut, &oldChar, 1, coord2, &result); } WriteConsoleOutputCharacter( hStdOut, szCount, strlen(szCount), coord, &result ); WriteConsoleOutputCharacter( hStdOut, &ch, 1, coord2, &result); strcpy(szCountOld, szCount); count ++; oldChar = ch; return count; } 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 ) { static int count = 0; count = RecordSubThreadCount(count, ch); /* On first loops, launch character threads. */ _beginthread( Bounce, 0, (void *) (ch++) ); if (ch > 'Z') ch = 'A'; /* Wait one second between loops. */ Sleep( 1000L ); } } /* 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.srWindow.Right - 1 ); newcoord.Y = GetRandom( 0, csbi.srWindow.Bottom - 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 += -1; newcoord.Y += -1; /* Correct placement (and beep) if about to go off the screen. */ if( newcoord.X < 0 ) newcoord.X = csbi.srWindow.Right ; if( newcoord.Y < 0 ) newcoord.Y = csbi.srWindow.Bottom; //Beep( ((char) ch - 'A') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); }