Using SWT to implement system pallets)

Source: Internet
Author: User
Eclispe's SWT and jface features are very powerful, and they can develop pretty nice GUI applications.
This article uses SWT to implement the system tray function of the Windows taskbar. Right-click the tray menu, and hide the display of the window on the taskbar when the window is minimized.

Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Events. selectionevent;
Import org. Eclipse. SWT. Events. selectionlistener;
Import org. Eclipse. SWT. Events. shelllistener;
Import org. Eclipse. SWT. Graphics. image;
Import org. Eclipse. SWT. Internal. win32. OS;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. event;
Import org. Eclipse. SWT. Widgets. listener;
Import org. Eclipse. SWT. Widgets. Menu;
Import org. Eclipse. SWT. Widgets. menuitem;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. Tray;
Import org. Eclipse. SWT. Widgets. trayitem;

Public class systemtray extends shell {

/**
* Launch the application
*
* @ Param ARGs
*/
Public static void main (string ARGs []) {
Try {
Display display = display. getdefault ();
Systemtray shell = new systemtray (display, SWT. shell_trim );
Shell. createsystemtray (Shell );
Shell. open ();
Shell. layout ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Create the shell
*
* @ Param display
* @ Param Style
*/
Public systemtray (display, int style ){
Super (display, style );
Createcontents ();
}

/**
* Create contents of the window
*/
Protected void createcontents (){
Settext ("SWT application ");
Setsize (500,375 );

}

Protected void checksubclass (){
// Disable the check that prevents subclassing of SWT Components
}

Public void createsystemtray (final shell ){
// The effect of the following two statements is: not displayed on the taskbar
Final int hwnd = shell. Handle;
OS. setwindowlong (hwnd, OS. gwl_exstyle, OS. ws_ex_captionokbtn );
Shell. addshelllistener (New shelllistener (){
Public void shelldeactivated (Org. Eclipse. SWT. Events. shellevent e ){
System. Out. println ("shelldeactivated ");
}

Public void shellactivated (Org. Eclipse. SWT. Events. shellevent e ){
System. Out. println ("shellactivated ");
}

Public void shellclosed (Org. Eclipse. SWT. Events. shellevent e ){
System. Out. println ("shellclosed ");
}

/**
* Sent when a shell is un-minimized.
*/
Public void shelldeiconified (Org. Eclipse. SWT. Events. shellevent e ){
System. Out. println ("shelldeiconified ");
}

/**
* Event after sent when a shell is minimized. Shell is minimized
*/
Public void shelliconified (Org. Eclipse. SWT. Events. shellevent e ){
// The task bar is not displayed when the value is minimized.
Shell. setvisible (false );
System. Out. println ("shell. setvisible (false );");
}

});

Display display = shell. getdisplay ();
// Image = new image (display, 16, 16 );
Image image = new image (display, "images/toc_closed.gif ");
Final tray = display. getsystemtray ();
Final trayitem = new trayitem (tray, SWT. None );
Trayitem. settooltiptext ("SWT trayitem ");
Trayitem. addlistener (SWT. Show, new listener (){
Public void handleevent (event ){
System. Out. println ("show ");
}
});
Trayitem. addlistener (SWT. Hide, new listener (){
Public void handleevent (event ){
System. Out. println ("hide ");
}
});

// Tray Click Event
Trayitem. addlistener (SWT. Selection, new listener (){

Public void handleevent (event ){
System. Out. println ("selection ");
// Shell. setvisible (true );
// Shell. setminimized (false );
}
});

// Double-click the tray event
Trayitem. addlistener (SWT. defaultselection, new listener (){
Public void handleevent (event ){
System. Out. println ("Default selection ");
Shell. setvisible (true );
Shell. setminimized (false );
}
});

// Right-click a tray and choose
Final menu = new menu (shell, SWT. pop_up );

Menuitem menuitemmaximize = new menuitem (menu, SWT. Push); // maximize the menu
Menuitemmaximize. settext ("maximize ");
Menuitemmaximize. addselectionlistener (New selectionlistener (){
Public void widgetselected (selectionevent e ){
Shell. setvisible (true );
Shell. setmaximized (true );
}

Public void widgetdefaselecselected (selectionevent e ){
// Widgetselected (E );
}

});

Menuitem menuitemminimize = new menuitem (menu, SWT. Push); // minimizes the menu
Menuitemminimize. settext ("minimize ");
Menuitemminimize. addselectionlistener (New selectionlistener (){
Public void widgetselected (selectionevent e ){
Shell. setminimized (true );
}

Public void widgetdefaselecselected (selectionevent e ){
// Widgetselected (E );
}
});

New menuitem (menu, SWT. separator); // delimiter

Menuitem menuitemclose = new menuitem (menu, SWT. Push); // close the menu
Menuitemclose. settext ("close ");
Menuitemclose. addselectionlistener (New selectionlistener (){
Public void widgetdefaselecselected (selectionevent e ){
Display. getcurrent (). Close ();
}

Public void widgetselected (selectionevent e ){
Widgetdefaselecselected (E );
}
});

Trayitem. addlistener (SWT. menudetect, new listener (){
Public void handleevent (event ){
System. Out. println ("shell. isvisible ():" + shell. isvisible ());
Menu. setvisible (true );
If (shell. isvisible ()){
Menu. getitem (0). setenabled (true );
Menu. getitem (1). setenabled (true );
} Else {
Menu. getitem (0). setenabled (true );
Menu. getitem (1). setenabled (false );
}
}
});
Trayitem. setimage (image );

}

}

Reprinted! :) Reprinted please indicate the source: http://blog.csdn.net/techyang
(End)

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.