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;
}
}

OLE--SWT高级控件的更多相关文章

  1. Android高级控件--AdapterView与Adapter

    在J2EE中提供过一种非常好的框架--MVC框架,实现原理:数据模型M(Model)存放数据,利用控制器C(Controller)将数据显示在视图V(View)上.在Android中有这样一种高级控件 ...

  2. Android 高级控件(七)——RecyclerView的方方面面

    Android 高级控件(七)--RecyclerView的方方面面 RecyclerView出来很长时间了,相信大家都已经比较了解了,这里我把知识梳理一下,其实你把他看成一个升级版的ListView ...

  3. Android高级控件(六)——自定义ListView高仿一个QQ可拖拽列表的实现

    Android高级控件(六)--自定义ListView高仿一个QQ可拖拽列表的实现 我们做一些好友列表或者商品列表的时候,居多的需求可能就是需要列表拖拽了,而我们选择了ListView,也是因为使用L ...

  4. Android高级控件(五)——如何打造一个企业级应用对话列表,以QQ,微信为例

    Android高级控件(五)--如何打造一个企业级应用对话列表,以QQ,微信为例 看标题这么高大上,实际上,还是运用我么拿到listview去扩展,我们讲什么呢,就是研究一下QQ,微信的这种对话列表, ...

  5. Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷

    Android高级控件(四)--VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷 是不是感觉QQ空间什么的每次新版本更新那炫炫的引导页就特别的激动,哈哈,其实他实现起来真的很简单很 ...

  6. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  7. Android高级控件(二)——SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现

    Android高级控件(二)--SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现 写这个的原因呢,也是因为项目中用到了gif动画,虽然网上有很多的架包可以实现,不过我们还 ...

  8. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

  9. PyQt5——高级控件

    PyQt5高级控件使用方法详见:https://blog.csdn.net/jia666666/article/list/4?t=1& PyQt5高级控件汇总: 1.QTableView 2. ...

  10. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

随机推荐

  1. package.json 详解

    使用package.json  属性说明 name - 包名. version - 包的版本号. description - 包的描述. homepage - 包的官网 url . author - ...

  2. (1)Linux文件系统的目录组成

    记忆秘诀:BBDEH OPRLM TLSUV 宝贝的恩惠 欧派入联盟 偷了suv,19   目录 英文释义 简写 详解 1 /   根目录 整个文件系统的唯一根目录 2 /bin Binary 普通命 ...

  3. JavaScript 常见的六种继承方式

    JavaScript 常见的六种继承方式 前言 面向对象编程很重要的一个方面,就是对象的继承.A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法.这对于代码的复用是非常有用的. 大部分 ...

  4. POJ 3581:Sequence(后缀数组)

    题目链接 题意 给出n个数字的序列,现在让你分成三段,使得每一段翻转之后拼接起来的序列字典序最小.保证第一个数是序列中最大的数. 例如样例是{10, 1, 2, 3, 4},分成{1, 10}, {2 ...

  5. UESTC 1324:卿学姐与公主(分块)

    http://acm.uestc.edu.cn/#/problem/show/1324 题意:…… 思路:卿学姐的学习分块例题. 分块是在线处理区间问题的类暴力算法,复杂度O(n*sqrt(n)),把 ...

  6. 有意思的 CDN

    Clean Clean false 7.8 磅 0 2 false false false EN-US ZH-CN AR-SA /* Style Definitions */ table.MsoNor ...

  7. 阿里系手淘weex学习第一天

    官网原文:https://weex.apache.org/zh/tools/extension.html#功能 功能 创建Weex项目. 支持在VSCode对Weex的语法支持. 检查iOS和Andr ...

  8. WeUI Picker组件 源代码分析

    前言 由于最近做的一个移动端项目需要使用到类似 WeUI Picker组件 的选择效果,  所以在这里来分析下 WeUI Picker 的实现逻辑.(weui.js项目地址) 之前也做过类似的组件, ...

  9. centos 7 安装docker,conflicts 异常

    [root@localhost html]# yum install docker-io 已加载插件:fastestmirror, langpacks Loading mirror speeds fr ...

  10. 异常——cmd下javac错误:编码GBK不可映射字符

    在看菜鸟教程时候用记事本创建文件,之后用notepad++编辑后,运行出现错误. 首先从信息上知道这是编码的问题了.开始试了下再notepad++上打开文件选择标签栏的“Encoding”中的“enc ...