In Windows, there are many application icons in the lower-right corner. The window of the program itself is hidden, and if you need to invoke the application's window, double-click the icon. This program is called a pallet program. This is a major feature of the WINDOWS98 operating system. Enables users to display and hide applications more quickly, so that the taskbar doesn't get too messy. How is the pallet program implemented in Visual Basic?
Writing a pallet program solves two main problems:
(1) Create, modify and delete pallets;
(2) How to handle the message received by the pallet.
This will take a few Windows API functions.
First, Shell_NotifyIcon is the Shell API for pallets. The API uses a NOTIFYICONDATA structure.
Type NOTIFYICONDATA
cbSize As Long 该结构所占字节数
hwnd As Long 接收托盘图标消息的窗口指针
uID As Long 由程序定义的图标识别符,因为有的程序有多个图标
uFlags As Long 对托盘图标操作的标志,包括添加、删除、修改
uCallbackMessage As Long 标志应用程序的消息
hIcon As Long 托盘图标指针
szTip As String * 64 当鼠标指到托盘图标时提示字符串
End Type
Second, should consider how to receive in VB, processing tray message (double-click, click, left, right button). C + +, Delphi and other languages to the message loop processing is simpler, but in VB processing message loop must apply Win32 SetWindowLong, CallWindowProc these two API functions. The SetWindowLong function uses the GWL_WNDPROC index to create a subclass of a window class (a window class is used to create a window) that uses the AddressOf keyword and the callback function (WINDOWPROC) to intercept the message and perform the corresponding function based on the message. such as maximizing the window, minimizing, hiding, exiting, and so on. The CallWindowProc function calls the default pointer of the original window class, which, when the program finally exits, closes the subclass by SetWindowLong to make the original Windows procedure a callback function.
This program project includes a module and a form
1, the module source code is:
Option Explicit force definition for each variable used
Type NOTIFYICONDATA 定义结构NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
The following are the constants that Shell_NotifyIcon will use
Public Const NIF_ICON = &H r> Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1
Shell_NotifyIcon's function declaration
Declare Function shell_notifyicon Lib "Shell32.dll" Alias "Shell_notifyicona" (ByVal dwmessage as Long, lpdata as Notifyic OnData) as Long
Structure, constants, API declarations that are used to process messages
Type POINTAPI
x As Long
y As Long
End Type
Type Msg
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Const WM_USER = &H400
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = -4
Public trayflag As Boolean 定义托盘图标是否在桌面上
Global lpPrevWndProc As Long
Global gHW As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
The following procedure handles the message loop
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If hw = Form1.hwnd And uMsg = WM_USER+100 Then检测到鼠标点动托盘图标
Select Case lParam
Case WM_RBUTTONDOWN 鼠标右键按下
Form1.PopupMenu Form1.mainmenu 弹出菜单
Case WM_LBUTTONDBLCLK 鼠标左键双击
Form1.Show 显示窗口
Case Else
End Select
Else 调用缺省窗口指针
WindowProc = CallWindowProc(lpPrevWndProc, hw,uMsg, wParam, lParam)
End If
End Function
Public Sub hook()
To hook a program into the message ring
Use AddressOf to get the pointer to the message handler function WindowProc and pass it to SetWindowLong
lpPrevWndProc A pointer to store the original window
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub Unhook()
Exits the program from the message ring. Replace the pointer of the WindowProc function with the pointer of the original window, closing the subclass, exiting the message loop
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub