Task Bar icon programming in VB6

Source: Internet
Author: User

 

Task Bar icon programming in VB6

Yang shanhe

 

The Windows 9x desktop taskbar displays the currently running tasks, and the taskbar icon in the lower right corner provides quick access to tasks that are not currently visible. The mouse clicking or double-clicking actions are forwarded to the corresponding window through the taskbar, even if the application is invisible (the window is hidden ), the application that receives the message responds to the user's mouse action. For example, double-click the speaker icon that represents the sound effect control program. The volume control program is displayed. In fact, in Microsoft's official documents, it is called the "launch shelf", which means that all events related to the taskbar icons are "launched" to send messages to corresponding applications. This is Windows 9x for Windows 3. x's advanced features. If necessary, our program should be as compliant with the Microsoft logo requirements as much as possible. The refer taskbar icon provides a means to access applications that must run but do not always show them, because all windows standard programs are message-driven, and the message receiving must have interface elements, many applications do not have interface elements (because it is unnecessary, for example, for a scheduled task that comes with Microsoft Windows 98, it does not need to open a desktop window every moment, but sometimes we need to access it randomly and it must also run on its own ), therefore, if you need to access these applications when the application window is invisible, you must use the taskbar icon.

For C, C ++, and other languages, it is easy to program the taskbar icon: add the icon to the taskbar by calling the Shell API when creating an application window, and then hide the application window, "patiently" listens to various mouse messages. When a user clicks the taskbar icon, the system (specifically, the taskbar) transmits the message to the application by calling the form process, the application determines the operation to perform based on the specific message. Obviously, this process should be completed in the stage C/C ++ in the form process, but it is a bit difficult for VB ". There is no "Form Process" in the standard programming process in VB. In VB, only objects can be programmed, and only methods and attributes of objects can be used, therefore, when many magazine newspapers mention taskbar programming, the examples all shy away from the most popular VB language examples. In fact, using the advanced features provided by VB6, we can implement vbprogramming for the taskbar.

To implement taskbar programming in VB, you need to know the following:

The relationship between the taskbar icon, the VB application, and the taskbar. We have discussed this in general just now: the taskbar icon is added by the taskbar according to the requirements of the VB application, and operations such as deletion and update are managed by the taskbar, however, the VB Application must "Apply ". When the VB Application is invisible, the taskbar icon indicates that it receives various events and the form process processing events of the VB Application.
VB does not provide taskbar programming statements, but we can call them through APIS. Which APIs will be used will be discussed later.
For VB taskbar icon programming, the form must be "subclass". You must use the API to directly obtain/set the form process and obtain the address of the form process, this requires the VB6 operator "addressof ". For more information about the usage, see the following instance. If you want to respond to the event forwarded by the taskbar icon in the event of the application form, the result will disappoint you. The form event is not a concept similar to the event returned and forwarded by the taskbar. The former is a concept in OLE technology, and the latter is an event in the true sense after being packaged by VB technology. To respond to the task bar icon event in VB, you must "take over" the real form process of the form directly.
The taskbar icon programming is not necessary. In addition to complicated use and easy to cause system crash, too many taskbar icons affect the desktop "appearance.
Next, we will illustrate this process through a simple instance. Our example is very simple. It is to add an icon to the taskbar after loading form1 of a standard project, and then use subclass technology to intercept messages sent to the form, double-click the event with the left mouse button to send a message box indicating that the message is indeed forwarded to the application.

First, set the application startup process to sub main (). You can select project/project1 properties in the general table that appears, select "sub main" from the startup object item ".

2. Add a module to the project. Add the data structures, constants, and APIs required for task bar icon programming. To do this, you need to start the API text viewer.

The data structure to be added is ):

Public type policyicondata

Cbsize as long 'length of the current structure

Hwnd as long 'installs the form handle of the graphic application to the taskbar, and the system sends a message to the corresponding application accordingly.

The unique identifier of the UID as long icon, determined by the application that installs the graph.

What kind of behavior will the uflags as long' icon accept or present?

The callback message sent to the application by ucallbackmessage as long. The same as the system pre-defined message should be avoided and the value must be greater than wm_user.

Hicon as long 'icon handle, that is, the icon to be displayed in the taskbar

Sztip as string * 64' if the icon is allowed to display the tip, this string is displayed when you move your cursor over the icon in the taskbar for a period of time.

End type

The following constants must be added:

Public const wm_lbuttondblclk = & h203

Public const wm_user = & h400' user-defined message Base Value

 

