-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06__定时器消息.cpp
More file actions
58 lines (56 loc) · 1.88 KB
/
Copy path06__定时器消息.cpp
File metadata and controls
58 lines (56 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <windows.h>
#include<string.h>
#include<tchar.h>
#include<stdio.h>
#include<winuser.h>
//增加控制台窗口
HANDLE g_Output = NULL;
void OnTimer(HWND hWnd, WPARAM wParam) {
char szText[256] = {0};
sprintf(szText, "定时器 %llu\n", wParam);
WriteConsole(g_Output, szText, strlen(szText), NULL, NULL);
}
//窗口处理函数(自定义,处理消息)
LRESULT CALLBACK WinProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam) {
switch (msgID) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_TIMER:
OnTimer(hWnd, wParam);
case WM_CREATE:
SetTimer(hWnd, 1, 100, NULL);
SetTimer(hWnd, 2, 250, NULL);
break;
}
return DefWindowProc(hWnd, msgID, wParam, lParam);
}
/* Win32 GUI程序的“main”函数:程序从这里开始执行 */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ipCmdLine, int nCmdShow) {
//增加控制台窗口
AllocConsole();
g_Output = GetStdHandle(STD_OUTPUT_HANDLE);
//注册窗口类
WNDCLASSEX wc;
memset (& wc, 0, sizeof wc); //将 wc 清零
wc.cbSize = sizeof (WNDCLASSEX); //变量所占字节数
wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; //窗口风格
wc.hInstance = hInstance; //当前进程实例句柄
wc.lpszClassName = "Main"; //窗口类名
wc.lpfnWndProc = WinProc; //窗口处理函数
wc.hbrBackground = GetSysColorBrush (COLOR_WINDOW); //窗口的背景
if (!RegisterClassEx (& wc)) return 0;
//创建窗口
HWND hWnd = CreateWindowEx(0, "Main", "Window", WS_OVERLAPPEDWINDOW,
100, 100, 1000, 500, NULL, NULL, hInstance, NULL);
//显示窗口
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
//消息循环
MSG nMsg = {0};
while (GetMessage(&nMsg, NULL, 0, 0)) {
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);//将消息交给窗口处理函数
}
return 0;
}