SetTimer在Win32和非窗口下定时

来源:互联网 发布:纸样软件 编辑:程序博客网 时间:2024/05/14 23:13
#include "stdafx.h"#include <stdio.h>#include <windows.h>#include <conio.h>UINT cnt = 0;// 定时器回调函数void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);// 线程回调函数DWORD CALLBACK ThreadProc(PVOID pvoid);  // 主函数int main(){DWORD dwThreadId;// 创建线程HANDLE hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &dwThreadId); printf("hello, thread start!\n");getch();       // 得到键盘输入后再退出return 0;}    void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime){cnt ++;printf("thread count = %d\n", cnt);}DWORD CALLBACK ThreadProc(PVOID pvoid){MSG msg;PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); SetTimer(NULL, 10, 1000, TimeProc);while(GetMessage(&msg, NULL, 0, 0)){if(msg.message == WM_TIMER){TranslateMessage(&msg);    // 翻译消息DispatchMessage(&msg);     // 分发消息}}KillTimer(NULL, 10);return 0;}

原创粉丝点击