CTimer,对同一定时器,多次调用SetTimer

1.结论

    对同一定时器,多次调用SetTimer,定时器会被重置。

2.验证

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rect;
	DWORD tick;
	static DWORD tick_s;
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 分析菜单选择:
		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:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: 在此添加任意绘图代码...
		GetClientRect(hWnd, &rect);
		tick_s = ::GetTickCount();
		DrawText(hdc, tmp_g, -1, &rect, DT_LEFT);	
		SetTimer(hWnd, 1, 3000, NULL);
		Sleep(1000);
		SetTimer(hWnd, 1, 3000, NULL);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_TIMER:
		KillTimer(hWnd, 1);
		tick = GetTickCount();
		tmp_g.Format( _T("times:%ld"), tick - tick_s);
		//UpdateWindow(hWnd);
		InValidateRect( hWnd, NULL, TRUE ); //使用InvalidRect使窗口重绘

  结果会发现界面上显示的时间段为:4000毫秒


 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的贪吃蛇C语言程序的代码实现,使用了SetTimer函数和线程函数: ```c #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h> #define WIDTH 20 #define HEIGHT 20 int map[WIDTH][HEIGHT] = {0}; // 地图 int x, y; // 蛇头坐标 int fx, fy; // 食物坐标 int length = 3; // 初始蛇长 int direction = 1; // 初始方向:向右 int score = 0; // 得分 void draw(); // 绘制地图 void init(); // 初始化 void update(); // 更新游戏状态 void create_food(); // 生成食物 void gameover(); // 游戏结束 void move(); // 移动蛇 void input(); // 处理输入 void timer(); // 计时器回调函数 int main() { init(); draw(); SetTimer(GetConsoleWindow(), 1, 100, (TIMERPROC)timer); // 设置计时器,每100毫秒执行一次timer函数 while (1) { input(); } return 0; } void draw() { system("cls"); printf("score: %d\n", score); printf("+"); for (int i = 0; i < WIDTH; i++) { printf("-"); } printf("+\n"); for (int i = 0; i < HEIGHT; i++) { printf("|"); for (int j = 0; j < WIDTH; j++) { if (map[j][i] == 0) printf(" "); else if (map[j][i] == 1) printf("O"); else if (map[j][i] == 2) printf("*"); } printf("|\n"); } printf("+"); for (int i = 0; i < WIDTH; i++) { printf("-"); } printf("+\n"); } void init() { // 设置蛇头初始位置 x = WIDTH / 2; y = HEIGHT / 2; map[x][y] = 1; // 生成初始食物 create_food(); } void update() { move(); // 移动蛇 draw(); // 重绘地图 input(); // 处理输入 } void create_food() { // 随机生成食物位置 fx = rand() % WIDTH; fy = rand() % HEIGHT; // 如果食物位置与蛇重叠,则重新生成 while (map[fx][fy] != 0) { fx = rand() % WIDTH; fy = rand() % HEIGHT; } // 在地图上标记食物位置 map[fx][fy] = 2; } void gameover() { printf("Game Over!\n"); printf("Your Score: %d\n", score); exit(0); // 结束程序 } void move() { // 根据方向移动蛇头 switch (direction) { case 1: // 向右 x++; break; case 2: // 向左 x--; break; case 3: // 向下 y++; break; case 4: // 向上 y--; break; } // 判断是否撞墙 if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) { gameover(); } // 判断是否吃到食物 if (x == fx && y == fy) { length++; // 增加蛇长 score += 10; // 增加得分 create_food(); // 生成新的食物 } // 判断是否撞到自己 if (map[x][y] == 1) { gameover(); } // 在地图上标记蛇头位置 map[x][y] = 1; // 如果蛇长度大于初始长度,删除蛇尾 if (length > 3) { for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { if (map[i][j] == length - 3) { map[i][j] = 0; } } } } } void input() { // 处理输入 if (_kbhit()) { int key = _getch(); switch (key) { case 'w': // 向上 if (direction != 3) { direction = 4; } break; case 's': // 向下 if (direction != 4) { direction = 3; } break; case 'a': // 向左 if (direction != 1) { direction = 2; } break; case 'd': // 向右 if (direction != 2) { direction = 1; } break; } } } void timer() { update(); } ``` 在这个程序中,我们使用了SetTimer函数来设置计时器,每100毫秒执行一次timer函数,这个函数用于更新游戏状态。同时,在主函数中使用了一个无限循环来处理输入,当用户按下键盘时,会调用input函数来处理输入。当蛇头移动时,会调用move函数来移动蛇,并判断游戏是否结束。最后,使用draw函数来绘制游戏界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值