Taskbar icon programming in VB6

zhaozj2021-02-16  58

Taskbar icon programming in VB6

Yangshan River

The Windows 9x desktop taskbar displays the currently running task, and the taskbar icon in the lower right corner provides quick access to the currently invisible task. Our mouse click or double-click the action to transfer to the corresponding window through the task bar, even if the app is noteworth (the window is hidden), the application that receives the message responds to the user's mouse action. For example: We double-click the speaker icon representing the sound control program, pop up the "Volume Control" program. In fact, in Microsoft's official document, it is called "launcher", which means that all events for the task bar icon are "transmitted", allowing the corresponding application to get messages. This is the advanced feature of Windows 3.x for Windows 3.x. If we have necessary, our program should try to match the Microsoft logo requires the jujube taskbar icon to provide a means of accessing but not always showing applications, because all All Windows standard programs are messaging, and the reception of the message must require interface elements to exist, but there is no interface element (because there is no need, such as Microsoft Windows 98 comes with the planned task, it does not need each moment) Open a desktop window, but sometimes we need to access it randomly, and it must also run it yourself), so if you need to access these applications if you need to access these applications, you must pass the taskbar icon.

If you have a language to implement a taskbar icon for C or C , it is very simple to implement the taskbar icon: Add the icon to the taskbar when you create an application window, then hide the application window, "Patience" listening to various mouse messages When the user performs a mouse operation on the taskbar icon, the system (specifically the taskbar) passes this message to the application in the way in which the form process is called, and the application determines what operations in accordance with the specific message. Obviously, the process should be completed in the performance site of the form of the form, but for the VB language, some "it is difficult". There is no "form process" in the VB bid programming process, and the object can only be programmed to the object, and only the methods, attributes, attributes, etc. of the object can be used, so many magazine newspaper referred to the taskbar, and the examples are avoided. The most popular VB language example. In fact, using the advanced feature provided by VB6, we can implement VB programming of the taskbar.

Taskbar programming in VB requires the following points:

The relationship between the task bar icon is between the VB application, the taskbar. This we have just discussed much: The taskbar icon is a taskbar management, delete, update, etc., which should be added, deleted, updated by the taskbar, but must be VB applications. When the VB application is invisible, the taskbar icon represents it receives various events, the form of the VB application's form process. The taskbar programming statement is not provided in VB, but we can be implemented via API calls. Which APIs should be used later. The VB taskbar icon programming must be used to "subcatenal", to use the API to obtain / set the form process, but also get the address of the form process, which must be used to the operator provided by VB6 " Addressof. The specific usage is subsequently provided. If you want to respond to events forwarded by the taskbar icon in the event of an application form, the result will be disappointed. The event of the form is not a concept with the task bar response and forwarded, the former is the concept in OLE technology, after the "package" of VB technology, the latter is a true event. In response to the event of the taskbar icon in VB, the true form of the "Take Over" form is directly "taken". The task bar icon programming is not necessary when necessary, in addition to the use process, which is more complicated, which is easy to cause the system crash, too many taskbar icons affect the desktop "City" is also a reason. Below, we will explain this process through a simple example. Our example is simple, just add an icon to the task bar before a standard engineering FORM1 load, load the form, use the subclassization technology to intercept the message to the form, and double click on the event in the left mouse button. Send a message box indicates that the message is indeed sent to the application. First, set the startup process of the application to Sub Main (), which can select "Sub Main" from the Startup Object item by selecting Project / Project1 Properties. In the pop-up GENERAL table, select "Sub Main".

Second, a module is added to the project. To join the data structure, constant, and API claimed to join the taskbar icon programming. To do this, you need to start the API Text Viewer.

The data structure that needs to be added is (see note of the corresponding meaning):

Public Type Notifyicondata

CBSIZE AS long 'in the length of this structure

HWND as long 'to install the form handle of the application of the icon to the taskbar, the system sends a message to the corresponding application

The unique identifier of the UID as long 'icon, determined by the application of the installation icon

What kind of behavior will be accepted or expressed in uflags as long?

UCALLBACKMESSAGE AS long 'Send the callback message to the application, should avoid the same system predefined message, should value greater than WM_USER

Hicon as long 'icon handle, you want the icon that is displayed in the taskbar

Sztip as string * 64 'If the icon is allowed to display TIP, then the string will be displayed when the mouse stays in the taskbar.

End Type

The constant statements that need to be added are:

Public const wm_lbuttondblclk = & h203 'mouse left click code

Public const wm_user = & h400 'user-defined base value

