In fact, writing a Tray program in Borland C ++ Builder 5.0 is quite simple. When Borland C ++ Builder 5.0 is installed, select Full (Full). After the installation is complete, a TrayIcon component is displayed on the Samples component page, which encapsulates the Window API functions required for compiling Tary, using this component to write a Tary program requires no additional code.
First, let's take a look at the common attributes of the TrayIcon component:
Attribute description
When the value of Animate is set to true, the icon set for the Icons attribute is changed sequentially.
AnimateInterva set the time for changing the icon (unit: milliseconds)
When Hide is set to true, the title bar hidden on the taskbar is minimized.
Hint sets the prompt message that appears when you move the cursor over the Tary icon.
IconIndex
Icons set the icon source (usually set an ImageList component)
PopupMenu set the menu to be popped up (usually set a PopupMenu component)
PopupMenuOn settings trigger the mouse operation of the pop-up menu
RestoreOn: Specifies the mouse operation to minimize recovery to normal state.
When Visible is set to true, the icon is displayed on the rightmost side of the taskbar.
Mouse operation instructions:
Value description
ImClick
ImDoubleClick
ImLeftClickUp click the left mouse button to open
ImLeftDoubleClick double-click the left mouse button
ImMouseDown press the mouse key
ImMouseUp press the mouse key to open
ImNone
Right-click imRightClickUp and open
Right-click imRightDoubleClick
The events and methods of the TrayIcon component are not described in detail.
Let's compile a simple Tary program:
1. Create a project and add a TrayIcon component, a PopupMenu component, and an ImageList component. Their Name attributes use the default names TrayIcon1, PopupMenu1, and imagelist1.
2. Set the attributes of TrayIcon1 as follows:
Attribute Value linux
Animate true
AnimateInterva 1000
Hide true
Hint Tary demo
IconIndex 0
Icons ImageList1
Name TrayIcon1
PopupMenu PopupMenu1
PopupMenuOn imRightClickUp
RestoreOn imDoubleClick
Visible true
3. Double-click PopupMenu1 to bring up the menu designer and add several menu items as needed.
4. Double-click ImageList1 and add the supported images (*. ico, *. bmp ).
At this point, there is no need to write a program code, and a simple Tary program is ready. Press F9 to compile and run the program. When you move the mouse over Tary, a prompt message "Tary demo program" appears. Right-click Tary and choose Popmenu1 from the shortcut menu. Then, press the Minimize button in the program window, after the program is minimized, the title bar on the taskbar is hidden. double-clicking Tary will minimize the recovery program, and the Tary icon will change at a rate of 1000 milliseconds (1 second. Is it easy enough ?!
The demo source program of the TaryIcon component is CBuilder5ExamplesAppsTrayIcon under the Borland C ++ Builder 5.0 installation directory. If you do not want to use the TaryIcon component to write Tary and only want to use API functions, you can refer to the source program under the installation directory CBuilder5ExamplesControlsTraydemo.
The shell_policyicon (DWORD dwMessage, PNOTIFYICONDATA pnid) function is used to place icons in the taskbar. The second parameter is a struct with a callback message, so you need to customize a message.
# Define wm_policyicom WM_USER + 5
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER (wm_policyicom, TMessage, policyicom)
END_MESSAGE_MAP (TForm)
The three statements are macro definitions. For more information, see help.
Then, write the defined response function for the wm_policyicom message.
// This function is used to respond to the operation of the mouse over the icon
Void _ fastcall TMainForm: policyicom (Tmessage msg)
{
POINT MousePos;
Switch (msg. LParam)
{
Case WM_RBUTTONUP: // right-click the response
......
}
}
Void _ fastcall TMainForm: TrayMessage (DWORD dwMessage)
{
NOTIFYICOMDATA tnd;
Tnd. cbSize = sizeof (policyicomdata );
Tnd. hWnd = Handle;
Tnd. uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
// The preceding program is used to implement the callback message.
Tnd. uCallbackMessage = wm_policyicom;
// This variable is used to load icons.
Tnd. hIcon = myIcon-> Handle;
Lstrcpyn (tnd. szTip, "my TrayIcom", sizeof (tnd. tip ));
Return (shell_policyicon (dwMessage, & tnd ));
}
This is probably the case. The details are unknown. Read the book.