Windows taskbar Program Development

Source: Internet
Author: User

I. Introduction


The taskbar is a well-known concept. It refers to the display of running programs on the Windows desktop.
A bar area.

Computer Knowledge Network
It.qc99.com

I
Generally speaking, the taskbar consists of three parts: the "Start" button on the left and the display area of the running program in the middle (Windows98 also has a shortcut toolbar ), the rightmost is the taskbar prompt area.
Domain. The so-called taskbar programming mainly refers to programming the taskbar prompt area. Windows allows you to place your application icons in the taskbar prompt area and customize the operations you need.

 
Placing the application icon in the taskbar prompt area facilitates the communication between users and applications, and enables the application to give users a prompt in a more visual form. Generally, place the application icon
In the taskbar prompt area, this application is a background program. For example, when printing the background in windows, a printer icon will display in the taskbar prompt area, prompting the user that the printer process is normal
Run in the background. At present, many software use the taskbar programming technology, such as NetAnts and go! Zilla, Kingsoft, and so on, this is because these software has some "background" Nature, another
On the one hand, the application icon is placed in the taskbar prompt area, making human-computer interaction easier.

At present, many programming languages can program the taskbar, such as V, VB
. However, I found that few articles systematically discuss taskbar programming from principle to program implementation. Most of the documents only provide several API function statements and
. However, task bar programming involves not only APIs, but also callback functions and other more complex issues. Therefore, it is necessary to systematically discuss this issue.

  II.
Taskbar Programming Implementation Technology

The basic idea of Windows taskbar programming is as follows: (1) When an application is started by Calling API functions
Put the program icon in the taskbar prompt area, modify the icon feature when the program is running, and delete the icon from the taskbar when the application is closed; (2) control the application by using the callback function. This section mainly discusses
API to control icon issues. The next section will focus on callback functions.

1. shell_policyicon Function

Its statement
Private declare function shell_policyiconlib "shell32" alias
"Shell_policyicona" (byval dwmessage as long, pnid as policyicondata)
Long

This function sends a message to the system for adding, modifying, and deleting the taskbar prompt area icon. The system processes the message according to the sent message. This function is the taskbar prompt area.
At the core of programming, you can easily compile programs that meet the requirements. The parameter meanings in this function are as follows:

(1) parameter dwmessage (byval
Dwmessage as long)

This parameter notifies the System of the following operations:
Nim_add
Prompt for adding an icon to the taskbar
Zone
Nim_delete
Delete icon
Nim_modify
Message with changed sending icon feature

(2)
Parameter pnid (pnid as policyicondata)

The icon feature data, which is defined as follows:
Private
Type policyicondata
Cbsize as long 'the size
Hwnd as long
'Processing the window handle of the icon notification message
UID as long 'application-defined icon ID
Uflags as long
'Is used to set whether ucallbackmessage, hicon, sztip, and other columns are valid. Generally, the combination of nif_icon or nif_tip or is used.
Nif_message, indicating all valid
Ucallbackmessage as long
'Message number. In the future, when a user clicks the mouse on the icon, the callback function will be processed with the Message notification message.
Hicon as long 'icon handle
Sztip
String * 64' prompt message
End type

2. Add the icon

In the form_load () event of the startup form
Add the following code to add an icon in the taskbar prompt area when the program is running:

Dim NID as policyicondata
Nid. cbsize
= Len (NID)
Nid. hwnd = me. hwnd
Nid. uid = 9998
Nid. uflags =
Nif_icon + nif_tip + nif_message
Nid. hicon = me. icon
Nid. ucallbackmessage
= Wm_user+ 100
Nid. sztip = "welcome to the taskbar"
Shell_policyicon nim_add, nid'

3. Delete the icon

In the form_unload () event, add the following code to delete the icon when the program is closed. Note that
The UID and hwnd used must be exactly the same as the UID and hwnd used when the icon was added.

Dim NID as policyicondata
Nid. cbsize
= Len (NID)
Nid. hwnd = me. hwnd
Nid. uid = 9998
Shell_policyicon
Nim_delete, NID

4. Modify the icon

