How to minimize windows to system pallets

Source: Internet
Author: User
Writing pallets mainly solves two problems:
(1) create, modify, and delete pallets;
(2) how to process the messages received by the tray.
This requires several Windows API functions.
First, shell_policyicon is the Shell API used for pallets. This API uses a policyicondata structure.
Type policyicondata
Cbsize as long 'the number of bytes occupied by the structure
Hwnd as long 'window pointer for receiving Tray Icon messages
UID as long 'is a program-defined icon identifier, because some programs have multiple icons
Uflags as long' indicates how to operate the tray icon, including adding, deleting, and modifying
Ucallbackmessage as long indicates the application message
Hicon as long 'tray icon pointer
Sztip as string * 64' the prompt string is displayed when the mouse points to the tray icon.
End type
Next, you should consider how to receive and process messages on the tray in VB (double-click, click, left-click, and right-key ). C ++, Delphi, and other languages are relatively simple in processing message loops. However, when processing message loops in VB, The setwindowlong and callwindowproc APIs of Win32 must be used. The setwindowlong function uses the gwl_wndproc index to create a subclass of the window class (the window class is used to create a window). It uses the addressof keyword and the callback function (windowproc) to intercept messages and execute relevant functions, such as window maximization, minimization, hiding, and exit. The callwindowproc function calls the default pointer of the original window class. When the program exits, it can use setwindowlong to close the subclass and re-Turn the original windows process into a callback function.
This project includes a module and a form.
1. Module source code:
Option explicit forces each variable to be used
Type policyicondata 'defines the notifyicondata structure.
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 is the constant shell_policyicon will use.
Public const nif_icon = & H2
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 _ policyicon function declaration
Declare function shell_policyicon lib "shell32.dll" alias "shell_policyicona" (byval dwmessage as long, lpdata as policyicondata) as long
'Structure, constant, and API Declaration used for Message Processing
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 'specifies whether the tray icon is on the desktop.
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 process is message loop Processing
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 ', the mouse clicks the tray icon
Select case lparam
Case wm_rbuttondown 'right-click and press
Form1.popupmenu form1.mainmenu
Case wm_lbuttondblclk
Form1.show 'display window
Case else
End select
Else' call the default window pointer
Windowproc = callwindowproc (lpprevwndproc, HW, umsg, wparam, lparam)
End if
End Function
Public sub hook ()
'Hook the program into the message ring
'Use addressof to get the pointer of the message processing function windowproc and pass it to setwindowlong.
'Lpprevwndproc is used to store the pointer of the original window.
Lpprevwndproc = setwindowlong (ghw, gwl_wndproc, addressof windowproc)
End sub
Public sub unhook ()
'Exit the program from the message ring. Replace the pointer of the windowproc function with the pointer of the original window, that is, close the subclass and return the message loop.
Dim temp as long
Temp = setwindowlong (ghw, gwl_wndproc, lpprevwndproc)
End sub
2. Add a Main Menu mainmenu in the form1 window and set it to invisible. Add some sub-menus, such as "display window" (Name: Show), "Hide window" (Name: Hide), and "Exit program" (Name: Exit ). Add the four buttons command1, command2, command3, and command. The caption attributes are: "delete tray icon", "Create tray icon", "Modify tray icon", and "Exit program ".
The content of the code window is:
Dim mynot as policyicondata 'defines a tray Structure
Private sub commandementclick ()
'Press the delete tray icon.
With mynot
. Hicon = form1.icon 'tray icon pointer pointing to the window icon
. Hwnd = form1.hwnd' form pointer
. Sztip = "" 'the prompt string is displayed. It should be blank when it is deleted.
. Ucallbackmessage = wm_user + 100 'corresponds to the program-defined message
. Uflags = nif_icon or nif_tip or nif_message icon
. Uid = 1' icon identifier
. Cbsize = Len (mynot) 'calculates the length of the structure instance mynot
End
HH = shell_policyicon (nim_delete, mynot) 'Delete the tray icon
Trayflag = false 'after the tray icon is deleted, the trayflag is false.
End sub
Private sub command2_click () 'press the create Tray Icon button
Dim HH as long
With mynot
. Hicon = form1.icon
. Hwnd = form1.hwnd
. Sztip = "tray icon" & CHR (& h0)
. Ucallbackmessage = wm_user+ 100
. Uflags = nif_icon or nif_tip or nif_message
. Uid = 1
. Cbsize = Len (mynot)
End
HH = shell_policyicon (nim_add, mynot) 'Add a tray icon
Trayflag = true 'after the tray icon is added, the trayflag is true.
End sub
Private sub command3_click () 'press the modify Tray Icon button
Dim HH as long
Set P = loadpicture ("CD. ICO") 'import a new icon
With mynot
. Hicon = P' change the tray icon to a new icon
. Hwnd = form1.hwnd
. Sztip = "disc icon" & CHR (& h0) 'Change Prompt information
. Ucallbackmessage = wm_user+ 100
. Uflags = nif_icon or nif_tip or nif_message
. Uid = 1
. Cbsize = Len (mynot)
End
HH = shell_policyicon (nim_modify, mynot) 'modify certain features of the tray icon
End sub
The private sub command4_click () 'exit window button is pressed
If trayflag = true then command;click' if the tray icon is still present, press the "delete tray icon" button.
Unhook 'Exit the message loop
Unload me 'unmount the form
End sub
Private sub exit_click ()
If trayflag = true then command?click' if the tray icon is still present, press the "delete tray icon" button.
Unhook 'Exit the message loop
Unload me 'unmount the form
End sub
Private sub form_load ()
Ghw = me. hwnd' get the pointer of this form
HOOK: Call the hook function to hook the self-made message processing function into the message loop of windows.
End sub
Private sub hide_click ()
Form1.hide 'hide the window
End sub
Private sub show_click ()
Form1.show window
End sub
Compile the program in VB and click the create tray icon. The icon appears in the lower right corner of the taskbar. Place the cursor over this icon to display a message. Right-click the menu, and you can select the corresponding feature. When the window is hidden, double-click the tray icon to display the window. Note: using function pointers in VB will make the program more vulnerable. Be careful when using functions and keywords related to it.
Related Article

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.