Public const gwl_wndproc = (-4) 'During the subclass form process, you must use this parameter to determine the Offset Value of the Form Process in the Form class structure to be replaced.

Public const nim_add = & H0 'indicates to add an icon to the taskbar

Public const nim_delete = & H2 'Delete icon

Public const nim_modify = & H1 'modify the icon

Public const nif_icon = & H2 'allow icon display

Public const nif_message = & H1 'allow icon message forwarding

Public const nif_tip = & H4 'allows the icon to display the content string prompted by the icon

 

Public const uid & = 88888 'is the Identifier value of your custom icon

Public const cbnotify & = wm_user + 100 'indicates the callback message forwarded by the icon, which must be greater than wm_user.

 

The APIs used include:

Public declare function getwindowlong lib "USER32" alias "getwindowlonga" (byval hwnd as long, byval nindex as long) as long

Public declare function setwindowlong lib "USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) As long'

Public 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

'The above functions are used to subclass the form.

Public declare function shellpolicyicon lib "shell32.dll" alias "shell_policyicona" (byval dwmessage as long, lpdata as policyicondata) as long

'This function is used to add the specified icon to the taskbar and use the policyicondata data structure mentioned above.

Variables used include:

Global ghw as long 'is used to save the window handle of the VB Application.

Global lpprevwndproc as long 'saves the process address of the form before replacement

Public mynid as policyicondata is used to specify information about the icon.

The functions and forms should also be designed. For details, see the notes:

Function windowproc (byval HW as long, byval umsg as long, byval wparam as long, byval

Lparam as long) As long 'This function intercepts all messages sent to the form.

If wparam = uid then', if the auxiliary parameter in the message parameter is our custom value, it indicates that the message is forwarded by the taskbar icon.

Select case lparam 'this parameter indicates the event that occurred. If you right-click the event, a message box is displayed.

Case wm_lbuttondblclk

Msgbox "The message has been forwarded by double-clicking the mouse! ", Vbinformation," taskbar icon programming"

Form1.show 'display form

End select

End if

Windowproc = callwindowproc (lpprevwndproc, HW, umsg, wparam, lparam) 'calls the original form process

End Function

 

Public sub hook () 'is used to subscribe the form and allow the self-compiled function to intercept messages. This sub sets the new form process.

Lpprevwndproc = setwindowlong (ghw, gwl_wndproc, addressof windowproc)

End sub

 

Sub unhook () 'undefined Form Process

Dim temp as long

Temp = setwindowlong (ghw, gwl_wndproc, lpprevwndproc)

End sub

 

Public sub main ()

Load form1 'only needs to load the form

End sub

 

Next, let's take a look at how to add icons in form1.

Enter the following code in form_load:

Private sub form_load ()

Ghw = me. hwnd' initialize the global variable

Mynid. cbsize = Len (mynid) 'Fill in mynid

Mynid. hicon = me. icon

Mynid. hwnd = ghw

Mynid. sztip = "my application icons"

Mynid. ucallbackmessage = cbpolicy

Mynid. uflags = nif_icon or nif_message or nif_tip

Mynid. uid = uid

Shellpolicyicon nim_add, mynid add to taskbar icon

Hook 'to set a new form

End sub

In the form uninstallation process, you need to remove the icon and remove the process of intercepting the form.

Private sub form_unload (cancel as integer)

Shellpolicyicon nim_delete, mynid 'delete the taskbar icon

Unhook' unhook

End sub

Now you can save the project and run it. You can see that a common form icon is added to the "launch shelf" in the taskbar, that is, you "manually" added. You can double-click it to see what will happen. If no message box pops up or the system crashes, it indicates that you entered the code incorrectly. Please carefully study the code in this example.

Finally, let's summarize the following things that need to be paid attention to throughout the process:

The windowproc function (New Form Process) must be named in the. Bas module and the parameters must be correct. No errors (whether syntactically or logically) are allowed in the function ). The original form process must be called at the end of this function, otherwise... (it will make you very ugly !!!)
During debugging, if you want to terminate the application, do not use the debug button of VB. You can only use the close button of the Form. Otherwise, VB will crash due to Invalid Pointer usage.
Subclass technology allows you to freely intercept form messages with powerful functions, but be careful not to abuse them. After all, it has many side effects.
Due to the limitations of VB, we can only use Windows API to complete some special tasks. When using APIs, you must note that the API parameters are correct and in which module the declaration is made.
In this example, the message forwarded by the taskbar icon is very simple. It only serves as a "Demo". You can add a lot of your own ideas and try it out. I am waiting for your good news!

--

1998/1999? No.

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.