From:
Http://www.blogjava.net/Unmi/archive/2008/03/23/188040.html
Some of our programs want to display icons at the tray to minimize to the system bar; close button to not close the program, also to minimize to the system bar; click the tray icon to activate the window, exit the program through the pop-up menu of the tray icon.
This section of code is implemented by SWT.
The code is provided directly, and the code has more detailed comments to illustrate the functions and implementation of this program. In this article, you should know which part of the taskbar and system bar it refers to. Microsoft defines it like this. You can use the findwindow of spyxx to check it out.
Package com. unmi;
Import org. Eclipse. SWT .*;
Import org. Eclipse. SWT. Events .*;
Import org. Eclipse. SWT. Graphics .*;
Import org. Eclipse. SWT. Widgets .*;
/**
* SWT 3.0 introduced tray. you can place your program icon in the system bar.
* This program provides four functions:
* 1. Click the minimize or close button of the window to hide the window. The window is not displayed in the taskbar and does not exit the program.
* 2. When the window is hidden, there is no icon in the taskbar and an icon in the system bar. When the window is displayed, the opposite is true.
* 3. When hiding a window, you can click the system bar icon or click "display window" in the system bar to display the window.
* 4. The program can only exit by clicking the "Exit program" menu item in the system bar. The X button in the window is invalid.
* @ Author unmi
*
*/
Public class trayexample {
Public static void main (string [] ARGs ){
Display display = New Display ();
// Disable the maximize button
Final shell = new shell (display, SWT. shell_trim ^ SWT. max );
Shell. settext ("trayexample ");
// Retrieve the preset icons in the system, saving you the need to add an icon file during testing.
Shell. setimage (display. getsystemimage (SWT. icon_working ));
// Construct the system bar Control
Final tray = display. getsystemtray ();
Final trayitem = new trayitem (tray, SWT. None );
// When the program starts, the window is displayed, so the system bar icon is hidden.
Trayitem. setvisible (false );
Trayitem. settooltiptext (shell. gettext ());
Trayitem. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
Toggledisplay (shell, tray );
}
});
Final menu traymenu = new menu (shell, SWT. pop_up );
Menuitem showmenuitem = new menuitem (traymenu, SWT. Push );
Showmenuitem. settext ("display window (& S )");
// Display the window and hide the icon in the system bar
Showmenuitem. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Toggledisplay (shell, tray );
}
});
Traymenu. setdefaultitem (showmenuitem );
New menuitem (traymenu, SWT. separator );
// Exit menu in the system bar. The program can only exit through this menu
Menuitem exitmenuitem = new menuitem (traymenu, SWT. Push );
Exitmenuitem. settext ("Exit Program (& X )");
Exitmenuitem. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Shell. Dispose ();
}
});
// The Event displayed when you right-click the System icon.
Trayitem. addmenudetectlistener (New menudetectlistener (){
Public void menudetected (menudetectevent e ){
Traymenu. setvisible (true );
}
});
Trayitem. setimage (shell. getimage ());
// Register the window event listener
Shell. addshelllistener (New shelladapter (){
// When you click the Minimize button, the window is hidden and the icon is displayed in the system bar.
Public void shelliconified (shellevent e ){
Toggledisplay (shell, tray );
}
// When the close button is clicked, the program is not terminated, while the window is hidden and the icon is displayed in the system bar.
Public void shellclosed (shellevent e ){
E. doit = false; // consume the events handled by the original system
Toggledisplay (shell, tray );
}
});
Shell. setsize (320,240 );
Center (Shell );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
Display. Dispose ();
}
/**
* When the window is visible, the window is hidden and the icon in the system bar is deleted.
* When the window is hidden, the window is displayed and the icon is displayed in the system bar.
* @ Param shell window
* @ Param tray system bar icon Control
*/
Private Static void toggledisplay (shell, tray ){
Try {
Shell. setvisible (! Shell. isvisible ());
Tray. getitem (0). setvisible (! Shell. isvisible ());
If (shell. getvisible ()){
Shell. setminimized (false );
Shell. setactive ();
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Center window display
* @ Param shell refers to the window to be displayed.
*/
Private Static void Center (shell ){
Monitor = shell. getmonitor ();
Rectangle bounds = monitor. getbounds ();
Rectangle rect = shell. getbounds ();
Int x = bounds. x + (bounds. Width-rect. width)/2;
Int y = bounds. Y + (bounds. Height-rect. Height)/2;
Shell. setlocation (x, y );
}
}
The implementation result is as follows:
When the left figure is displayed in a window, there is no icon in the system bar, and there is an icon in the taskbar. The picture on the right shows that only the system bar has icons when the window is hidden.
After that, I saw the translation software lingoes lingers in the following forms:
1. There are icons in the system bar at any time
2. The Minimize button does not hide the window, but is minimized to the taskbar.
3. The close button will not close the program, but will be minimized to the system bar.
4. The program can only be closed through the pop-up menu item "exit" of the tray icon.
Reference: http://www.eclipseworld.org/bbs/read-cec-tid-15458-fpage-9.html
However, there is still a problem: how can we make the window visible without displaying anything in the taskbar?