您的位置:首页 > 其它

OLE--SWT高级控件

2014-05-18 18:31 232 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: