用C语言中的图形库在界面上显示计时时间的代码怎么写
时间: 2024-03-10 20:50:06 浏览: 23
你可以使用C语言的图形库来在界面上显示计时时间。以下是一个基于Windows API的示例代码,使用Win32 API的CreateWindow函数创建一个窗口并在窗口上显示计时时间:
```c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ID_TIMER 1
#define TIMER_INTERVAL 50
// 全局变量
HWND hwnd;
int cxClient, cyClient;
clock_t start_time;
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
char str[256];
double elapsed_time;
switch (message) {
case WM_CREATE:
start_time = clock();
SetTimer(hwnd, ID_TIMER, TIMER_INTERVAL, NULL);
return 0;
case WM_TIMER:
InvalidateRect(hwnd, NULL, TRUE); // 刷新窗口
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
elapsed_time = (double)(clock() - start_time) / CLOCKS_PER_SEC;
sprintf(str, "Elapsed Time: %.2f seconds", elapsed_time);
DrawText(hdc, str, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
// WinMain函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
static char szAppName[] = "TimerApp";
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
// 注册窗口类
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, "This program requires Windows NT!", szAppName, MB_ICONERROR);
return 0;
}
// 创建窗口
hwnd = CreateWindow(szAppName, "Timer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window creation failed!", szAppName, MB_ICONERROR);
return 0;
}
// 显示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 消息循环
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
```
这个程序使用了CreateWindow函数创建一个窗口,并在WM_CREATE消息中记录了程序开始的时间。在WM_TIMER消息中刷新窗口,WM_SIZE消息中记录窗口大小,WM_PAINT消息中计算已经经过的时间并将其绘制在客户区中。当窗口被销毁时,程序结束并退出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)