#define _CRT_SECURE_NO_DEPRECATE // 仅VC++2005才需要这个宏
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cassert>
#include <conio.h>
#include <windows.h>
using namespace std;
#pragma comment( lib, "Winmm" ) // 如果是dev-cpp, 手工在连接器中加入 libwinmm.a 文件路径,比如 C:/Dev-Cpp/lib/libwinmm.a
void __stdcall timecallback( UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2 )
{
time_t t = time( 0 );
tm* lt = localtime( &t );
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), *(COORD*)dwUser );
cout << setw(4) << setfill('0') << right << ( 1900 + lt->tm_year ) << '/'
<< setw(2) << setfill('0') << right << ( 1 + lt->tm_mon ) << '/'
<< setw(2) << setfill('0') << right << ( lt->tm_mday ) << ' '
<< setw(2) << setfill('0') << right << ( lt->tm_hour ) << ':'
<< setw(2) << setfill('0') << right << ( lt->tm_min ) << ':'
<< setw(2) << setfill('0') << right << ( lt->tm_sec ) << endl;
}
int main( void )
{
cout << "按任意键退出..." << endl;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi );
WORD wAttrnew = csbi.wAttributes|FOREGROUND_INTENSITY;
SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), wAttrnew );
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), &cci );
BOOL bVisibleold = cci.bVisible;
cci.bVisible = FALSE;
SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), &cci );
timecallback( 0, 0, (DWORD_PTR)&csbi.dwCursorPosition, 0, 0 );
MMRESULT id = timeSetEvent( 1000, 0, &timecallback, (DWORD_PTR)&csbi.dwCursorPosition, TIME_PERIODIC|TIME_CALLBACK_FUNCTION );
assert( id );
_getch();
timeKillEvent( id );
cci.bVisible = bVisibleold;
SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), &cci );
SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes );
return 0;
}

评论