WinCE7定时器通知 by斜风细雨QQ:253786989 2012-01-01
如果想在某个时间点,自动运行一个指定的app,或者将某个event设置为有效状态,而又不需要用户掺和。那这个时候用“定时器通知”就比较适合了。
置“定时器通知”,同样使用CeSetUserNotificationEx函数。关于CeSetUserNotificationEx函数的使用参考“WinCE7用户通知”。
范例1:(定时运行NotificationApp.exe)
SYSTEMTIME st = {0}; HANDLE hNotify; CE_NOTIFICATION_TRIGGER nt; TCHAR szExeName[MAX_PATH], szText[128]; TCHAR szArgs[128] = TEXT("Timer notification."); GetLocalTime (&st); if (st.wMinute == 59) { st.wHour++; st.wMinute = 0; } else { st.wMinute++; } memset (&nt, 0, sizeof (CE_NOTIFICATION_TRIGGER)); nt.dwSize = sizeof (CE_NOTIFICATION_TRIGGER); nt.dwType = CNT_TIME; nt.lpszApplication = TEXT("NotificationApp.exe"); nt.lpszArguments = szArgs; nt.stStartTime = st; // Set the notification. hNotify = CeSetUserNotificationEx (0, &nt, NULL); if (hNotify) { wsprintf (szText, TEXT ("Timer notification set for %d:%02d:%02d"), st.wHour, st.wMinute, st.wSecond); } else { wsprintf (szText, TEXT ("Timer notification failed. rc = %d"), GetLastError()); } MessageBox (szText, TEXT("置定时器通知"), MB_OK | MB_ICONINFORMATION);
范例2:(定时将某个事件设置为有信号状态)
SYSTEMTIME st = {0}; HANDLE hNotify; CE_NOTIFICATION_TRIGGER nt; TCHAR szExeName[MAX_PATH], szText[128]; TCHAR szArgs[128] = TEXT("Timer notification."); GetLocalTime (&st); if (st.wMinute == 59) { st.wHour++; st.wMinute = 0; } else { st.wMinute++; } memset (&nt, 0, sizeof (CE_NOTIFICATION_TRIGGER)); nt.dwSize = sizeof (CE_NOTIFICATION_TRIGGER); nt.dwType = CNT_TIME; nt.lpszApplication = szExeName; nt.lpszArguments = szArgs; nt.stStartTime = st; StringCchCopy (szExeName, dim(szExeName), NAMED_EVENT_PREFIX_TEXT); StringCchCat (szExeName, dim(szExeName), szEventName); hNotify = CeSetUserNotificationEx (0, &nt, NULL); if (hNotify) { wsprintf (szText, TEXT ("Timer notification set for %d:%02d:%02d"), st.wHour, st.wMinute, st.wSecond); } else { wsprintf (szText, TEXT ("Timer notification failed. rc = %d"), GetLastError()); } MessageBox (hWnd, szText, szAppName, MB_OK);
将要设置为有效态的event赋值给lpszApplication,event名称有一个固定格式:
\\.\Notifications\NamedEvents\<Event Name>
其中Event Name用event名字代替,因为“\”是c/c++语言里面的转义字符,如要设置的event名字为“TimerNotificationEvent”,则:
nt.lpszApplication = TEXT(“\\\\.\\Notifications\\NamedEvents\\ TimerNotificationEvent”);
上面范例代码中用到的NAMED_EVENT_PREFIX_TEXT是一个宏定义:
#define NAMED_EVENT_PREFIX_TEXT TEXT(\\\\.\\Notifications\\NamedEvents\\)
WinCE7定时器通知 by斜风细雨QQ:253786989 2012-01-01