用C++制作一个倒数计时器
Making a countdown timer in C++
我有一个控制台应用程序,旨在 仅 运行 windows。它是用 C++ 编写的。有什么方法可以等待 60 秒(并且在屏幕上显示剩余时间)然后继续代码流吗?
我尝试了来自互联网的不同解决方案,但其中 none 有效。它们要么不工作,要么没有正确显示时间。
您可以使用 sleep() 系统调用休眠 60 秒。
您可以按照此 link 了解如何使用系统调用 Timer in C++ using system calls 设置 60 秒计时器。
可以使用 Waitable Timer Objects 将 perion 设置为 1 秒来完成此任务。可能的实施
VOID CALLBACK TimerAPCProc(
__in_opt LPVOID /*lpArgToCompletionRoutine*/,
__in DWORD /*dwTimerLowValue*/,
__in DWORD /*dwTimerHighValue*/
)
{
}
void CountDown(ULONG Seconds, COORD dwCursorPosition)
{
if (HANDLE hTimer = CreateWaitableTimer(0, 0, 0))
{
static LARGE_INTEGER DueTime = { (ULONG)-1, -1};//just now
ULONGLONG _t = GetTickCount64() + Seconds*1000, t;
if (SetWaitableTimer(hTimer, &DueTime, 1000, TimerAPCProc, 0, FALSE))
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
do
{
SleepEx(INFINITE, TRUE);
t = GetTickCount64();
if (t >= _t)
{
break;
}
if (SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition))
{
WCHAR sz[8];
WriteConsoleW(hConsoleOutput,
sz, swprintf(sz, L"%02u..", (ULONG)((_t - t)/1000)), 0, 0);
}
} while (TRUE);
}
CloseHandle(hTimer);
}
}
COORD dwCursorPosition = { };
CountDown(60, dwCursorPosition);
//Please note that this is Windows specific code
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int counter = 60; //amount of seconds
Sleep(1000);
while (counter >= 1)
{
cout << "\rTime remaining: " << counter << flush;
Sleep(1000);
counter--;
}
}
在 C++ 中,您可以使用倒计时。请完成以下逻辑,让您在屏幕上显示剩余时间。
for(int min=m;min>0;min--) //here m is the total minits as per ur requirements
{
for(int sec=59;sec>=;sec--)
{
sleep(1); // here you can assign any value in sleep according to your requirements.
cout<<"\r"<<min<<"\t"<<sec;
}
}
如果您需要更多帮助,请关注 link here
希望它会起作用,请告诉我它是否适用于您的情况?或者如果您需要任何帮助。
谢谢!
这可能会有一些帮助,目前还不完全清楚问题是什么,但这是一个从 10 秒开始的倒计时计时器,您可以更改秒数并增加分钟数和小时数。
#include <iomanip>
#include <iostream>
using namespace std;
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
int main()
{
for (int sec = 10; sec < 11; sec--)
{
cout << setw(2) << sec;
cout.flush();
sleep(1);
cout << '\r';
if (sec == 0)
{
cout << "boom" << endl;
}
if (sec <1)
break;
}
}
我有一个控制台应用程序,旨在 仅 运行 windows。它是用 C++ 编写的。有什么方法可以等待 60 秒(并且在屏幕上显示剩余时间)然后继续代码流吗?
我尝试了来自互联网的不同解决方案,但其中 none 有效。它们要么不工作,要么没有正确显示时间。
您可以使用 sleep() 系统调用休眠 60 秒。
您可以按照此 link 了解如何使用系统调用 Timer in C++ using system calls 设置 60 秒计时器。
可以使用 Waitable Timer Objects 将 perion 设置为 1 秒来完成此任务。可能的实施
VOID CALLBACK TimerAPCProc(
__in_opt LPVOID /*lpArgToCompletionRoutine*/,
__in DWORD /*dwTimerLowValue*/,
__in DWORD /*dwTimerHighValue*/
)
{
}
void CountDown(ULONG Seconds, COORD dwCursorPosition)
{
if (HANDLE hTimer = CreateWaitableTimer(0, 0, 0))
{
static LARGE_INTEGER DueTime = { (ULONG)-1, -1};//just now
ULONGLONG _t = GetTickCount64() + Seconds*1000, t;
if (SetWaitableTimer(hTimer, &DueTime, 1000, TimerAPCProc, 0, FALSE))
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
do
{
SleepEx(INFINITE, TRUE);
t = GetTickCount64();
if (t >= _t)
{
break;
}
if (SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition))
{
WCHAR sz[8];
WriteConsoleW(hConsoleOutput,
sz, swprintf(sz, L"%02u..", (ULONG)((_t - t)/1000)), 0, 0);
}
} while (TRUE);
}
CloseHandle(hTimer);
}
}
COORD dwCursorPosition = { };
CountDown(60, dwCursorPosition);
//Please note that this is Windows specific code
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int counter = 60; //amount of seconds
Sleep(1000);
while (counter >= 1)
{
cout << "\rTime remaining: " << counter << flush;
Sleep(1000);
counter--;
}
}
在 C++ 中,您可以使用倒计时。请完成以下逻辑,让您在屏幕上显示剩余时间。
for(int min=m;min>0;min--) //here m is the total minits as per ur requirements
{
for(int sec=59;sec>=;sec--)
{
sleep(1); // here you can assign any value in sleep according to your requirements.
cout<<"\r"<<min<<"\t"<<sec;
}
}
如果您需要更多帮助,请关注 link here
希望它会起作用,请告诉我它是否适用于您的情况?或者如果您需要任何帮助。
谢谢!
这可能会有一些帮助,目前还不完全清楚问题是什么,但这是一个从 10 秒开始的倒计时计时器,您可以更改秒数并增加分钟数和小时数。
#include <iomanip>
#include <iostream>
using namespace std;
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
int main()
{
for (int sec = 10; sec < 11; sec--)
{
cout << setw(2) << sec;
cout.flush();
sleep(1);
cout << '\r';
if (sec == 0)
{
cout << "boom" << endl;
}
if (sec <1)
break;
}
}