OLE和ActiveX控件的支持
OLE(Object Link Embeded)是指在程序之間鏈接和嵌入對象數據。通過OLE技術可以在一個應用程序中執行其他的應用程序。
而ActiveX控件是OLE的延伸,一般用於網絡。
SWT中涉及OLE和ActiveX的類都在org.eclipse.swt.ole.win32包中,使用最常見的是OleFrame類、OleClientSite類和OleControlSite類。
1. OLE控件的面板類(OleFrame)
該類繼承自Composite類,相當於一個放置OLE控件的面板。
該類的主要功能有以下幾點:
◆ 對OLE控件進行布局的設置,相當於一個普通的面板類。
◆ 可以為控件添加菜單。
◇ 設置和獲取文件菜單:setFileMenus(MenuItem[] fileMenus)和getFileMenus()。
◇ 設置和獲取容器菜單:setContainerMenus(MenuItem[] containerMenus)和getContainerMenus()。
◇ 設置和獲取窗口菜單:setWindowMenus(MenuItem[] windowMenus)和getWindowMenus()。
◆ 既然可以獲取OLE控件的菜單,就可以對菜單項進行控制,例如設置可見和設置加速鍵等。
創建一個OleFrame對象的方法:
frame = new OleFrame(sShell, SWT.NONE);
// 為控件設置菜單項
frame.setFileMenus(fileItem);
2. OLE控件類(OleClientSite和OleControlSite)
OleClientSite對象和OleControlSite對象都是OLE控件,其中OleClientSite為OLE控件,OleControlSite為ActiveX控件,因為OleControlSite類繼承自OleClientSite類。
若想創建OLE對象,有兩種常用的構造方法:
◆ OleClientSite(Composite parent, int style, String progId):progId為標示應用系統的字符,例如,Word的progId為“Word.Document”,Excel的為“Excel.Sheet”,IE的為“Shell.Explorer”。若要查看其他應用程序的progId,可以查看系統注冊表。
OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
◆ OleClientSite(Composite parent, int style, File file):file為保存的某一個文件。用這種方法創建的OLE控件,系統會根據文件自動查找對應打開的應用程序。
File file = new File("F://Temp.doc");
OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, file);
創建了一個OLE控件,接下來需要打開控件,才可以顯示控件。使用doVerb(int verb)方法。其中verb有以下可以選擇的參數:
◇ OLE.OLEIVERB_PRIMARY:打開編輯狀態的OLE控件。
◇ OLE.OLEIVERB_SHOW:顯示OLE控件。
◇ OLE.OLEIVERB_OPEN:在另外一個窗口中打開OLE控件。
◇ OLE.OLEIVERB_HIDE:隱藏OLE控件。
◇ OLE.OLEIVERB_INPLACEACTIVATE:不帶有工具欄和菜單欄的方式。
◇ OLE.OLEIVERB_UIACTIVATE:激活OLE的菜單欄和工具欄。
◇ OLE.OLEIVERB_DISCARDUNDOSTATE:關閉OLE控件。
例如,以下代碼可以打開編輯狀態的OLE控件:
clientSite.doVerb(OLE.OLEIVERB_PRIMARY);
當OLE對打開的文件修改后,通過isDirty()方法可以判斷是否已經修改過。如果修改后,可以使用save(File file, boolean includeOleInfo)方法進行保存。例如:
if(clientSite.isDirty()) {
clientSite.save(file, true);
}
創建ActiveX控件對象要使用OleControlSite類,該類只有一個構造方法:
OleControlSite(Composite parent, int style, String progId):只能根據progId來創建。例如,創建一個Word的ActiveX控件對象的代碼如下:
OleControlSite controlSite = new OleControlSite(frame, SWT.NONE, "Word.Document");
3. OLE程序示例:
該程序的功能是:可以選擇打開Word文檔,然后進行編輯后保存該文檔。
import java.io.File; public class OleSample { private Shell sShell; private MenuItem[] fileItem;//OLE的菜單項 private OleClientSite clientSite;//OLE控件對象 private OleFrame frame;//OLE的面板的對象 private File openFile;//打開的文件 public static void main(String[] args) { Display display = Display.getDefault(); OleSample thisClass = new OleSample(); thisClass.createSShell(); thisClass.sShell.open(); while (!thisClass.sShell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } thisClass.clientSite.dispose(); display.dispose(); } private void createSShell() { sShell = new Shell(); sShell.setText("OLE Sample"); sShell.setLayout(new FillLayout()); createMenu(); sShell.setSize(new Point(300, 200)); } //創建OLE控件對象 private void createOle() { frame = new OleFrame(sShell, SWT.NONE); frame.setFileMenus(fileItem); // 設置文件菜單 if (openFile != null) clientSite = new OleClientSite(frame, SWT.NONE, openFile); clientSite.doVerb(OLE.OLEIVERB_PRIMARY); } private void createMenu() { Menu main = new Menu(sShell, SWT.BAR); MenuItem file = new MenuItem(main, SWT.CASCADE); file.setText("文件(&F)"); Menu fileMenu = new Menu(file); fileItem = new MenuItem[2]; fileItem[0] = new MenuItem(fileMenu, SWT.PUSH); fileItem[0].setText("打開"); fileItem[1] = new MenuItem(fileMenu, SWT.PUSH); fileItem[1].setText("保存"); file.setMenu(fileMenu); sShell.setMenuBar(main); fileItem[0].addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(sShell, SWT.OPEN); dialog.setFilterExtensions(new String[] { "*.doc", "*.*" }); String file = dialog.open(); if (file != null) { openFile = new File(file); //打開OLE控件 createOle(); } } }); fileItem[1].addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { //如果打開文件被修改過 if (clientSite.isDirty()) { //創建一個臨時文件 File tempFile = new File(openFile.getAbsolutePath() + ".tmp"); openFile.renameTo(tempFile); //如果保存成功,則刪除臨時文件,否則恢復到臨時文件保存的狀態 if (clientSite.save(openFile, true)) tempFile.delete(); else tempFile.renameTo(openFile); } } }); } }
效果
顯示效果:
打開一個Word文檔后:
“文件”菜單為在代碼中定義的菜單,其他菜單為Word的菜單。
swt制作的播放器
package org.flash.player; import java.awt.BorderLayout; import java.io.File; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; 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.OleControlSite; 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.wb.swt.SWTResourceManager; public class WMP extends Composite { private OleAutomation player; /** * Create the composite * @param parent * @param style */ public WMP(Composite parent, int style) { super(parent, style); setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); createContents(); } protected void createContents() { setLayout(new FillLayout()); OleControlSite controlSite; try { OleFrame frame = new OleFrame(this, SWT.NO_TRIM); frame.setLayoutData(new BorderLayout(0, 0)); frame.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE)); controlSite = new OleControlSite(frame, SWT.None, "WMPlayer.OCX.7"); controlSite.setBackgroundImage(SWTResourceManager.getImage(WMP.class, "/img/1.jpg")); controlSite.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); controlSite.setRedraw(true); controlSite.setLayoutDeferred(true); controlSite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); controlSite.pack(); } catch (SWTError e) { System.out.println("Unable to open activeX control"); return; } player = new OleAutomation(controlSite); setFocus(); setUIMode("none"); File file = new File("."); if (file.exists()) { File fs[] = file.listFiles(); addPlayList(fs); } setMode("loop", true); play(); } public boolean loadFile(String URL) { int[] ids = player.getIDsOfNames(new String[] { "URL" }); if (ids != null) { Variant theFile = new Variant(URL); return player.setProperty(ids[0], theFile); } return false; } public void setUIMode(String s) { int ids[] = player.getIDsOfNames(new String[] { "uiMode" }); if (ids != null) { player.setProperty(ids[0], new Variant(s)); } } public void setVolume(int v) { int value = getVolume(); OleAutomation o = getSetting(); ; int id[] = o.getIDsOfNames(new String[] { "volume" }); if (id != null) { int vv = v + value; if (vv > 100) { vv = 100; } o.setProperty(id[0], new Variant(vv)); } } public int getVolume() { int value = 0; OleAutomation o = getSetting(); int id[] = o.getIDsOfNames(new String[] { "volume" }); if (id != null) { Variant vv = o.getProperty(id[0]); if (vv != null) value = vv.getInt(); } return value; } /** * autoRewind Mode ———indicating whether the tracks are rewound to the * beginning after playing to the end. Default state is true. * * loop Mode ——– indicating whether the sequence of tracks repeats itself. * Default state is false. * * showFrame Mode ——— indicating whether the nearest video key frame is * displayed at the current position when not playing. Default state is * false. Has no effect on audio tracks. * * shuffle Mode ———- indicating whether the tracks are played in random * order. Default state is false. * * @param m * @param flag */ public void setMode(String m, boolean flag) { OleAutomation o = getSetting(); int ids[] = o.getIDsOfNames(new String[] { "setMode" }); if (ids != null) { o.invoke(ids[0], new Variant[] { new Variant(m), new Variant(flag) }); } } private OleAutomation getSetting() { OleAutomation o = null; int ids[] = player.getIDsOfNames(new String[] { "settings" }); if (ids != null) { o = player.getProperty(ids[0]).getAutomation(); } return o; } private OleAutomation getControls() { OleAutomation o = null; int ids[] = player.getIDsOfNames(new String[] { "controls" }); if (ids != null) { o = player.getProperty(ids[0]).getAutomation(); } return o; } public void setPostion(int s) { OleAutomation o = getControls(); int ids[] = o.getIDsOfNames(new String[] { "currentPosition" }); if (ids != null) { o.setProperty(ids[0], new Variant(s)); } } public void play() { OleAutomation o = getControls(); int ids[] = o.getIDsOfNames(new String[] { "play" }); if (ids != null) { o.invoke(ids[0]); } } public void stop() { OleAutomation o = getControls(); int ids[] = o.getIDsOfNames(new String[] { "stop" }); if (ids != null) { o.invoke(ids[0]); } } public void pause() { OleAutomation o = getControls(); int ids[] = o.getIDsOfNames(new String[] { "pause" }); if (ids != null) { o.invoke(ids[0]); } } public void fullScreen(boolean b) { if (true && getSize().x == 0) { return; } int ids[] = player.getIDsOfNames(new String[] { "fullScreen" }); if (ids != null) { player.setProperty(ids[0], new Variant(b)); } } public int getPlayState() { int state = 0; int ids[] = player.getIDsOfNames(new String[] { "playState" }); if (ids != null) { Variant sv = player.getProperty(ids[0]); if (sv != null) state = sv.getInt(); } return state; } public void closeMedia() { int ids[] = player.getIDsOfNames(new String[] { "close" }); if (ids != null) { player.invoke(ids[0]); } } public void addPlayList(File urls[]) { int ids[] = player.getIDsOfNames(new String[] { "currentPlaylist" }); if (ids != null) { OleAutomation o = player.getProperty(ids[0]).getAutomation(); int idsaddma[] = o.getIDsOfNames(new String[] { "appendItem" }); int idsma[] = player.getIDsOfNames(new String[] { "newMedia" }); if (idsaddma != null && idsma != null) { for (File url : urls) { Variant media = player.invoke(idsma[0], new Variant[] { new Variant(url.getAbsolutePath()) }); if (media != null) { o.invoke(idsaddma[0], new Variant[] { media }); } } } } } public void play(String url) { int idsma[] = player.getIDsOfNames(new String[] { "newMedia" }); if (idsma != null) { Variant media = player.invoke(idsma[0], new Variant[] { new Variant(url) }); int cmedia[] = player.getIDsOfNames(new String[] { "currentMedia" }); if (cmedia != null) { player.setProperty(cmedia[0], media); play(); } } } public void playList() { File file = new File(""); if (file.exists()) { File fs[] = file.listFiles(); addPlayList(fs); } setMode("loop", true); play(); } }
package org.flashSys.ui; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.flash.player.WMP; public class Player { protected Shell shell; private String file; /** * Launch the application * @param args */ public static void main(String[] args) { try { Player window = new Player(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window */ public void open() { final Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } /** * Create contents of the window */ protected void createContents() { shell = new Shell(); shell.setLayout(new FillLayout()); shell.setSize(500, 375); shell.setText("模板播放"); final WMP composite = new WMP(shell, SWT.NONE); // composite.play("D:/1.swf"); this.setFile("D:/tween.swf"); composite.play(file); // } public String getFile() { return file; } public void setFile(String file) { this.file = file; } }