// 2Window_Timer.cpp : 定义应用程序的入口点。//#include "stdafx.h"#include "2Window_Timer.h"#include <iostream>HINSTANCE g_hInst;HANDLE g_hStdout = NULL;//宏定义输入函数#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL);void CALLBACK TimerProc( //定时器回调函数HWND hwnd, //窗口句柄UINT uMsg, //WM_TIMER消息IDUINT idEvent, //定时器IDDWORD dwTime //当前系统时间){CHAR szText[256];sprintf_s(szText, 256, "ID为%d调用系统时间%ld\n", idEvent,dwTime);//PrintLog(szText);}void OnTimer(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) //定时器触发函数{CHAR szText[256];sprintf_s(szText, 256, "编号ID为:%d的定时器被触发\n", wParam);PrintLog(szText);}void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){PrintLog("SetTimer Begin:\n");SetTimer(hWnd, 1000, 3 * 1000, NULL); //设置一个定时器SetTimer(hWnd, 1001, 5 * 1000, NULL);//使用TimerProc处理多线程//如果不设置,则不会调用默认的WM_TIMER消息SetTimer(NULL, 0, 2 * 1000, TimerProc); //如果第一个参数为空时,则必须给出第四个参数,也就是回调函数SetTimer(NULL, 0, 3 * 1000, TimerProc); //因为第一参数和第四参数同时为空,所以不执行}LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){switch (nMsg){case WM_CREATE: //创建时的消息OnCreate(hWnd, nMsg, wParam, lParam);break;case WM_TIMER: //定时器OnTimer(hWnd, nMsg, wParam, lParam);switch (wParam) //这个switch只有放到WM_TIMER消息里面才会起作用{case 1000:PrintLog("1000定时器极招灭杀\n");break;case 1001:PrintLog("1001定时器极招黑天\n");break;}PrintLog("定时器极招黑天\n");break;case WM_DESTROY: //窗口销毁KillTimer(hWnd, 1000);PostQuitMessage(0);break;}return DefWindowProc(hWnd, nMsg, wParam, lParam);}BOOL RegisterWnd(LPSTR pszClassName){WNDCLASSEX wce = { 0 };wce.cbSize = sizeof(wce);wce.cbClsExtra = 0;wce.cbWndExtra = 0;wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);wce.hCursor = NULL;wce.hIcon = NULL;wce.hIconSm = NULL;wce.hInstance = g_hInst;wce.lpfnWndProc = WndProc;wce.lpszClassName = pszClassName;wce.lpszMenuName = NULL;wce.style = CS_VREDRAW | CS_HREDRAW;ATOM aTom = RegisterClassEx(&wce);if (aTom == 0){return FALSE;}else{return TRUE;}}HWND CreateWnd(LPSTR pszClassName){HWND hWnd = CreateWindowEx(0, pszClassName, "ttttt", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, g_hInst, NULL);return hWnd;}void showWnd(HWND hWnd){ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);}void msg(){MSG msg = { 0 };while (GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}}//控制台void ConsoleWnd(){AllocConsole();g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);CHAR szText[] = "Debug start:\n";PrintLog(szText);}int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd){g_hInst = hInstance;ConsoleWnd();RegisterWnd("OOOO");HWND hWnd = CreateWnd("OOOO");showWnd(hWnd);msg();return 0;}
原文:http://www.cnblogs.com/nfking/p/5573150.html