定时器回调函数原型
typedef BOOL (* TIMERPROC)(HWND, int, DWORD);
第1个参数:窗口句柄;
第2个参数:定时器ID;
第3个参数:定时器被触发时的系统滴答值;
当TIMERPROC返回值为FALSE时,自动删除该定时器,该功能用于创建单次(one-shot)定时器
设置定时器,可以设置定时器回调函数
BOOL GUIAPI SetTimerEx (HWND hWnd, int id, unsigned int speed, TIMERPROC timer_proc);
重置定时器的间隔,可以设置定时器回调函数
BOOL GUIAPI ResetTimerEx (HWND hWnd, int id, unsigned int speed, TIMERPROC timer_proc);
#define SetTimer(hwnd, id, speed) SetTimerEx(hwnd, id, speed, NULL)
#define ResetTimer(hwnd, id, speed) ResetTimerEx(hwnd, id, speed, (TIMERPROC)0xFFFFFFFF)
默认以
10 毫秒为单位,speed取值 100 即 1 秒
当定时器到期时,定时器指定窗口会收到一个
MSG_TIMER消息
删除定时器
BOOL GUIAPI KillTimerEx (HWND hWnd, int id);
检查某个定时器是否被安装到指定窗口
BOOL GUIAPI IsTimerInstalled (HWND hWnd, int id);
检查系统是否还有可用定时器资源
BOOL GUIAPI HaveFreeTimer (void);
获取自MiniGUI启动到现在的滴答值
unsigned int GUIAPI GetTickCount (void);
限制:
1. MiniGUI-Threads模式下,每个消息队列最多能管理 32个定时器,每个线程对应一个消息队列,每个应用程序最多也只能设置 32 个定时器;
2. MiniGUI-Processes 模式下,每个进程只有一个消息队列,这个消息队列最多可以管理 32 个定时器;
3. 当一个MSG_TIMER消息尚未处理而新的消息到来,则忽略新消息;
4. 定时器消息是优先级最低的消息类型,只有消息队列中不存在其它类型的消息(比如邮寄消息、通知消息、退出消 息、绘图消息)时,系统才会去检查是否有定时器到期
5. MiniGUI-Processes 的服务器进程,即 mginit 程序已经调用 setitimer 系统调用安装了定时器, 应用程序自己实现的 mginit 程序不应该再使用 setitimer 实现自己的定时器,但MiniGUI-Processes 的客户程序仍可以调用 setitimer 函数,MiniGUI-Threads 则没有这样的限制;
定时器示例
1 #include <stdio.h>
2 #include <time.h>
3
4 #include <minigui/common.h>
5 #include <minigui/minigui.h>
6 #include <minigui/gdi.h>
7 #include <minigui/window.h>
8 #include <minigui/control.h>
9
10 #define _ID_TIMER 100
11 #define _ID_TIME_STATIC 101
12
13 static char* mk_time (
char*
buff)
14 {
15 time_t t;
16 struct tm *
tm;
17
18 time (&
t);
19 tm = localtime (&
t);
20 sprintf (buff,
"d:d:d", tm->tm_hour, tm->tm_min, tm->
tm_sec);
21
22 return buff;
23 }
24
25 static int TimerDemoProc(HWND hWnd,
int message, WPARAM wParam, LPARAM lParam)
26 {
27 char buff [
20];
28
29 switch (message)
30 {
31 case MSG_CREATE:
32 {
33 CreateWindow (CTRL_STATIC, mk_time (buff),
34 WS_CHILD | WS_BORDER | WS_VISIBLE |
SS_CENTER,
35 _ID_TIME_STATIC,
36 50,
50,
37 100,
20,
38 hWnd,
0);
39
40 SetTimer (hWnd, _ID_TIMER,
100);
41 break;
42 }
43 case MSG_TIMER:
44 {
45 if(wParam ==
_ID_TIMER)
46 {
47 SetDlgItemText (hWnd, _ID_TIME_STATIC, mk_time (buff));
48 }
49
50 break;
51 }
52 case MSG_CLOSE:
53 {
54 KillTimer (hWnd, _ID_TIMER);
55 DestroyAllControls (hWnd);
56 DestroyMainWindow (hWnd);
57 PostQuitMessage (hWnd);
58 return 0;
59 }
60 }
61
62 return DefaultMainWinProc(hWnd,message,wParam,lParam);
63 }
64
65 int MiniGUIMain(
int argc,
const char*
argv[])
66 {
67 MSG msg;
68 MAINWINCREATE CreateInfo;
69 HWND hMainWnd;
70
71 #ifdef _MGRM_PROCESSES
72 JoinLayer(NAME_DEF_LAYER ,
"main" ,
0 ,
0);
73 #endif
74
75 CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER |
WS_CAPTION;
76 CreateInfo.dwExStyle =
WS_EX_NONE;
77 CreateInfo.spCaption =
"Timer Demo";
78 CreateInfo.hMenu =
0;
79 CreateInfo.hCursor = GetSystemCursor(
0);
80 CreateInfo.hIcon =
0;
81 CreateInfo.MainWindowProc =
TimerDemoProc;
82 CreateInfo.lx =
0;
83 CreateInfo.ty =
0;
84 CreateInfo.rx = g_rcScr.right /
2;
85 CreateInfo.by = g_rcScr.bottom /
2;
86 CreateInfo.iBkColor =
COLOR_lightwhite;
87 CreateInfo.dwAddData =
0;
88 CreateInfo.hHosting =
HWND_DESKTOP;
89
90 hMainWnd = CreateMainWindow(&
CreateInfo);
91 if (hMainWnd ==
HWND_INVALID)
92 return -
1;
93
94 ShowWindow(hMainWnd, SW_SHOWNORMAL);
95
96 while (GetMessage(&
msg,hMainWnd))
97 {
98 TranslateMessage(&
msg);
99 DispatchMessage(&
msg);
100 }
101
102 MainWindowThreadCleanup(hMainWnd);
103
104 return 0;
105 }
106
107 #ifndef _MGRM_PROCESSES
108 #include <minigui/dti.c>
109 #endif
转载于:https://www.cnblogs.com/paullam/p/3653936.html
相关资源:miniui常用界面设置