The following code modifies the icon. The UID and hwnd must be the same
The
UID and hwnd are exactly the same. You must also set uflags and hicon correctly. Uflags must be set to nif_icon, indicating that the modification is valid for the icon; hicon should be set
New icon.

Dim NID as policyicondata
Nid. cbsize = Len (NID)
Nid. hwnd
= Me. hwnd
Nid. uid = 9999
Nid. uflags = nif_icon
Nid. hicon =
Image1.picture
Shell_policyicon nim_modify, NID

Modify the prompt message and Message ID
The method is similar. The difference is that uflags must be set to nif_tip when the message is modified, and uflags must be set to nif_message when the message number is modified.

 
 Iii. Callback Functions


As mentioned above, when you press the mouse on the icon, the system will receive and pass the message to the window message
Callback Function for specific processing. You can perform different program processing on different messages in the callback function to customize the functions of your own application (for example, when you press left, a menu is displayed, and you can press right.
And double-click the mouse to display the main window ). When an icon is added to the taskbar prompt area, the message number umessage is used to tell the callback function that the message comes from the program corresponding to the icon,
The callback function can be processed accordingly. The callback function is a complex issue. The following describes the implementation of the callback function in taskbar programming starting with the running mechanism of the VB program.

  
1. VB Application Running Mechanism

The running of VB Applications is based on Windows message passing mechanism. Windows is a multi-task, multi-line
The way the VB program runs is controlled by windows. Specifically, when a VB program is running, the Windows Process Management module creates a process for it. Process and
Communication between windows is implemented through message passing: A process sends a message to Windows and waits. After Windows finishes processing the message, the result is sent to the process. Because
Windows supports multithreading. Therefore, a process can create multiple threads internally. These threads share the address space, global variables, files, and various signals of the process. In addition to the shared address space
Threads are independent. Each thread has its own program counters, stacks, registers, and States (waiting, ready, and running ). When the vbprogram is running, it creates a thread for each of its controls to concurrently
Process control events. A control corresponds to a thread in the process. These threads also communicate with windows through messages. Windows provides a clear solution for applications running on it.
Information processing program. For example, when a VB program is running, right-click a text box and a shortcut menu containing "cut", "copy", and "Paste" will pop up, which is saved by windows.
Information processing program. Windows allows users to intercept messages in the application window and process them in a custom process. This custom process is called a "Callback" function.
To process the messages you are interested in, and send other messages to the system for processing. This "substitution" seems very difficult, but it is actually completely achievable.

 
2. Use the callback function to process messages in the prompt area of the taskbar.

