Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在屏幕上显示经过时间的计数器,就像在现实生活中的秒表一样?I';我对编程很陌生(C+;+;)_C++ - Fatal编程技术网

C++ 如何在屏幕上显示经过时间的计数器,就像在现实生活中的秒表一样?I';我对编程很陌生(C+;+;)

C++ 如何在屏幕上显示经过时间的计数器,就像在现实生活中的秒表一样?I';我对编程很陌生(C+;+;),c++,C++,如何在屏幕上显示经过时间的计数器?我想要一个经过时间的实时计数器。如果您有任何其他建议或改进建议,请告诉我,因为这对我非常重要 #include <iostream> #include <time.h> using namespace std; class C{ int ti,tf; public: void start(); void stop(); int print(); } ; void C::start(){ t

如何在屏幕上显示经过时间的计数器?我想要一个经过时间的实时计数器。如果您有任何其他建议或改进建议,请告诉我,因为这对我非常重要

#include <iostream>
#include <time.h>

using namespace std;
class C{
int ti,tf;
public:
    void start();
    void stop();
    int print();
    } ;
    void C::start(){
    ti=time(0);
    tf=0;
    }
    void C::stop(){
    tf=time(0);
    }
    int C::print(){
    if (tf==0) return time(0)-ti;
    else return tf-ti;}

int main()
{
    char ch;
    C p;
    cout << "i] start\n";
    cout << "f] stop\n";
    cout << "p] print\n";
    cout << "x] exit\n";
     do{
        cout << "option:"; cin >> ch;
        ch=tolower(ch);

        switch(ch){
    case 'i': p.start(); cout << "watch started\n";break;
    case 'f': p.stop(); cout << "watch stopped\n";break;
    case 'p': cout << "time measured: " <<  p.print() << " seconds" << endl ;break;
    case 'x': break;
    default: cout << "invalid option\n";break;
    }
    }while(ch!='x');
    return 0;
}

#包括
#包括
使用名称空间std;
C类{
int-ti,tf;
公众:
void start();
无效停止();
int print();
} ;
void C::start(){
ti=时间(0);
tf=0;
}
void C::stop(){
tf=时间(0);
}
intc::print(){
如果(tf==0)返回时间(0)-ti;
else返回tf ti;}
int main()
{
char ch;
cp;

cout似乎您想知道如何制作一个图形程序,该程序实际上有一个窗口,您可以在屏幕上绘制并显示实时输出

这在很大程度上取决于您使用的操作系统(Windows?),以及您是否希望为桌面GUI程序使用本机内置库,或使用第三方库

如果您使用的是Windows,则可以在Visual Studio中创建Windows桌面应用程序类型的新项目,并使用此代码显示计时器。请看我在何处使用DrawText函数:

// WindowsProject1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WindowsProject1.h"
#include <string>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
HWND g_hWnd = NULL;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));

    MSG msg;

    // Main message loop:
    while (true)
    {
        static DWORD last = GetTickCount();
        if (GetTickCount() - last > 32) {
            last = GetTickCount();
            InvalidateRect(g_hWnd, NULL, true);
        }

        if (!PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
            continue;
        }

        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   g_hWnd = hWnd;
   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            RECT r{ 0, 0, 100, 100 };
            std::wstring timeStr = std::to_wstring(GetTickCount());
            // TODO: Add any drawing code that uses hdc here...
            DrawText(hdc, timeStr.c_str(), timeStr.size(), &r, DT_CENTER);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

//WindowsProject1.cpp:定义应用程序的入口点。
//
#包括“stdafx.h”
#包括“WindowsProject1.h”
#包括
#定义最大加载字符串100
//全局变量:
HINSTANCE hInst;//当前实例
WCHAR szTitle[MAX_LOADSTRING];//标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];//主窗口类名
HWND g_HWND=NULL;
//转发此代码模块中包含的函数声明:
ATOM MyRegisterClass(HINSTANCE HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
INT_PTR回调关于(HWND、UINT、WPARAM、LPARAM);
国际货币基金组织,
_在当前情况下,
_在LPWSTR lpCmdLine中,
_In_uuint(nCmdShow)
{
未引用的_参数(HPPreInstance);
未引用的_参数(lpCmdLine);
//TODO:将代码放在这里。
//初始化全局字符串
LoadStringW(hInstance、IDS\U APP\U TITLE、szTitle、MAX\U LOADSTRING);
LoadStringW(hInstance、IDC_WINDOWSPROJECT1、szWindowClass、MAX_LOADSTRING);
MyRegisterClass(hInstance);
//执行应用程序初始化:
如果(!InitInstance(hInstance,nCmdShow))
{
返回FALSE;
}
HACCEL hAccelTable=加载加速器(hInstance、MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));
味精;
//主消息循环:
while(true)
{
静态DWORD last=GetTickCount();
if(GetTickCount()-last>32){
last=GetTickCount();
无效(g_hWnd,NULL,true);
}
如果(!peek消息(&msg,nullptr,0,0,PM_REMOVE)){
持续
}
if(!TranslateAccelerator(msg.hwnd、hAccelTable和msg))
{
翻译信息(&msg);
发送消息(&msg);
}
}
返回(int)msg.wParam;
}
//
//函数:MyRegisterClass()
//
//目的:注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE HINSTANCE)
{
WNDCLASSEXW wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc=WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
wcex.hCursor=LoadCursor(nullptr,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+1);
wcex.lpszMenuName=MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));
返回寄存器CLASSEXW(&wcex);
}
//
//函数:InitInstance(HINSTANCE,int)
//
//用途:保存实例句柄并创建主窗口
//
//评论:
//
//在这个函数中,我们将实例句柄保存在一个全局变量中,然后
//创建并显示主程序窗口。
//
BOOL InitInstance(HINSTANCE HINSTANCE,int nCmdShow)
{
hInst=hInstance;//将实例句柄存储在全局变量中
HWND HWND=CreateWindowW(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,
CW_usefault,0,CW_usefault,0,nullptr,nullptr,hInstance,nullptr);
如果(!hWnd)
{
返回FALSE;
}
显示窗口(hWnd、nCmdShow);
更新窗口(hWnd);
g_hWnd=hWnd;
返回TRUE;
}
//
//功能:WndProc(HWND、UINT、WPARAM、LPARAM)
//
//用途:处理主窗口的消息。
//
//WM_命令-处理应用程序菜单
//WM_绘制-绘制主窗口
//WM_DESTROY-发布退出消息并返回
//
//
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
开关(信息)
{
case WM_命令:
{
int wmId=低ORD(wParam);
//解析菜单选项:
交换机(wmId)
{
案例IDM_关于:
对话框(hInst、MAKEINTRESOURCE(IDD_ABOUTBOX)、hWnd、About);
打破
案例IDM_退出:
窗口(hWnd);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
}
打破
案例WM_油漆:
{
PAINTSTRUCT-ps;
HDC HDC=开始喷漆(hWnd和ps);
RECT r{0,0,100,100};
std::wstring timeStr=std::towstring(GetTickCount());
//TODO:在此处添加任何使用hdc的图形代码。。。
DrawText(hdc、timeStr.c_str()、timeStr.size()、和r、DT_CENTER);
端漆(hWnd和ps);
}
打破
案例WM_销毁:
邮递