如何在点击通知时捕获用户的点击动作
How to capture user's click action when clicking on notifications
目前,我想在 Windows 上显示气球通知,然后我想在用户单击 气球时 捕获操作 通知。
然后捕捉到这个动作再做一个动作
我该怎么做?
我可以在所有 Windows 平台(从 Windows 7 到 Windows 10)上这样做吗?
您在评论中说您正在使用 Shell_NotifyIcon()
来显示气球。您问题的答案在 documentation:
As of Windows XP (Shell32.dll version 6.0), if a user passes the mouse pointer over an icon with which a balloon notification is associated, the Shell sends the following messages:
NIN_BALLOONSHOW. Sent when the balloon is shown (balloons are queued).
NIN_BALLOONHIDE. Sent when the balloon disappears. For example, when the icon is deleted. This message is not sent if the balloon is dismissed because of a timeout or if the user clicks the mouse.
As of Windows 7, NIN_BALLOONHIDE is also sent when a notification with the NIIF_RESPECT_QUIET_TIME flag set attempts to display during quiet time (a user's first hour on a new computer). In that case, the balloon is never displayed at all.
NIN_BALLOONTIMEOUT. Sent when the balloon is dismissed because of a timeout.
NIN_BALLOONUSERCLICK. Sent when the balloon is dismissed because the user clicked the mouse.
In addition to those messages, as of Windows Vista (Shell32.dll version 6.0.6), if a user passes the mouse pointer over an icon with which a balloon notification is associated, the Windows Vista Shell also adds the following messages:
NIN_POPUPOPEN. Sent when the user hovers the cursor over an icon to indicate that the richer pop-up UI should be used in place of a standard textual tooltip.
NIN_POPUPCLOSE. Sent when a cursor no longer hovers over an icon to indicate that the rich pop-up UI should be closed.
Regardless of the operating system version, you can select which way the Shell should behave by calling Shell_NotifyIcon
with dwMessage set to NIM_SETVERSION
. Set the uVersion
member of the NOTIFYICONDATA structure pointed to by lpdata to indicate whether you want Windows 2000, Windows Vista, or pre-version 5.0 (Windows 95) behavior.
Note The messages discussed above are not conventional Windows messages. They are sent as the lParam value of the application-defined message that is specified in the uCallbackMessage
member of the NOTIFYICONDATA structure pointed to by lpdata, when Shell_NotifyIcon
is called with the NIM_ADD
flag set in dwMessage.
因此,要接收这些消息,您需要在调用 Shell_NotifyIcon()
并将 dwMessage
设置为 NIM_ADD
时在 NOTIFYICONDATA
结构中指定 HWND 和回调消息 ID或 NIM_MODIFY
:
hWnd
Type: HWND
A handle to the window that receives notifications associated with an icon in the notification area.
...
uCallbackMessage
Type: UINT
An application-defined message identifier. The system uses this identifier to send notification messages to the window identified in hWnd
. These notification messages are sent when a mouse event or hover occurs in the bounding rectangle of the icon, when the icon is selected or activated with the keyboard, or when those actions occur in the balloon notification.
When the uVersion
member is either 0 or NOTIFYICON_VERSION, the wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. This identifier can be 32 bits in length. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the pointer moves over a taskbar icon, lParam is set to WM_MOUSEMOVE.
When the uVersion
member is NOTIFYICON_VERSION_4, applications continue to receive notification events in the form of application-defined messages through the uCallbackMessage
member, but the interpretation of the lParam and wParam parameters of that message is changed as follows:
LOWORD(lParam) contains notification events, such as NIN_BALLOONSHOW, NIN_POPUPOPEN, or WM_CONTEXTMENU.
HIWORD(lParam) contains the icon ID. Icon IDs are restricted to a length of 16 bits.
GET_X_LPARAM(wParam) returns the X anchor coordinate for notification events NIN_POPUPOPEN, NIN_SELECT, NIN_KEYSELECT, and all mouse messages between WM_MOUSEFIRST and WM_MOUSELAST. If any of those messages are generated by the keyboard, wParam is set to the upper-left corner of the target icon. For all other messages, wParam is undefined.
GET_Y_LPARAM(wParam) returns the Y anchor coordinate for notification events and messages as defined for the X anchor.
...
If you set the NIF_INFO
flag in the uFlags
member, the balloon-style notification is used. For more discussion of these notifications, see Balloon tooltips.
因此,为了回答您的问题,在您在 NOTIFYICONDATA::hWnd
字段中指定的 HWND 的 WndProc 中,您将查找您在 NOTIFYICONDATA::uCallbackMessage
字段中指定的消息 ID,然后在邮件的 lParam
或 LOWORD(lParam)
值中查找 NIN_BALLOONUSERCLICK
通知,具体取决于您如何设置 NOTIFYICONDATA::uVersion
字段。
目前,我想在 Windows 上显示气球通知,然后我想在用户单击 气球时 捕获操作 通知。 然后捕捉到这个动作再做一个动作
我该怎么做? 我可以在所有 Windows 平台(从 Windows 7 到 Windows 10)上这样做吗?
您在评论中说您正在使用 Shell_NotifyIcon()
来显示气球。您问题的答案在 documentation:
As of Windows XP (Shell32.dll version 6.0), if a user passes the mouse pointer over an icon with which a balloon notification is associated, the Shell sends the following messages:
NIN_BALLOONSHOW. Sent when the balloon is shown (balloons are queued).
NIN_BALLOONHIDE. Sent when the balloon disappears. For example, when the icon is deleted. This message is not sent if the balloon is dismissed because of a timeout or if the user clicks the mouse.
As of Windows 7, NIN_BALLOONHIDE is also sent when a notification with the NIIF_RESPECT_QUIET_TIME flag set attempts to display during quiet time (a user's first hour on a new computer). In that case, the balloon is never displayed at all.
NIN_BALLOONTIMEOUT. Sent when the balloon is dismissed because of a timeout.
NIN_BALLOONUSERCLICK. Sent when the balloon is dismissed because the user clicked the mouse.
In addition to those messages, as of Windows Vista (Shell32.dll version 6.0.6), if a user passes the mouse pointer over an icon with which a balloon notification is associated, the Windows Vista Shell also adds the following messages:
NIN_POPUPOPEN. Sent when the user hovers the cursor over an icon to indicate that the richer pop-up UI should be used in place of a standard textual tooltip.
NIN_POPUPCLOSE. Sent when a cursor no longer hovers over an icon to indicate that the rich pop-up UI should be closed.
Regardless of the operating system version, you can select which way the Shell should behave by calling
Shell_NotifyIcon
with dwMessage set toNIM_SETVERSION
. Set theuVersion
member of the NOTIFYICONDATA structure pointed to by lpdata to indicate whether you want Windows 2000, Windows Vista, or pre-version 5.0 (Windows 95) behavior.Note The messages discussed above are not conventional Windows messages. They are sent as the lParam value of the application-defined message that is specified in the
uCallbackMessage
member of the NOTIFYICONDATA structure pointed to by lpdata, whenShell_NotifyIcon
is called with theNIM_ADD
flag set in dwMessage.
因此,要接收这些消息,您需要在调用 Shell_NotifyIcon()
并将 dwMessage
设置为 NIM_ADD
时在 NOTIFYICONDATA
结构中指定 HWND 和回调消息 ID或 NIM_MODIFY
:
hWnd
Type:HWND
A handle to the window that receives notifications associated with an icon in the notification area....
uCallbackMessage
Type:UINT
An application-defined message identifier. The system uses this identifier to send notification messages to the window identified inhWnd
. These notification messages are sent when a mouse event or hover occurs in the bounding rectangle of the icon, when the icon is selected or activated with the keyboard, or when those actions occur in the balloon notification.When the
uVersion
member is either 0 or NOTIFYICON_VERSION, the wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. This identifier can be 32 bits in length. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the pointer moves over a taskbar icon, lParam is set to WM_MOUSEMOVE.When the
uVersion
member is NOTIFYICON_VERSION_4, applications continue to receive notification events in the form of application-defined messages through theuCallbackMessage
member, but the interpretation of the lParam and wParam parameters of that message is changed as follows:
LOWORD(lParam) contains notification events, such as NIN_BALLOONSHOW, NIN_POPUPOPEN, or WM_CONTEXTMENU.
HIWORD(lParam) contains the icon ID. Icon IDs are restricted to a length of 16 bits.
GET_X_LPARAM(wParam) returns the X anchor coordinate for notification events NIN_POPUPOPEN, NIN_SELECT, NIN_KEYSELECT, and all mouse messages between WM_MOUSEFIRST and WM_MOUSELAST. If any of those messages are generated by the keyboard, wParam is set to the upper-left corner of the target icon. For all other messages, wParam is undefined.
GET_Y_LPARAM(wParam) returns the Y anchor coordinate for notification events and messages as defined for the X anchor.
...
If you set the
NIF_INFO
flag in theuFlags
member, the balloon-style notification is used. For more discussion of these notifications, see Balloon tooltips.
因此,为了回答您的问题,在您在 NOTIFYICONDATA::hWnd
字段中指定的 HWND 的 WndProc 中,您将查找您在 NOTIFYICONDATA::uCallbackMessage
字段中指定的消息 ID,然后在邮件的 lParam
或 LOWORD(lParam)
值中查找 NIN_BALLOONUSERCLICK
通知,具体取决于您如何设置 NOTIFYICONDATA::uVersion
字段。