以下代码主要是在swt控件播放html文件。
package unmi;import java.util.Properties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
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 MainFrame extends Composite {
public static Variant variant = null; public static OleAutomation oa = null; public static void main(String[] args) { Display display = Display.getDefault();
final Shell shell = new Shell(display);
Properties properties = System.getProperties();
String porjectPath = properties.getProperty("user.dir").replaceAll(
"\\\\", "/");
//播放一个html文件
new MainFrame(shell, SWT.NONE, porjectPath + "/html/default.html"); shell.setText("多媒体广告互动平台");
shell.setImage(new Image(display, "D:/1.gif")); shell.setLayout(new FillLayout());
shell.layout();
// 构造系统栏控件
final Tray tray = display.getSystemTray();
final TrayItem trayItem = new TrayItem(tray, SWT.NONE); // 程序启动时,窗口是显示的,所以系统栏图标隐藏
trayItem.setVisible(false);
trayItem.setToolTipText(shell.getText()); final Menu trayMenu = new Menu(shell, SWT.POP_UP);
MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);
showMenuItem.setText("显示窗口(&s)"); // 显示窗口,并隐藏系统栏中的图标
showMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
toggleDisplay(shell, tray);
}
}); trayMenu.setDefaultItem(showMenuItem); new MenuItem(trayMenu, SWT.SEPARATOR); // 系统栏中的退出菜单,程序只能通过这个菜单退出
MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
exitMenuItem.setText("退出程序(&x)"); exitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
}); // 在系统栏图标点击鼠标右键时的事件,弹出系统栏菜单
trayItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
trayMenu.setVisible(true);
}
}); trayItem.setImage(shell.getImage()); //在shell中添加一个键盘事件,如果没有new MainFrame(红色标记的那个代码)对象,该事件可用。如果new了,就不能用
shell.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
evt.doit = false;
if (evt.keyCode == SWT.ESC) {
System.out.println("dddd");
toggleDisplay(shell, tray);
}
}
}); shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
} public MainFrame(Composite parent, int style, String htmlFile) {
super(parent, style);
initGUI(htmlFile);
} public void initGUI(String htmlFile) {
try {
FillLayout thisLayout = new FillLayout();
this.setLayout(thisLayout);
{
OleFrame oleFrame1 = new OleFrame(this, SWT.NONE);
try {
OleClientSite site = new OleClientSite(oleFrame1, SWT.NONE,
"Shell.Explorer"); site.doVerb(OLE.OLEIVERB_SHOW);
oa = new OleAutomation(site);
variant = new Variant(htmlFile);
oa.invoke(104, new Variant[] { variant });
} catch (SWTException e) {
String sr = "创建OleClientSite发送错误,原因:" + e.toString();
System.out.println(sr);
return;
}
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
} private static void toggleDisplay(Shell shell, Tray 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();
}
}
}