Callback Function (callback
A function provided by an application to a Windows system DLL or called by another DLL. It is generally used to intercept messages, obtain system information, or process asynchronous events. Application calls back
The number of address pointers tell the DLL, And the DLL will call this function when appropriate. The callback function must comply with the pre-defined parameter format and transmission method. Otherwise, when a DLL is called, it will cause the program or System
Crash.

Generally, the callback function uses Windows
The call method of the API, that is, _ stdcall. Of course, the DLL compiler can customize the call method, but the client program must also comply with the same rules. Parameter of the function in the _ stdcall Mode
The number is pushed to the stack in the order from right to left. Except for specifying a pointer or reference, all parameters are passed by value. Before the function is returned, the parameter is automatically popped up from the stack.

Map the application
To open the application window, you must click the icon in the taskbar prompt area. However, this does not mean that the window is automatically displayed after the user clicks. Every user point
By clicking the icon, the system notifies the message processing callback function of the window in the form of a message, so you only need to program the callback function in the application window.

Window message callback
The function format is as follows:
Function wndproc (byval hwnd as long, byval MSG as long,
Byval wparam as long, byval lparam as long) as long

Because the callback function is customized
Function, so the function name and parameter name can be customized. The meanings of parameters are shown in the following table:
Hwnd
Window handle
MSG
Equal to the original call
Value of ucallbackmessage set when shell_policyicon is used
Wparam
Equal to the original call
The UID value set when shell_policyicon is used.
Lparam
Similar to the mouse message, for example, wm_lbuttondown (left mouse button pressed ).

 
There are many restrictions on using callback functions in VB:

1. The callback function must be placed in a standard module instead of a class module or form code;
 
2. The addressof operator can only be used for custom procedures, functions, or attributes. It cannot be used for external functions declared by the declare statement, nor for functions in the Type Library;
 
3. The processes, functions, and attributes written after addressof must be in the same project with the relevant declarations and processes;
4. debugging 10 because the callback function needs to interact directly with the System
Difficulty. If an error occurs in the callback function, illegal operations may occur, causing the VB runtime environment to crash.

The specific steps for using a callback function in VB to process messages in the taskbar prompt area are as follows:
Below:

(1) Add the following code to the form_load () event of the startup form.

Prevwndproc =
Getwindowlong (Me. hwnd, gwl_wndproc) 'obtains the handle of the window message processing function that is saved by the system.
Setwindowlong
Me. hwnd, gwl_wndproc, addressof wndproc 'specifies wndproc as the new message processing function
'Add icon
Dim
NID as policyicondata
Nid. cbsize = Len (NID)
Nid. hwnd = me. hwnd
Nid. uid
= 9998
Nid. uflags = nif_icon + nif_tip + nif_message
Nid. hicon =
Me. icon
Nid. ucallbackmessage = wm_user + 100 'defines the message number
Shell_policyicon
Nim_add, nid'

(2) Add the callback function wndproc code to the module

Function
Wndproc (byval hwnd as long, byval MSG as long, byval wparam as long,
Byval lparam as long) as long
If MSG = wm_user+ 100 then
''This message is the ucallbackmessage defined when shell_policyicon is called.
If lparam =
Wm_lbuttondown or lparam = wm_rbuttondown then
'When you press the left or right-click button, the menu is displayed.
Frmcaution. popupmenu
Frmcaution. mnuaa
End if
End if
'Other messages are sent to the system for processing, and prevwndproc is the original window
Handle of the processing function
Wndproc = callwindowproc (prevwndproc, hwnd, MSG, wparam,
Lparam)
End Function

When the above code is executed, the API functions getwindowlong (),
The declarations of setwindowlong () and callwindowproc () must be added to the module first. These declarations can be obtained through the "API text Browser" in VB.

 
 4. instance analysis: a scheduled music reminder Program


For a person engaged in computer work, sitting in front of a computer for a long time
Work is a common task. Persistent screen watching often causes eye fatigue. It is best to take a rest at intervals. However, computer work, especially programming, tends to make people concentrate and forget the time. We can
Design a scheduled music reminder program to automatically remind you to take a break at intervals.

The following describes the specific program implementation based on the aforementioned taskbar programming ideas. This program
You can play a music reminder and a music reminder at a specific time point at intervals. You can customize the reminder time, reminder content, and sound effects.

1. Create
A project and five forms are added, respectively named as frmcaution, frmtipmess, frmsound, frmchangtime, and frmabout.
. Frmcaution is the start form for displaying reminder messages, frmtipmess is the form for user-defined reminder content, and frmsound is the window for user-defined reminder sound
Body, frmchangtime is the User-Defined reminder time form, frmabout is the "about" information display form.
 
2.
Add a menu "mnuaa" to the frmcaution and add the menu "set reminder time..." under "mnuaa ......" , "Set reminder message ......" , "Sound ......" , "Off
Yu ......" And "exit ". The menu "mnuaa" is used as the pop-up menu when the icon is clicked. Add a panel to frmcaution and name it "pnlmessage ",
Displays messages. Add a multimedia control to frmcaution and name it "mm" for playing music. Add a time control "time1" to frmcaution ".

 
3. Add a standard module mdlcommon to the project and place the following code in it:

'Api function declaration
Declare Function
Shell_policyicon lib "shell32.dll" alias "shell_policyicona" (byval
Dwmessage as long, lpdata as policyicondata) 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
Getwindowlong lib "USER32" alias "getwindowlonga" (byval hwnd as long,
Byval nindex as long) as long
Declare function setwindowlong lib
"USER32" alias "setwindowlonga" (byval hwnd as long, byval nindex
Long, byval dwnewlong as long) as long
Public declare Function
Setwindowpos lib "USER32" (byval hwnd as long, byval hwndinsertafter
Long, byval X as long, byval y as long, byval CX as long, byval Cy
Long, byval wflags as long) as long

Public prevwndproc as long
'Existing window message processing functions
Public message as string 'message content
Public tipinterval
Integer 'prompt Interval
Public tiptime as string 'prompt time
Public time
Integer 'cumulative time

Function wndproc (byval hwnd as long, byval MSG
Long, byval wparam as long, byval lparam as long) as long
If MSG =
Wm_user+ 100 then
If lparam = wm_lbuttondown or lparam =
Wm_rbuttondown then
'When you press the left or right-click button, the menu is displayed.
Frmcaution. popupmenu
Frmcaution. mnuaa
End if
End if
'Other messages are sent to the system for processing. prevwndproc is the original window message.
Handle of processing functions
Wndproc = callwindowproc (prevwndproc, hwnd, MSG, wparam,
Lparam)
End Function
The above procedures omit the definition of data structures and constants. This definition can be obtained from "API text Browser.

 
4. Compile the form_load () process of frmcaution.

Private sub form_load ()
Load
Frmsound
Prevwndproc = getwindowlong (Me. hwnd, gwl_wndproc)
'Retrieve the handle of the window message processing function saved by the System
Setwindowlong me. hwnd, gwl_wndproc, addressof
Wndproc 'specifies wndproc as the new message processing function
'Add icon
Dim NID as policyicondata
Nid. cbsize
= Len (NID)
Nid. hwnd = me. hwnd
Nid. uid = 9998
Nid. uflags =
Nif_icon + nif_tip + nif_message
Nid. hicon = me. icon
Nid. ucallbackmessage
= Wm_user + 100 'defines the message number
Shell_policyicon nim_add, NID
'Set the display page
Pnlmessage. Left
= (Me. scalewidth-pnlmessage. width)/2
Pnlmessage. Top =
(Me. scaleheight-pnlmessage. Height)/2
Tipinterval = 30
'The default reminder interval is 30 minutes.
Tiptime = "" 'the default reminder time is null.
Time = 0
Message =
"Dear Master, we recommend that you take a 5-minute break first! "
Mm. filename = frmsound! Filsound. List (0)
End sub

 
5. Compile the form_unload () process of frmcaution.

Private sub form_unload (cancel
As integer)
Dim NID as policyicondata
'Restore the original window message processing function
Setwindowlong
Me. hwnd, gwl_wndproc, prevwndproc
'Delete the icon
Nid. cbsize = Len (NID)
Nid. hwnd
= Me. hwnd
Nid. uid = uid
Shell_policyicon nim_delete, NID
Mm. Command
= "Close"
End sub
6. Compile the timer1_timer () process.
Private sub
Timer1_timer ()
If tipinterval <> 0 then', if the reminder interval is set
Time =
Time + 1' cumulative time plus 1 second
If time> = tipinterval * 60 then' the reminder interval has reached
Pnlmessage. Caption
= Message
Timer1.enabled = false
Mm. filename = mm. filename
Mm. Command
= "Open"
Mm. Command = "play"
Time = 0
Frmcaution. Show
'Total window display
Display at the beginning
Setwindowpos me. hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nosize or
Swp_nomove
End if
End if
If tiptime <> "" then' is set
Dim
Nowtime as date
Dim desttime as date
Nowtime = format (now (),
"HH: NN: SS ")
Desttime = format (tiptime, "HH: NN: SS ")
If nowtime =
Desttime then' the scheduled time has reached
Pnlmessage. Caption = message
Timer1.enabled =
False
Mm. filename = mm. filename
Mm. Command = "open"
Mm. Command
= "Play"
Frmcaution. Show
Setwindowpos me. hwnd, hwnd_topmost, 0,
0, 0, 0, swp_nosize or swp_nomove
End if
End if
End sub

 
The above shows the general process of the scheduled music reminder program. When the program runs, a reminder window pops up every 30 minutes, and a piece of music is played at the same time. When you click
The icon will pop up a menu, and you can select the function to be executed. Some other functions of this program, such as "setting the reminder time form" and "setting the sound effect", are not closely related to the topic of this article.
.
The timer music reminder program is successfully debugged in 6.0 Chinese Enterprise Edition and Windows 98. If you need a complete source program, contact the author by email.

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.