雁城传奇|

WuMin4

园龄:6个月粉丝:0关注:0

c++计时器

c++计时器 鼠标版

#include<bits/stdc++.h>
#include<windows.h>
#define kd(vk) (GetAsyncKeyState(vk)&0x8000?1:0)
using namespace std;
#define SHAKE 30
void ShakeWindow(){ 
	RECT rect; 
	HWND hwnd=GetConsoleWindow();
	GetWindowRect(hwnd,&rect); 
	MoveWindow(hwnd,rect.left+SHAKE,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE); 
	Sleep(28); 
	MoveWindow(hwnd,rect.left+SHAKE,rect.top-SHAKE,rect.right-rect.left,rect.bottom-rect.top,TRUE); 
	Sleep(28); 
	MoveWindow(hwnd,rect.left,rect.top-SHAKE,rect.right-rect.left,rect.bottom-rect.top,TRUE); 
	Sleep(28); 
	MoveWindow(hwnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE); 
}
void moveToMid(){
	HWND consoleWindow = GetConsoleWindow();
	RECT desktop;
	GetWindowRect(GetDesktopWindow(), &desktop);
	int width = desktop.right - desktop.left;
	int height = desktop.bottom - desktop.top;
	RECT consoleRect;
	GetWindowRect(consoleWindow, &consoleRect);
	int consoleWidth = consoleRect.right - consoleRect.left;
	int consoleHeight = consoleRect.bottom - consoleRect.top;
	SetWindowPos(consoleWindow, NULL,
		(width - consoleWidth) / 2,
		(height - consoleHeight) / 2,
		0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
void gt(int x,int y){
	COORD pos;pos.X=x;pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
bool gks(char s){
	return GetKeyState(s)<0;
}
void SetConsoleWindowSize(SHORT width,SHORT height)
{
	HANDLE hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
	SMALL_RECT wrt={0,0,width-1,height-1};
	SetConsoleWindowInfo(hStdOutput,TRUE,&wrt);
	COORD coord={width,height};
	SetConsoleScreenBufferSize(hStdOutput,coord);
}
void setTextSize(int x,int y){
	CONSOLE_FONT_INFOEX cfi;
	cfi.cbSize=sizeof(cfi);
	cfi.nFont=0;
	cfi.dwFontSize.X=x;
	cfi.dwFontSize.Y=y;
	cfi.FontFamily = FF_DONTCARE;
	cfi.FontWeight = FW_NORMAL;
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE),FALSE,&cfi);
}
void closeQuickEditMode(){
	HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);DWORD mode;
	GetConsoleMode(hStdin,&mode);mode&=~ENABLE_QUICK_EDIT_MODE;
	SetConsoleMode(hStdin,mode);
}
void disMouse(){
	ShowCursor(false);HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci);
	cci.bVisible=false;SetConsoleCursorInfo(hOut,&cci);
}
POINT gm(){
	HWND h=GetForegroundWindow();
	POINT p;
	GetCursorPos(&p);
	ScreenToClient(h,&p);
	return p;
}
void TopWindow(HWND &hWnd) {
	SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
void color(int a){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void debug(){
	color(7);
		int nx=min(max((long int)-1,gm().x/16),(long int)24),ny=min(max((long int)-1,gm().y/16),(long int)8);
	gt(0,4);
	cout<<nx<<"  "<<ny<<"   ";
}
int hr,mn,sc,tot,lst=-1;
void update(){
	color(7);
	gt(4,2);
	if(hr<10) cout<<"0"<<hr;
	else cout<<hr;
	gt(11,2);
	if(mn<10) cout<<"0"<<mn;
	else cout<<mn;
	gt(18,2);
	if(sc<10) cout<<"0"<<sc;
	else cout<<sc;
}
void count_down(){
	color(136);
	if((int)(((tot-(3600*hr+60*mn+sc))*1.0/tot)*29)!=lst){
		for(int i=lst+1;i<=(int)(((tot-(3600*hr+60*mn+sc))*1.0/tot)*29);i++)
			gt(i,4),cout<<"=";
		lst=(int)(((tot-(3600*hr+60*mn+sc))*1.0/tot)*29);
	}
}
signed main(){
	closeQuickEditMode();
	SetConsoleWindowSize(30,5);
	setTextSize(13,26);
	disMouse();
	SetConsoleTitle("计时器");
	gt(0,0);
	cout<<"计时器设置:";
	gt(4,1);
	cout<<"︽     ︽     ︽";
	gt(4,2);
	cout<<"  时:   分:   秒  [启动]";
	gt(4,3);
	cout<<"︾     ︾     ︾";
	while(true){
		update();
		int nx=min(max((long int)-1,gm().x/16),(long int)25),ny=min(max((long int)-1,gm().y/16),(long int)9);
		if((nx==3&&ny==2)||(nx==4&&ny==2)){
			color(8),gt(4,1),cout<<"︽";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				hr=(hr+1)%100,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)hr=(hr+1)%100,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))hr=(hr+1)%100,update(),Sleep(20);
			}
		}
		else
			color(7),gt(4,1),cout<<"︽";
		if((nx==3&&ny==5)||(nx==4&&ny==5)){
			color(8),gt(4,3),cout<<"︾";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				hr=(hr+99)%100,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)hr=(hr+99)%100,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))hr=(hr+99)%100,update(),Sleep(20);
			}
		}
		else
			color(7),gt(4,3),cout<<"︾";
		if((nx==9&&ny==2)||(nx==10&&ny==2)){
			color(8),gt(11,1),cout<<"︽";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				mn=(mn+1)%60,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)mn=(mn+1)%60,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))mn=(mn+1)%60,update(),Sleep(20);
			}
		}
		else
			color(7),gt(11,1),cout<<"︽";
		if((nx==9&&ny==5)||(nx==10&&ny==5)){
			color(8),gt(11,3),cout<<"︾";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				mn=(mn+59)%60,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)mn=(mn+59)%60,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))mn=(mn+59)%60,update(),Sleep(20);
			}
		}
		else
			color(7),gt(11,3),cout<<"︾";
		if((nx==14&&ny==2)||(nx==15&&ny==2)){
			color(8),gt(18,1),cout<<"︽";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				sc=(sc+1)%60,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)sc=(sc+1)%60,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))sc=(sc+1)%60,update(),Sleep(20);
			}
		}
		else
			color(7),gt(18,1),cout<<"︽";
		if((nx==14&&ny==5)||(nx==15&&ny==5)){
			color(8),gt(18,3),cout<<"︾";
			int tcnt=0;
			if(kd(VK_LBUTTON)){
				sc=(sc+59)%60,update();
				while(kd(VK_LBUTTON)&&tcnt<20) tcnt++,Sleep(10);
				tcnt=0;
				while(kd(VK_LBUTTON)&&tcnt<10)sc=(sc+59)%60,update(),Sleep(100),tcnt++;
				while(kd(VK_LBUTTON))sc=(sc+59)%60,update(),Sleep(20);
			}
		}
		else
			color(7),gt(18,3),cout<<"︾";
		if((19<=nx&&nx<=24)&&(3<=ny&&ny<=4)){
			color(8),gt(24,2),cout<<"[启动]";
			if(kd(VK_LBUTTON)){
				break;
			}
		}
		else
			color(7),gt(24,2),cout<<"[启动]";
		HWND hWnd=GetConsoleWindow();
		TopWindow(hWnd);
		Sleep(10);
	}
	tot=3600*hr+60*mn+sc;
	color(7);
	gt(0,0);
	cout<<"距离结束还剩";
	gt(3,1);
	cout<<"====================";
	gt(3,2);
	cout<<"=  时:   分:   秒= [暂停]";
	gt(3,3);
	cout<<"====================";
	int lst_sec=clock(),isp;
	while(kd(VK_LBUTTON));
	while(true){
		int nx=min(max((long int)-1,gm().x/16),(long int)25),ny=min(max((long int)-1,gm().y/16),(long int)9);
		if(!isp){
			update();
			sc=sc-((clock()/CLOCKS_PER_SEC)-(lst_sec/CLOCKS_PER_SEC));
			lst_sec=clock();
		}
		if(sc<0)
			sc=59,mn=mn-1;
		if(mn<0)
			mn=59,hr=hr-1;
		
		if((19<=nx&&nx<=24)&&(3<=ny&&ny<=4)){
			color(8),gt(24,2),cout<<(isp?"[继续]":"[暂停]");
			if(kd(VK_LBUTTON)){
				if(!isp)
					isp=true;
				else
					isp=false,lst_sec=clock();
				while(kd(VK_LBUTTON));
			}
		}
		else{
			color(7),gt(24,2),cout<<(isp?"[继续]":"[暂停]");
		}
		if(sc==0&&mn==0&&hr==0)
			break;
		count_down();
		HWND hWnd=GetConsoleWindow();
		TopWindow(hWnd);
		Sleep(10);
	}
	color(7);
	gt(0,0);
	cout<<"            ";
	gt(3,2);
	cout<<"=[TIME  IS  OVER!!]=       ";
	gt(0,4);
	cout<<"                             ";
	while(true){
		moveToMid();
		ShakeWindow();
		Sleep(50);
		moveToMid();
		ShakeWindow();
		Sleep(50);
		moveToMid();
		ShakeWindow();
		Sleep(1000);
	}
	return 0; 
}

本文作者:WuMin4

本文链接:https://www.cnblogs.com/WuMin4/p/18494561

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   WuMin4  阅读(21)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起