首页 > 编程语言 >c++计时器

c++计时器

时间:2024-10-23 10:10:00浏览次数:1  
标签:right int top c++ desktop 计时器 rect left

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; 
}

标签:right,int,top,c++,desktop,计时器,rect,left
From: https://www.cnblogs.com/WuMin4/p/18494561

相关文章

  • C++刷题tricks整理
    是自己做题中整理的常用C++操作,此为存档。STL容器容器适配器,STL里面的vector/array/deque/set/map/unordered_set可以直接使用==比较是否相等:vector<int>a;deque<int>a;map<int,string>a;unordered_map<int,string>a;set<int>a;unordered_set<int>a;forward_lis......
  • 马拉车算法(C/C++)
    #1024程序员节|征文#马拉车算法(Manacher'sAlgorithm)是一种用于在字符串中查找最长回文子串的线性时间复杂度算法。该算法由UdiManacher在1980年代提出,因此得名。它的核心思想是利用已知的回文信息来减少不必要的比较,从而提高效率。算法步骤预处理字符串:为了处理奇数......
  • C++入门Day5 ~ 6:简单变量 & 数据类型 part 1 <8000字长文带你初步理解数据类型>
    这是我在学习中的一个小问题,希望对你也有所帮助:        问:数据类型和简单变量属于oop的基本概念吗?        答:不是!数据类型和简单变量本身并不属于面向对象编程(OOP)的基本概念,但它们是编程中的基础概念,面向对象编程会基于这些基础概念来构建更复杂的结构。......
  • Vue2 项目实战:打造一个简易倒计时计时器工具 Vue2 实践教程:如何实现一个工作与休息倒
    效果图Vue2倒计时计时器工具教程在本教程中,我们将一步步实现一个Vue2倒计时计时器工具。这个工具允许用户在工作和休息模式之间切换,并设置倒计时时间。倒计时结束时,系统会发出提醒,提示用户切换工作或休息状态。非常适合初学者练习Vue的数据绑定、计算属性和事件处理......
  • C++基础——寻找水仙花数
    C++基础——寻找水仙花数一、水仙花数1.什么是水仙花数2.编译思路二、头文件三、主文件四、整体代码五、运行结果一、水仙花数1.什么是水仙花数原理:我们先理解什么是水仙花数(NarcissisticNumber),所谓的水仙花数是指一个整数的各位数字的立方和等于它本身。例如153......
  • 【C++】踏上C++学习之旅(三):“我“ 与 “引用“ 的浪漫邂逅
    文章目录前言1."引用"的概念1.1"引用"的语法2."引用"的特性3."引用"的使用场景3.1"引用"做参数3.2"引用"做返回值3.2.1"引用"做返回值时需要注意的点4.常引用5."引用"在底层的实现6."引用"和"指针"的不同点(面试常考)前言本文会着重的讲解&q......
  • 【C++-NOIP篇-4】 [NOIP2007 普及组] 纪念品分组
    文章目录[NOIP2007普及组]纪念品分组题目背景题目描述输入格式输出格式样例#1样例输入#1样例输出#1提示题目思路完整Code[NOIP2007普及组]纪念品分组题目背景NOIP2007普及组T2题目描述元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参......
  • 施磊c++基础8
    STL内容学习简介C++STL:standardtemplatelibarayvector容器底层数据结构:动态开辟的数组。每次以空间大的二倍扩容增加vec.push_back(20);末尾添加元素20—O(1)vec.insert(it,20);在it迭代器指向的位置插入元素20—O(n)删除vec.pop_back;末尾删除元素----......
  • 施磊c++基础7
    C++的四种类型转换c语言中提供的类型强转inta=(int)b;c++提供:const_cast:去掉常量属性的一个类型转换 int*p1=(int*)&a; int*p2=const_cast<int*>(&a);这两句是一样的,只不过使用第二种,可以保证类型转换是安全的,如果要转换成不符合的类型就会报错。static_......
  • 【C++指南】类和对象(四):类的默认成员函数——全面剖析 : 拷贝构造函数
     引言拷贝构造函数是C++中一个重要的特性,它允许一个对象通过另一个已创建好的同类型对象来初始化。了解拷贝构造函数的概念、作用、特点、规则、默认行为以及如何自定义实现,对于编写健壮和高效的C++程序至关重要。 C++类和对象系列文章,可点击下方链接阅读:【C++指南......