Public const GWL_WndProc = (-4) When the 'subcatenutical process, this parameter must be used to determine the offset value of the form process in the form class structure that needs to be replaced.

Public const nim_add = & h0 'means to join the icon to the taskbar

Public const nim_delete = & h2 'delete icon

Public const nim_modify = & h1 'modified icon

Public const nif_icon = & h2 'Allow icon Show public const nif_message = & h1' Allow icon message forwarding

Public const nif_tip = & h4 'Allow icon display icon prompt content string

Public const uid & = 88888 'The identity value of the icon you customize

Public const cbnotify & = wm_user 100 'icon forwarded callback message must be greater than WM_USER

The API used is:

Public Declare Function GetWindowlong Lib "User32" Alias ​​"getWindowlonga" (Byval Nindex as long) As long

Public Declare Function SetWindowlong Lib "User32" Alias ​​"Setwindowlonga" (Byval Nindex As Long, Byval Dwnewlong As Long) AS Long '

Public Declare Function CallWindowProc LIB "User32" Alias ​​"CallWindowProca" (Byval Hwndfunc As Long, Byval Hwnd As Long, Byval Msg As Long, Byval WParam As Long) As long

'Above these functions for subclass forms

Public Declare Function Shellnotifyicon lib "shell32.dll" Alias ​​"shell_notifyecona" (Byval DwMessage As Long, LPDATA AS Notifyicondata) AS Long

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

The use of variables are:

Global GHW as long 'is used to save the window handle of the VB application

Global LPPREVWNDPROC AS long 'Save the replaced form process address

Public mynid as notifyicondata 'information for a given icon

You should also design a function and a form, see more comments:

Function WindowProc (Byval HW 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 is our custom value, it indicates that this message is forwarded by the taskbar icon.

Select Case LParam 'This parameter indicates that an event has occurred. If you double-click the right click, you will pop up a message box.

Case WM_LBUTTONDBLCLK

Msgbox "has been forwarded to the mouse double-click message!", Vbinformation, "taskbar icon programming"

Form1.show 'Display Form

End SELECT

END IF

WindowProc = CallWindowProc (LPPREVWndProc, HW, UMSG, WPARAM, LPARAM) 'Call the original form process

END FUNCTION

Public Sub Hook () 'is used for subclassics, allowing self-compiled functions, this SUB sets new form process lpprevwndproc = setWindowlong (GHW, GWL_WndProc, Addressof WINDOPROC)

End Sub

Sub unHook () 'Releases custom form procedures

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

Below, let's take a look at how to add icons in Form1.

Enter the following code in Form_Load ():

Private sub flow_load ()

GHW = me.hwnd 'Initializing global variable

Mynid.cbsize = len (mynid) 'Pack MYNID

Mynid.hicon = me.icon

Mynid.hwnd = GHW

Mynid.sztip = "My Application Icon"

MYNID.UCALLBACKMESSAGE = CBNOTIFY

MYNID.UFLAGS = NIF_ICON OR NIF_MESSAGE OR NIF_TIP

Mynid.uid = UID

ShellNotifyicon Nim_add, Mynid 'joins the taskbar icon

Hook 'Settings New Form Process

End Sub

During the uninstallation of the form, you need to remove the icon and the process of uninterrupted the form.

Private Sub Form_Unload (Cancel AS Integer)

ShellNotifyicon Nim_Delete, Mynid 'Delete Task Bar Icon

UnHook 'releases hooks

End Sub

Ok, now you can save the project, then run. You can see an ordinary form icon in the "Lunner" of the taskbar, that is, you are addointed. You can double-click it and what will happen. If there is no message box, or the system crashes, indicating that you are incorrect when entering the code, please study this example code carefully.

Finally, we have to summarize what the following processes need to pay attention to:

The WindowProc function (new form process) must be. The name of the BAS module and the parameters must be correct, and any errors are not allowed in the function (whether or logically logically on the syntax). The last must call the original form, otherwise. . . (Will make you dying !!!) When you want to terminate the application, please do not use the VB debug button, you can only use the form of shut down button, otherwise, due to the failure of the pointer to make VB collapse. Subclassics allow you to intercept form messages, powerful, but pay attention to not abuse, after all, there are many side effects. Due to VB constraints, we can only complete some special tasks through the Windows API mode. Using the API, you need to pay attention to the correctness of the API parameter and which module declaration. This example is very simple for the message forwarded by the taskbar icon. Only "demo" effect, you can join many your own ideas, try, I am waiting for your good news!

-

1998/1999? I don't remember.

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

New Post(0)