用C++做的同步系统时间的代码,会不断更新,以毫秒为单位。打开后自动固定到左上角,会缩到刚好能看到时间,欢迎阅读!
#include<chrono>
#include<ctime>
#include<iostream>
#include<windows.h>
using namespace std::chrono;
using namespace std;
void gettime()
{
auto now=chrono::system_clock::now();
uint64_t dis_millseconds=chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count()-chrono::duration_cast<chrono::seconds>(now.time_since_epoch()).count()*1000;
time_t tt=chrono::system_clock::to_time_t(now);
auto time_tm=localtime(&tt);
char strTime[66]={0};
sprintf(strTime,"%d/%02d/%02d %02d:%02d:%02d.%03d",time_tm->tm_year + 1900,time_tm->tm_mon + 1,time_tm->tm_mday,time_tm->tm_hour,time_tm->tm_min,time_tm->tm_sec,(int)dis_millseconds);
cout<<strTime;
}
void gotoxy(short x=0, short y=0)
{
COORD pos={x,y};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hide()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);
}
void modeset(int width,int high) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD size = {width,high};
SetConsoleScreenBufferSize(hOut,size);
SMALL_RECT rc = {1,1,width,high};
SetConsoleWindowInfo(hOut,true,&rc);
return;
}
int main()
{
hide();
system("color F0");
HWND hwnd;
hwnd=FindWindow("ConsoleWindowClass",NULL);
SetWindowPos(hwnd,NULL,0,0,0,0,SWP_NOSIZE);
modeset(30,3);
while(1)
{
gotoxy();
gettime();
}
return 0;
}
本文为新手小白制作,有什么不足欢迎指正!
等等,点个赞再走呗>_<!
|