看msdn吧,有例子的。查_beginthread或者AfxBeginThread,前者是C++的,后者是MFC的。如果没有msdn,就辛苦点看下面的代码吧,拷的,没什么贡献/* BEGTHRD.C illustrates multiple threads using functions: * * _beginthread _endthread * * * This program requires the multithreaded library. For example, * compile with the following command line: * CL /MT /D "_X86_" BEGTHRD.C * * If you are using the Visual C++ development environment, select the * Multi-Threaded runtime library in the compiler Project Settings * dialog box. * */#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 */void 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 ); } }/* 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') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); }
看我的例子 void CSimulateDlg::OnTimer(UINT nIDEvent) { static int count=0; BYTE name[8]={65,66,67,68,69,70,71}; //CFont *pfont=new CFont(); switch(nIDEvent) { case TIMER1: GetDlgItem(IDC_EDIT)->SetFocus(); keybd_event(name[count],0,0,0); keybd_event(name[count],0,KEYEVENTF_KEYUP,0); break; case TIMER2: POINT lpPoint; GetCursorPos(&lpPoint); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
*
* _beginthread _endthread
*
*
* This program requires the multithreaded library. For example,
* compile with the following command line:
* CL /MT /D "_X86_" BEGTHRD.C
*
* If you are using the Visual C++ development environment, select the
* Multi-Threaded runtime library in the compiler Project Settings
* dialog box.
*
*/#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 */void 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 );
}
}/* 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') * 100, 175 );
}
/* _endthread given to terminate */
_endthread();
}
void CSimulateDlg::OnTimer(UINT nIDEvent) {
static int count=0;
BYTE name[8]={65,66,67,68,69,70,71};
//CFont *pfont=new CFont();
switch(nIDEvent)
{
case TIMER1:
GetDlgItem(IDC_EDIT)->SetFocus();
keybd_event(name[count],0,0,0);
keybd_event(name[count],0,KEYEVENTF_KEYUP,0);
break;
case TIMER2:
POINT lpPoint;
GetCursorPos(&lpPoint);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
break;
case TimerMousePosition:
POINT lpPointPosition;
//CString strX,strY;
GetCursorPos(&lpPointPosition);
sprintf( strX,"%d",lpPointPosition.x);
sprintf( strY,"%d",lpPointPosition.y);
m_MouseX=strX;
m_MouseY=strY;
UpdateData(false);
break;
case TimerDateTime:
GetLocalTime( &CurTime );
sprintf( strDateTime,"%04d.%02d.%02d %02d:%02d:%02d",CurTime.wYear,CurTime.wMonth,CurTime.wDay,CurTime.wHour,CurTime.wMinute,CurTime.wSecond );
m_DateTime=strDateTime;
UpdateData(false);
break;
default:
break; }
CDialog::OnTimer(nIDEvent);
}
//Timer
//开始
SetTimer(1,500,0); //1 为 Timer的ID 500 为时间间隔
SetTimer(2,1000,0); //第二个Timer//停止
KillTimer(1);
KillTimer(2);//函数
void CXXDlg::OnTimer(UINT nIDEvent)
{
//如果只有一个Timer 直接写代码,不用if 判断ID
if (nIDEvent == 1)
{
//事件
}
else
{
//其它Timer..
}
CDialog::OnTimer(nIDEvent);
}
在线程函数里死循环
太麻烦了!
OnTimer()不是有编号的吗!
可以有好几个的呀!