📄 tut23.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Iczelion">
<meta name="GENERATOR" content="Mozilla/4.7 [en] (Win98; I) [Netscape]">
<title>Iczelion's Win32 Assembly Tutorial 23: Tray Icon</title>
</head>
<body text="#CCCCCC" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">
<center>
<h1>
<font face="Arial,Helvetica"><font color="#FFCC00">Tutorial 23: Tray Icon</font></font></h1></center>
<font face="Arial,Helvetica"><font size=-1>In this tutorial, we will learn
how to put icons into system tray and how to create/use a popup menu.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>Download the example <a href="files/tut23.zip">here</a>.</font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CC6600"><font size=-1>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font size=-1>System tray is the rectangular
region in the taskbar where several icons reside. Normally, you'll see
at least a digital clock in it. You can also put icons in the system tray
too. Below are the steps you have to perform to put an icon into the system
tray:</font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font size=-1>Fill a <font color="#FFCC00">NOTIFYICONDATA</font>
structure which has the following members:</font></font></li>
<ul>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">cbSize</font>
The size of this structure.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">hwnd</font>
Handle of the window that will receive notification when a mouse event
occurs over the tray icon.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">uID</font>
A constant that is used as the icon's identifier. You are the one who decides
on this value. In case you have more than one tray icons, you will be able
to check from what tray icon the mouse notification is from.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">uFlags</font>
Specify which members of this structure are valid</font></font></li>
<ul>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#33CCFF">NIF_ICON</font></b>
The hIcon member is valid.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#33CCFF">NIF_MESSAGE</font></b>
The uCallbackMessage member is valid.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#33CCFF">NIF_TIP</font></b>
The szTip member is valid.</font></font></li>
</ul>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">uCallbackMessage</font>
The custom message that Windows will send to the window specified by the
hwnd member when mouse events occur over the tray icon. You create this
message yourself.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">hIcon</font>
The handle of the icon you want to put into the system tray</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">szTip</font>
A 64-byte array that contains the string that will be used as the tooltip
text when the mouse hovers over the tray icon.</font></font></li>
</ul>
<li>
<font face="Arial,Helvetica"><font size=-1>Call <font color="#FFFF00">Shell_NotifyIcon</font>
which is defined in shell32.inc. This function has the following prototype:</font></font></li>
<p><br><b><font face="Arial,Helvetica"><font color="#FFCC00"><font size=-1>
Shell_NotifyIcon PROTO dwMessage:DWORD ,pnid:DWORD</font></font></font></b>
<p><font face="Arial,Helvetica"><font size=-1> <font color="#FFFF00">dwMessage</font>
is the type of message to send to the shell.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>
<b><font color="#33CCFF">NIM_ADD</font></b> Adds an icon to the status
area.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>
<b><font color="#33CCFF">NIM_DELETE</font></b> Deletes an icon from the
status area.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>
<b><font color="#33CCFF">NIM_MODIFY</font></b> Modifies an icon in the
status area.</font></font>
<br><font face="Arial,Helvetica"><font size=-1> <font color="#FFFF00">pnid</font>
is the pointer to a <font color="#FFCC00">NOTIFYICONDATA</font> structure
filled with proper values</font></font>
<br><font face="Arial,Helvetica"><font size=-1>If you want to add an icon
to the tray, use NIM_ADD message, if you want to remove the icon, use NIM_DELETE.</font></font></ol>
<font face="Arial,Helvetica"><font size=-1>That's all there is to it. But
most of the time, you're not content in just putting an icon there. You
need to be able to respond to the mouse events over the tray icon. You
can do this by processing the message you specified in uCallbackMessage
member of NOTIFYICONDATA structure. This message has the following values
in wParam and lParam (special thanks to s__d for the info):</font></font>
<ul>
<li>
<font face="Arial,Helvetica"><font size=-1>wParam contains the ID of the
icon. This is the same value you put into uID member of NOTIFYICONDATA
structure.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1>lParam The low word contains
the mouse message. For example, if the user right-clicked at the icon,
lParam will contain WM_RBUTTONDOWN.</font></font></li>
</ul>
<font face="Arial,Helvetica"><font size=-1>Most tray icon, however, displays
a popup menu when the user right-click on it. We can implement this feature
by creating a popup menu and then call TrackPopupMenu to display it. The
steps are described below:</font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font size=-1>Create a popup menu by calling
<font color="#FFFF00">CreatePopupMenu</font>.
This function creates an empty menu. It returns the menu handle in eax
if successful.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1>Add menu items to it with <font color="#FFFF00">AppendMenu</font>,
<font color="#FFFF00">InsertMenu</font>
or <font color="#FFFF00">InsertMenuItem</font>.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1>When you want to display the
popup menu where the mouse cursor is, call GetCursorPos to obtain the screen
coordinate of the cursor and then call <font color="#FFFF00">TrackPopupMenu</font>
to display the menu. When the user selects a menu item from the popup menu,
Windows sends WM_COMMAND message to your window procedure just like normal
menu selection.</font></font></li>
</ol>
<font face="Arial,Helvetica"><font size=-1>Note: Beware of two annoying
behaviors when you use a popup menu with a tray icon:</font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font size=-1>When the popup menu is displayed,
if you click anywhere outside the menu, the popup menu will not disappear
immediately as it should be. This behavior occurs because the window that
will receive the notifications from the popup menu MUST be the foreground
window. Just call SetForegroundWindow will correct it.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1>After calling SetForegroundWindow,
you will find that the first time the popup menu is displayed, it works
ok but on the subsequent times, the popup menu will show up and close immediately.
This behavior is "intentional", to quote from MSDN. The task switch to
the program that is the owner of the tray icon in the near future is necessary.
You can force this task switch by posting any message to the window of
the program. Just use PostMessage, not SendMessage!</font></font></li>
</ol>
<h3>
<font face="Arial,Helvetica"><font size=-1>Example:</font></font></h3>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>option
casemap:none</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\shell32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\shell32.lib</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WM_SHELLNOTIFY
equ WM_USER+5</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDI_TRAY
equ 0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_RESTORE
equ 1000</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_EXIT
equ 1010</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
PROTO :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ClassName
db "TrayIconWinClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>AppName
db "TrayIcon Demo",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>RestoreString
db "&Restore",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ExitString
db "E&xit Program",0</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>hInstance
dd ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>note
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -