ICZelion TUT23

zhaozj2021-02-11  192

Tutorial 23: TRAY ICON

.

Download the Example

Here.

Theory: 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:

Fill a Notifyicondata Structure Which Has The Following Members:

cbSize The size of this structure. hwnd Handle of the window that will receive notification when a mouse event occurs over the tray icon. uID 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 Icons, You Will Be Able To Check From What Tray Icon The Mouse Notification IS from. Uflags Specify Which Members of this Structure Are Valid

NIF_ICON The hIcon member is valid. NIF_MESSAGE The uCallbackMessage member is valid. NIF_TIP The szTip member is valid. UCallbackMessage 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. hIcon The handle of the icon you want to put into the system tray szTip A 64-byte array that contains the string that will be used as the tooltip text when the mouse hovers over the tray icon. Call Shell_NotifyIcon which is defined in shell32 .inc This function has the following prototype:. Shell_NotifyIcon PROTO dwMessage: DWORD, pnid: DWORD dwMessage is the type of message to send to the shell NIM_ADD Adds an icon to the status area NIM_DELETE Deletes an icon from the status area NIM_MODIFY... Modifies an icon in the status area. Pnid is the pointer to a notifyicondata structure flyd with prot values ​​if you want to add an icon to the tray, use NIM_ADD message, if you want to remove the icon, use NIM_DELETE. 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).:

wParam contains the ID of the icon. This is the same value you put into uID member of NOTIFYICONDATA structure. lParam The low word contains the mouse message. For example, if the user right-clicked at the icon, lParam will contain WM_RBUTTONDOWN. 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:.. Create a popup menu by calling CreatePopupMenu. This function creates an empty menu. It returns the menu handle in eax if successful. Add menu items to it with AppendMenu, InsertMenu or InsertMenuItem. 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 TrackPopupMenu 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 Note:.. Beware Of Two Annoying Behaviors WHEN You Use a Popup Menu with a TRAY ICON:

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. 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 Example..!:

.386

.Model flat, stdcall

Option CaseMAP: NONE

INCLUDE /MASM32/INCLUDE/Windows.inc

INCLUDE /MASM32/INCLUDE/USER32.INC

INCLUDE /MASM32/INCLUDE / WANEL32.INC

INCLUDE /MASM32/INCLUDE/SHELL32.INC

INCLUDELIB /MASM32/LIB/USER32.LIB

INCLUDELIB /MASM32/LIB/kernel32.lib

INCLUDELIB /MASM32/LIB/Shell32.lib

WM_SHELLNOTIFY EQU WM_USER 5 IDI_TRAY EQU 0 IDM_RESTORE EQU 1000 IDM_EXIT EQU 1010 Winmain Proto: DWORD,: DWORD,: DWORD,: DWORD

.data classname DB "TRAYICONWINCLASS", 0 Appname DB "Trayicon Demo", 0 RestoreString DB "& Restore", 0 EXITSTRING DB "E & Xit Program", 0

.DATA? HINSTANCE DD? NOTE NOTIFYICONDATA <> HPOPUPMENU DD?

. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Winmain, Hinstance, Null, Null, SW_SHOWDEFAULT INVOKE EXITPROCESS, EAX

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS mov wc.lpfnWndProc , OFFSET WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInst pop wc.hInstance mov wc.hbrBackground, COLOR_APPWORKSPACE mov wc.lpszMenuName, NULL mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon , eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx, WS_EX_CLIENTEDGE, aDDR ClassName, aDDR AppName, / WS_OVERLAPPED WS_CAPTION WS_SYSMENU WS_MINIMIZEBOX WS_MAXIMIZEBOX WS_VISIBLE, CW_USEDEFAULT , / CW_USEDEFAULT, 350, 200, NULL, NULL, / HINST, NULL MOV HWND, EAX .While True Invoke GetMessage, Addr MSG, NULL, 0, 0 .Break .if (! EAX) Invoke TranslateMessage, Addr Msg Invoke DispatchMessage, Addr Msg .Endw Mov Eax, Msg.wParam Ret Winmain Endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM LOCAL pt: POINT .if uMsg == WM_CREATE invoke CreatePopupMenu mov hPopupMenu, eax invoke AppendMenu, hPopupMenu, MF_STRING, IDM_RESTORE, addr RestoreString invoke AppendMenu, hPopupMenu, MF_STRING , IDM_EXIT, addr ExitString .elseif uMsg == WM_DESTROY invoke DestroyMenu, hPopupMenu invoke PostQuitMessage, NULL .elseif uMsg == WM_SIZE .if wParam == SIZE_MINIMIZED mov note.cbSize, sizeof NOTIFYICONDATA push hWnd pop note.hwnd mov note.uID, IDI_TRAY mov note.uFlags, NIF_ICON NIF_MESSAGE NIF_TIP mov note.uCallbackMessage, WM_SHELLNOTIFY invoke LoadIcon, NULL, IDI_WINLOGO mov note.hIcon, eax invoke lstrcpy, addr note.szTip, addr AppName invoke ShowWindow, hWnd, SW_HIDE invoke Shell_NotifyIcon, NIM_ADD, addr Note .ndif .elseif uMSG == WM_COMMAND .if lParam == 0 invoke Shell_NotifyIcon, NIM_DELETE, addr note mov eax, wParam .if ax == IDM_RESTORE invoke ShowWindow, hWnd, SW_RESTORE .else invoke DestroyWindow, hWnd .endif .endif .elseif uMsg == WM_SHELLNOTIFY .if wParam == IDI_TRAY .if lParam == WM_RBUTTONDOWN invoke GetCursorPos, addr pt invoke SetForegroundWindow, hWnd invoke TrackPopupMenu, hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, NULL, hWnd, NULL invoke PostMessage, hWnd, WM_NULL, 0,0 .elseif lParam = =

WM_LBUTTONDBLCLK INVOKE SendMessage, HWND, WM_COMMAND, IDM_RESTORE, 0.endif.ndif.else Invoke DefWindowProc, Hwnd, UMSG, WPARAM, LPARAM RET .Endif xor Eax, Eax Ret WndProc Endped Start

Analysis: The program will display a simple window When you press the minimize button, it will hide itself and put an icon into the system tray When you double-click on the icon, the program will restore itself and remove the icon from the.. System TRAY. WHEN You Right-Click ON IT, A Popup Menu Is Displayed. You Can Choose To Restore The Program OR Exit IT.

.if uMsg == WM_CREATE invoke CreatePopupMenu mov hPopupMenu, eax invoke AppendMenu, hPopupMenu, MF_STRING, IDM_RESTORE, addr RestoreString invoke AppendMenu, hPopupMenu, MF_STRING, IDM_EXIT, addr ExitString

When the main window is created, IT Creates a popup menu and append two menu items. Appendmenu has the folloading syntax:

Appendmenu Proto Hmenu: DWORD, UFLAGS: DWORD, UidNewItem: DWORD, LPNEWITEM: DWORD

hMenu is the handle of the menu you want to append the item to uFlags tells Windows about the menu item to be appended to the menu whether it is a bitmap or a string or an owner-draw item, enabled, grayed or disable etc. You can get the complete list from win32 api reference. In our example, we use MF_STRING which means the menu item is a string. uIDNewItem is the ID of the menu item. This is a user-defined value that is used to represent the menu item . lpNewItem specifies the content of the menu item, depending on what you specify in uFlags member. Since we specify MF_STRING in uFlags member, lpNewItem must contain the pointer to the string to be displayed in the popup menu.

After the Popup Menu Is Created, The Main Window Waits Patiently for the User To Press Minimize Button.when A Window Is Minimized, IT Receives WM_SIZE Message with size_minimized value in wparam.

.elseif uMsg == WM_SIZE .if wParam == SIZE_MINIMIZED mov note.cbSize, sizeof NOTIFYICONDATA push hWnd pop note.hwnd mov note.uID, IDI_TRAY mov note.uFlags, NIF_ICON NIF_MESSAGE NIF_TIP mov note.uCallbackMessage, WM_SHELLNOTIFY invoke LoadIcon, NULL, IDI_WINLOGO MOV Note.HICON, EAX Invoke Lstrcpy, Addr Note.sztip, Addr Appname Invoke ShowWindow, Hwnd, SW_HIDE INVOKE Shell_notifyicon, NIM_ADD, AddR Note .endif

We use this opportunity to fill NOTIFYICONDATA structure. IDI_TRAY is just a constant defined at the beginning of the source code. You can set it to any value you like. It's not important because you have only one tray icon. But if you will put several icons into the system tray, you need unique IDs for each tray icon. We specify all flags in uFlags member because we specify an icon (NIF_ICON), we specify a custom message (NIF_MESSAGE) and we specify the tooltip text (NIF_TIP). WM_SHELLNOTIFY is just a custom message defined as WM_USER 5. The actual value is not important so long as it's unique. I use the winlogo icon as the tray icon here but you can use any icon in your program. Just load it from the resource with LoadIcon and put the returned handle in hIcon member. lastly, we fill the szTip with the text we want the shell to display when the mouse is over the icon. We hide the main window to give the illusion of "minimizing-to-tray- Icon "APPEARANCE. Next We call shell_notifyicon w ith NIM_ADD message to add the icon to the system tray.Now our main window is hidden and the icon is in the system tray. If you move the mouse over it, you will see a tooltip that displays the text we put into szTip member. Next, IF you double-click at the icon, The main window will review.

.elseif uMsg == WM_SHELLNOTIFY .if wParam == IDI_TRAY .if lParam == WM_RBUTTONDOWN invoke GetCursorPos, addr pt invoke SetForegroundWindow, hWnd invoke TrackPopupMenu, hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, NULL, hWnd, NULL invoke PostMessage, HWnd, WM_NULL, 0, 0 .ELSEIF LPARAM == WM_LButtondblclk Invoke SendMessage, Hwnd, WM_COMMAND, IDM_RESTORE, 0.endif.ndif.

When a mouse event occurs over the tray icon, your window receives WM_SHELLNOTIFY message which is the custom message you specified in uCallbackMessage member. Recall that on receiving this message, wParam contains the tray icon's ID and lParam contains the actual mouse message. In the code above, we check first if this message comes from the tray icon we are interested in. If it does, we check the actual mouse message. Since we are only interested in right mouse click and double-left-click, we process only WM_RBUTTONDOWN and WM_LBUTTONDBLCLK messages. If the mouse message is WM_RBUTTONDOWN, we call GetCursorPos to obtain the current screen coordinate of the mouse cursor. When the function returns, the POINT structure is filled with the screen coordinate of the mouse cursor. By screen coordinate, I mean the Coordinate in the entire screen without regarding to any window boundary. for example, if the screen resolution is 640 * 480, the right-lower corner of the screen is x == 639 and y == 479 . If you want to convert the screen coordinate to window coordinate, use ScreenToClient function. However, for our purpose, we want to display the popup menu at the current mouse cursor position with TrackPopupMenu call and it requires screen coordinates, we can use the coordinates Filled by getCursorpos Directly. TRACKPOPUPMENU HAS The Following Syntax: TRACKPOPUPMENU PROTO HMENU: DWORD, UFLAGS: DWORD, X: DWORD, Y: DWORD, NRESERVED: DWORD, HWND: DWORD, PRCRECT: DWORD

hMenu is the handle of the popup menu to be displayed uFlags specifies the options of the function. Like where to position the menu relative to the coordinates specified later and which mouse button will be used to track the menu. In our example, we use TPM_RIGHTALIGN to position the popup menu to the left of the coordinates. x and y specify the location of the menu in screen coordinates. nReserved must be NULL hWnd is the handle of the window that will receive the messages from the menu. prcRect is the rectangle in the screen where it is possible to click without dismissing the menu. Normally we put NULL here so when the user clicks anywhere outside the popup menu, the menu is dismissed. When the user double-clicks at the tray icon, we send WM_COMMAND message to our own window specifying IDM_RESTORE to emulate the user selects Restore menu item in the popup menu thereby restoring the main window and removing the icon from the system tray. in order to be able to receive double click me ssage, the main window must have CS_DBLCLKS style.invoke Shell_NotifyIcon, NIM_DELETE, addr note mov eax, wParam .if ax == IDM_RESTORE invoke ShowWindow, hWnd, SW_RESTORE .else invoke DestroyWindow, hWnd .endif

When the user selects Restore menu item, we remove the tray icon by calling Shell_NotifyIcon again, this time we specify NIM_DELETE as the message. Next, we restore the main window to its original state. If the user selects Exit menu item, we also remove The icon from the THE AND DESTROY THE Main window by calling destroywindow.

转载请注明原文地址:https://www.9cbs.com/read-4672.html

New Post(0)