How to display icons on pallets in the lower right corner of the system in VB6

Source: Internet
Author: User

Attribute vb_name = "modtray"
'-----------------------------------------
'Code in the module is as follows:
'-----------------------------------------
Option explicit
Public oldwindowproc as long
Public theform as form
Public themenu as menu
'[VB Declaration]
'Destare 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
'[Description]
'The process of sending a message to a window using this function
'[Return value]
'Long, varies based on the sent message
'[Parameter table]
'Lpprevwndfunc ----- long, the original window process address
'Hwnd ------------ long, window handle
'Msg ------------ long, sent message
'Wparam ----------- long, message type. For details, refer to the wparam parameter table.
'Lparam ----------- long, which varies with the wparam Parameter
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
'[VB Declaration]
'Private declare function setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
'[Description]
'Set the information for the specified window in the window structure
'[Return value]
'Long, the previous value of the specified data
'[Parameter table]
'Hwnd ----------- long, the handle of the window for which you want to obtain information
'Nindex --------- long, please refer to the description of the nindex parameter of the getwindowlong Function
'Dwnewlong ------ long, the new value of the window information specified by nindex
Declare function setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
'[VB Declaration]
'Destare function shell_policyicon lib "shell32.dll" alias "shell_policyicona" (byval dwmessage as long, lpdata as same yicondata) as long
'[Description]
'[Parameter table]
'Parameter dwmessage ---- set the message value. It can be the following common values: 0, 1, and 2.
'Nim _ add = 0 Add the icon to the system status bar
'Nim _ modify = 1. Modify the icon in the system status bar.
'Nim _ Delete = 2 Delete the icon in the system status bar
The 'lpdata-parameter is used to input the policyicondata data Data Structure Variable. We also need to define its structure in the "module" as follows:
'Type policyicondata
'Cbsize as long 'must be filled with the length of the policyicondata data Data Structure
'Hwnd as long is set as the window handle
'Uid as long is the ID value set by the icon
'Uflags as long is used to set the following three parameters: ucallbackmessage, hicon, and sztip.
'Ucallbackmessage as long message No.
'Hicon as long 'icon displayed on the status bar
'Sztip as string * 64 prompt information
'End type
'---- The ucallbackmessage, hicon, and sztip parameters should also be declared as the following constants in the module:
'Public const nif_message = 1
'Public const nif_icon = 2
'Public const nif_tip = 4
Declare function shell_policyicon lib "shell32.dll" alias "shell_policyicona" (byval dwmessage as long, lpdata as policyicondata) as long
Public const wm_user = & H400
Public const wm_lbuttonup = & H202
Public const wm_mbuttonup = & h208
Public const wm_rbuttonup = & h205
Public const tray_callback = (wm_user+ 1001 &)
Public const gwl_wndproc = (-4)
Public const gwl_userdata = (-21)
Public const nif_icon = & H2
Public const nif_tip = & h4
Public const nim_add = & H0
Public const nif_message = & H1
Public const nim_modify = & H1
Public const nim_delete = & H2
'Record the Data Type of the data set Tray Icon policyicondata
Public type policyicondata
Cbsize as long
Hwnd as long
UID as long
Uflags as long
Ucallbackmessage as long
Hicon as long
Sztip as string * 64
End type
'Thedata variable record set Tray Icon data
Private thedata as policyicondata
'*************************************** ******
'New window procedure -- the address of the window function is changed using the setwindowlong function in the main program, and the message is switched to newwindowproc for processing.
'*************************************** ******
Public Function newwindowproc (byval hwnd as long, byval MSG as long, byval wparam as long, byval lparam as long) as long
'If the user clicks the icon in the tray, it determines whether the left button is clicked or right-click
If MSG = tray_callback then
'If you left click
If lparam = wm_lbuttonup then
'At this time, the form state is minimized.
If theform. windowstate = vbminimized then _
'Restore to the form state before Minimization
Theform. windowstate = theform. laststate
Theform. setfocus
Exit Function
End if
End if
'If you right-click
If lparam = wm_rbuttonup then
', Right-click the menu
Theform. popupmenu themenu
Exit Function
End if
End if
'If messages of other types are passed to the original default Window Function
Newwindowproc = callwindowproc (oldwindowproc, hwnd, MSG, wparam, lparam)
End Function
'*************************************** ******
'Add the icon of the main form (the form1.icon attribute can be changed) to the tray.
'*************************************** ******
Public sub addtotray (FRM as form, MNU as menu)
'Save the current form and menu Information
Set theform = FRM
Set themenu = MNU
'Gwl _ wndproc: Obtain the window function address of the window.
Oldwindowproc = setwindowlong (FRM. hwnd, gwl_wndproc, addressof newwindowproc)
'Knowledge bit by bit: hwnd attribute
'Return the form or control handle. Syntax: object. hwnd
'Note: in the Microsoft Windows operating environment, each form and control in the application is provided.
'Assign a handle (or hwnd) to identify them. The hwnd attribute is used for Windows API calls.
'Add the main form icon to the tray
With thedata
. Uid = 0' forgot? Refer to the previous content, uid icon serial number, which is useful for animation icons.
. Hwnd = FRM. hwnd
. Cbsize = Len (thedata)
. Hicon = FRM. Icon. Handle
. Uflags = nif_icon 'indicates the icon to be set.
. Ucallbackmessage = tray_callback
. Uflags =. uflags or nif_message 'indicates to set the icon or return information to the main form. This sentence cannot be omitted.
. Cbsize = Len (thedata) 'Why? When adding an icon, we need to make it return information
End with 'to the main form. Or means setting and returning messages at the same time.
Shell_policyicon nim_add, thedata 'according to the previous definition of nim_add, set to "add mode"
End sub
'*************************************** ******
'Delete the icon in the system tray
'*************************************** ******
Public sub removefromtray ()
'Delete the icons in the tray
With thedata
. Uflags = 0
End
Shell_policyicon nim_delete, thedata 'according to the previous definition of nim_delete, set to "delete mode"
'Restore the original settings
Setwindowlong theform. hwnd, gwl_wndproc, oldwindowproc
End sub
'*************************************** ******
'Add a floating prompt to the icon in the tray (that is, the prompt note that appears when the mouse moves up)
'*************************************** ******
Public sub settraytip (tip as string)
With thedata
. Sztip = tip & vbnullchar
. Uflags = nif_tip 'indicates that you want to set the floating prompt
End
Shell_policyicon nim_modify, thedata 'according to the previous definition of nim_modify, set to "Modify mode"
End sub
'*************************************** ******
'Set the tray icon (not used in this example. It is very useful if you want to dynamically change the icon displayed in the tray)
'Example: 1. display the animation icon. (You must have guessed it! Use the timer control to continuously call this process. Place the animation in the PIC array)
'2. Different icons are displayed when the program is in different States. The method is similar.
'Try it if you are interested.
'*************************************** ******
Public sub settrayicon (PIC as picture)
'Determine if the image is saved as an icon
If pic. Type <> vbpictypeicon then exit sub
'Change the icon to the icon stored in the pic.
With thedata
. Hicon = pic. Handle
. Uflags = nif_icon
End
Shell_policyicon nim_modify, thedata
End sub

 

 

 

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.