How to use Windows CE notification API

Source: Internet
Author: User

1 Introduction
A Windows CE-based handheld computer (such as pocketpc or HPC) not only has PC functions, but also has strong control capabilities. One of the ways Windows CE APIs surpass Microsoft's APIs in other operating systems is that it provides a powerful notification API ), this interface allows an application to schedule its own running at a specific time, or run when a system event occurs. This allows us to apply it to design and develop various advanced control programs, for example, you can automatically enable or disable one or more applications for scheduled or scheduled events, control the running process of one or more applications.
2 notification API resolution
A notification is a response signal sent by the operating system to an event. The response signal sent by Windows CE to the timer event is the "timer event notification", and the response signal sent to the system event is the "System Event Notification ". The timer event indicates that the specified time has been reached, and the system event indicates that a system-level event has occurred. If a device is added or deleted, the system time has changed and is synchronized with other devices, RS232 port connection is detected. If we want to directly run an application at a given time (without user interference), we can simply use "timer event notification ", when we need to monitor the occurrence of some system events, we need to use "System Event Notification ″. Note: unless a battery is not installed or is in the dead state, the power supply of the handheld computer will never be turned off; when the user presses the power off button or does not use it, the machine is only in sleep state and does not really cut off the power (in sleep state, it only provides the minimum energy required to keep its clock, applications, and data stored in Ram ). Therefore, for programs registered with "timer event notification" or "System Event Notification", even if the system is disabled, when a timer event arrives or a system event occurs, the application to run will also start.
Although the above "timer event notification" is easy to use, it sometimes cannot meet user needs. For example, for a complex control process, not only must the application be run at the specified time, but also different controls should be implemented based on the user's different responses. Therefore, Windows CE also provides the third notification interface: "user notification ″. "User notification" also uses timer events, but unlike "timer event notification", "user notification" must be confirmed by the user when it occurs, thus, different processes can be controlled according to different user responses at the time specified by "user notification. For example, if you only need to make a prompt at the specified time, the application that uses "user notification" can be designed to be in four ways (flashing led, vibrating device, sound and display prompt box). You can change the prompt mode at any time. For example, you must not only make a prompt at the specified time, but also make the program continue to run after the user makes a confirmation, in this case, "user notification" is used instead of "timer event notification ".
When we register a program we developed to a specific event notification, the operating system will generate a notification when the event occurs. The system uses notifications to communicate with users and other programs. Windows CE provides six notification interfaces:
Cesetusernotification
Cegetusernotificationpreferences
Ceclearusernotification
Cehandleappnotifications
Cerunappattime and cerunappatevent

The first four are used for "user notification", and the last two are "timer event notification" and "System Event Notification" respectively. The following describes how to use these six APIs:
(1) The cesetusernotification function is used to register user notifications. Its prototype is:
Handle cesetusernotification (handle hnotification,
Tchar * pwszappname,
Systemtime * lptime,
Pce_user_notification lpusernotification );
The parameter meaning is:

If the hnotification configuration of the handle is 0, a new notification is created, to change a registered notification, configure hnotification as the handle of the user notification you want to change (this handle is returned by the registered user notification program after cesetusernotification is called );

Pwszappname is the name of the application. When a notification occurs, the small icon of the application is displayed on the taskbar. lptime is a pointer to the systemtime structure, this structure specifies the notification occurrence time. lpusernotification is also a structure pointer pointing to the pce_user_notification structure. Windows CE uses this structure to describe how users are notified. The definition of this structure is:
Typedef struct usernotificationtype {
DWORD actionflags;
Tchar * pwszdialogtitle;
Tchar * pwszdialogtext;
Tchar * pwszsound;
DWORD nmaxsound;
DWORD dwreserved;
} Ce_user_notification, * pce_user_notification;

The variable actionflags is a set of symbols that define the form of prompting the user when the specified time is reached:

Pun_led (flashing screen ),

Pun_vibrate (vibration device ),

Pun_dialog (display dialog box ),

Pun_sound (playback sound Documentation)

Pun_repeat (Repeat audio files for 10 to 15 seconds ),

He can be any combination of the above signs. From the time when the program calls cesetusernotification to the time when the user is notified, the notification remains active. To modify this notification before it times out, the program can call cesetusernotification again.

(2) Call cegetusernotificationpreferences. The function prototype is:
Bool cegetusernotificationpreferences (hwnd hwndparent, pce_user_notification lpnotification );
This function allows you to configure user notifications so that you can change the prompt method. hwndparent is the window handle of the parent window in the prompt box.

(3) Call ceclearusernotification to clear a user before the user notification arrives.

(4) Call the cehandleappnotifications function to confirm user notification. After the user notification arrives, you need to confirm. For notifications in the prompt box, click the OK button in the prompt box or press the notification button on the device shell (in this case, user notification only serves as a prompt and does not start the application ); for notifications without a prompt box, the system displays the icon of the program that registers the notification on the taskbar, when you click this icon, the system starts an instance of the corresponding application (the system also passes the command line parameter "lpcmdline" to show why the application runs, this parameter is the handle of the app_run_to_handle_notification string with a space and a notification ). For user notifications without a prompt box, you must call the cehandleappnotifications function in the application to confirm the notification. This function marks any activity notifications used for the application as handled, and delete the icon on the taskbar. In actual coding, you must also consider whether another instance of the application is running. If yes, a custom message should be sent to the instance to process the notification and terminate itself to save resources.

(5) Call cerunappattime to generate a "timer event notification". The function prototype is:
Bool cerunappattime (tchar * pwszappname, systemtime * lptime );

The parameter meaning is:

Lptime is a structure pointer that specifies the time for running the application;

Pwszappname is the name of the application to run. It is simple to automatically run an application at a given time. To modify the "timer event notification", you only need to call cerunappattime again. Because the last call to cerunappattime will replace the previous notification. To clear the "timer event notification", you only need to pass a null pointer in the lptime parameter when you call cerunappattime.

(6) Call cerunappatevent to generate "System Event Notification". function prototype:
Bool cerunappatevent (tchar * pwszappname, long lwhichevent );

The parameter meaning is:

Pwszappname is the name of the application to be run;

Lwhichevent indicates the event to be monitored. The flag constant is as follows:
Notification_event_none clear Event Notification
Notification_event_sync_end notification of synchronization completion
Notification_event_device_change add or delete device notifications
Notification_event_rs232_detected: notification of RS232 connection Detected
Notification_event_time_change system time change notification
Notification_event_restore_end notification of device recovery completion

To stop responding to System Event Notifications, the application only needs to call cerunappatevent again and pass its name and icationication_event_none in the lwhichevent parameter.

3. Notify API usageCodeInstance
# Include <policy. h>
Ce_user_notification g_ceun;
(1) code snippet initialized to the ce_user_notification structure.

Memset (& g_ceun, sizeof (g_ceun ));
G_ceun.actionflags = pun_dialog;
G_ceun.pwszdialogtitle = szdlgtitle;
G_ceun.pwszdialogtext = szdlgtext;
G_ceun.pwszsound = szsound;
G_ceun.nmaxsound = sizeof (szsound );

(2) code snippet for registering a user notification:
Systemtime st;

Getlocaltime (& St );
Getmodulefilename (hinst, szexename, sizeof (szexename ));
Hsf-y = cesetusernotification (0, szexename, & St, & g_ceun );

(3) code segment for configuring user notification:
Cegetusernotificationpreferences (hwnd, & g_ceun );

(4) code segments that use cehandleappconfigurications and only run one instance (to save resources:
// Determine whether application startup is due to user notification
If (lstrcmp (sztext, app_run_to_handel_notification = 0)
Getmodulefilename (hinst, sztext, sizeof (sztext ));

Cehandleappnotifications (sztext );
Hsf-y = (handle) _ wtol (pptr); // handle for receiving notifications
// Check whether an application instance is running
Hwnd = findwindow (null, szappname );
If (hwnd) // if any, send a custom message to the user who processes the notification.
Sendmessage (hwnd, mymsg_tellnotify, 0, (lparm) hsf-y );

// Terminate itself and skip the code

(5) code segment using "timer event notification:
Systemtime st;

Getlocaltime (& St );
Getmodulefilename (hinst, szexename, sizeof (szexename ));
Cerunappattime (szexename, & St );

(6) code segment using "System Event Notification:
Long Levent;
If (isdlgbuttonchecked (hwnd, idc_sync_end) = 1)
Levent! = Icationication_event_sync_end;
If (isdlgbuttonchecked (hwnd, idc_serial_detect) = 1)
Levent! = Icationication_event_rs232_detected;
If (isdlgbuttonchecked (hwnd, idc_device_change) = 1)
Levent! = Icationication_event_device_change;
If (isdlgbuttonchecked (hwnd, idc_time_change) = 1)
Levent! = Notifaction_event_time_change;
If (isdlgbuttionchecked (hwnd, idc_restore_end) = 1)
Levent! = Icationication_event_restore_end;

Getmodulefilename (hinst, szexename, sizeof (szexename );
Cerunappatevent (szexename, Levent );
The following describes how to use the Windows CE notification API and how to use the Windows CE application development environment.

 

    • How to remove the notification from the taskbar

There are two scenarios:

1. The notification created by using shnotificationadd is removed by using shnotificationremove.

2. The following methods can be used for system notification (such as missed calls or new text messages:

Modify the key values in the registry [HKEY_CURRENT_USER \ System \ State. E.g.

To remove the unanswered phone maid, modify it as follows:

[HKEY_CURRENT_USER \ System \ state \ phone]

"Missed call count" = 0

Remove the New Short Message notification and modify it as follows:

[HKEY_CURRENT_USER \ System \ state \ messages \ SMS \ unread]

"Count" = 0

The last time the key value was modified to delete the notification, it may have side effects. For example, the count in today screen may display an error value due to the registry modification.

[HKEY_CURRENT_USER \ System \ state \ messages \ SMS \ unread]

"Count" = 0

I searched for it on the Internet. Some people in codeproject implemented it through shnotificationremove. I modified it. The sample code is as follows:

Void removenewmessagenotification ()
{
Shnotificationdata shnd;
CLSID;
Lresult result;
DWORD dwid = 0;

If (0 = clsidfromstring (text ("{A877D65B-239C-47a7-9304-0D347F580408}"), & CLSID ))
{
Memset (& shnd, 0, sizeof (shnd ));
Shnd. cbstruct = sizeof (shnotificationdata );
Do
{
Result = shnotificationgetdata (& CLSID, dwid, & shnd );
If (error_success = Result)
{
Shnotificationremove (& CLSID, dwid );
If (shnd. pszhtml)
{
Free (void *) shnd. pszhtml );
Shnd. pszhtml = NULL;
}
If (shnd. psztitle)
{
Free (void *) shnd. psztitle );
Shnd. psztitle = NULL;
}
}
Else
Dwid ++;
} While (error_success! = Result) & (dwid <20000 ));
}
}

 

    • System Menu Extension

The extension of the system menu is implemented through COM. There are two main tasks: code implementation and registry key value creation.

1. Code implementation:

The implementation interfaces are iobjectwithsite and icontextmenu. Of course, it also includes iclassfactory, which helps to generate the object with the specified clsid. Its code is actually the same, and it can be used by slightly modifying the code in the sample.

For more information, see <SDK installation path> \ samples \ common \ CPP \ Win32 \ inboxmenuextensibilityto extend the menu in the message list of tmail.exe (messaging.

The key implementation is mainly in querycontextmenu and invokecommand. querycontextmenu is generally used to add menu operations, and invokecommand is used to respond when you click the menu item you just added. It is worth mentioning that the return value of querycontextmenu should be set to the number of menu items you have added, which is generally returned make_hresult (severity_success, 0, nmenuadded ); in this way, the system will know how many menus you have added. id1_first is correct when someone else calls querycontextmenu. For example, other applicationsProgramIf a menu item is added to this menu item, it will not cause a conflict (for example, if you click your menu item, the result will be the command of the menu item added by other software ). Smart phone is also quite special. Every time you enter a popup menu, the system will respond to querycontextmenu, therefore, the number of menu items in the current pop-up menu is generally calculated to avoid adding a menu item to each pop-up menu.

 

2. Create a registry key value:

Create a key under [hkey_classes_root \ CLSID]. The name is the CLSID used by you, and then create a key under this key. The name is inprocserver32. The default value is set to your DLL.

The key created under [HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ shell \ extensions \ contextmenus] is determined based on the position of your extended menu. For specific values, see menu Overview In SDK document.

State and configurications Broker

State and notification brokers provide a mechanism for storing system and application information in the registry and a notification system for storing information changes. It can be used to monitor any registry key values in the system. The notification types include:

system state information, such as features present (camera, keyboard), active application, cradled state, battery state.
message information, such as Count of unread, last received, account info.
tasks and appointments information, such as upcoming, overdue, location.
Windows Media Player information, such as currently playing album, artist.
phone information, such as signal information, operator information, call information, multiline information.
connections information, related to Bluetooth, cellular, network, modem, etc.
snapi. h contains the registry key, path, Value Name, and mask to be monitored. For example, if you want to know the current status of the phone, you can see the snapi. h has such a paragraph:

//////////////////////////////////////// ////////////////////////////////////////
// Phoneroaming
// Gets a value indicating whether the phone is currently in roaming mode.
# Define sn_phoneroaming_root HKEY_LOCAL_MACHINE
# Define sn_phoneroaming_path text ("SYSTEM \ state \ phone ")
# Define sn_phoneroaming_value text ("status ")
# Define sn_phoneroaming_bitmask 0x200

If the value 0 x of the key value [HKLM \ System \ state \ phone] "status" is 1, the phone is roaming.

So, how to monitor the registry key value? You can use APIs such as registrypolicyapp, registrypolicywindow, and registrypolicycallback. Sample Code:

# Include <snapi. h>
# Include <regext. h>

# Define um_reg1_y wm_user + 1000

// Global variables:
Hregnotify g_hregnotify;

Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case wm_create:
Icationicationcondition NC;
Zeromemory (& NC, sizeof (icationicationcondition ));
NC. ctcomparisontype = reg_ct_equal;
NC. dwmask = 0x200;
NC. targetvalue. DW = 0x200;
Registrypolicywindow (sn_phoneroaming_root, sn_phoneroaming_path, sn_phoneroaming_value, hwnd, um_reg1_y, 0, & NC, & g_hreg1_y );
Break;
Case um_regnotify:
MessageBox (hwnd, _ T ("phone is roaming! "), _ T (" Notification broker "), mb_ OK );
Break;
Case wm_destroy:
Registryclosenotification (g_hregnotify );
Postquitmessage (0 );
Break;

Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Return 0;
}

 

This article is from the csdn blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.