最近因项目需要测试一些ActiveX控件的功能,使用原生工具笨重且比较慢,错误跟踪也不太方便,决定使用Eclipse的SWT尝试一番,最后发现果然比较便捷。这里根据网上某大神的用例来构建一个工具类。
package com.xxx.yyy;
import org.eclipse.swt.SWT;
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.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Shell;
public class ActiveXUtil {
private Shell _shell;
private OleFrame _frame;
private OleControlSite _site;
private OleAutomation _auto;
public Shell getShell() {
return _shell;
}
public OleFrame getFrame() {
return _frame;
}
public OleControlSite getSite() {
return _site;
}
public OleAutomation getAuto() {
return _auto;
}
public int doVerb(int verb) {
return _site.doVerb(verb);
}
ActiveXUtil(String activexId, OleControlSite site, Shell shell) {
if (site == null) {
_shell = (shell == null ? new Shell() : shell);
_frame = new OleFrame(_shell, SWT.NONE);
_site = new OleControlSite(_frame, SWT.NONE, activexId);
_auto = new OleAutomation(_site);
} else {
_site = site;
_auto = new OleAutomation(site);
}
}
public int getID(String name) {
try {
int[] ids = _auto.getIDsOfNames(new String[] { name });
if (ids.length >= 0)
return ids[0];
} catch (RuntimeException e) {
e.printStackTrace();
}
return -1;
}
public Variant[] createVariants(String[] paras) {
Variant[] vr = new Variant[paras.length];
for (int i = 0; i < paras.length; i++) {
vr[i] = new Variant(paras[i]);
}
return vr;
}
public Variant getProperty(String prop) {
int propId = getID(prop);
Variant v = null;
try {
v = _auto.getProperty(propId);
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
public void setProperty(String name, String... params) {
int propId = getID(name);
if (propId < 0)
return;
if (params.length == 1)
_auto.setProperty(propId, new Variant(params[0]));
else {
Variant[] vs = new Variant[params.length];
int i = 0;
for (String param : params) {
vs[i] = new Variant(param);
i++;
}
_auto.setProperty(propId, vs);
}
}
public void setProperty(String name, Variant... params) {
int propId = getID(name);
if (propId < 0)
return;
_auto.setProperty(propId, params);
}
public Variant execute(String methodName, Variant... params) {
int mid = getID(methodName);
if (mid < 0)
return null;
Variant rtnv;
if (params == null)
rtnv = _auto.invoke(mid);
else
rtnv = _auto.invoke(mid, params);
return rtnv;
}
public Variant execute(String methodName) {
int mid = getID(methodName);
if (mid < 0)
return null;
Variant rtnv = _auto.invoke(mid);
return rtnv;
}
public void addEventListener(int eventID, OleListener listener) {
_site.addEventListener(eventID, listener);
}
public void removeEventListener(int eventID, OleListener listener) {
_site.removeEventListener(eventID, listener);
}
}
使用工具类的方法如下:
package com.xx.yy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SwtMainWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 设置应用标题
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(800, 660);
shell.setLayout(new RowLayout());
shell.setText("某某测试程序(SWT)");
// 初始化ActiveX控件
ActiveXUtil util = new ActiveXUtil("{04655FF8-9979-4FFA-8F77-C3A539EA7370}", null, shell);//这里可以是ClassID,也可以是AppID,用OLE-COM Object Viewer注册表中都能找到
util.getFrame().setSize(shell.getClientArea().width, shell.getClientArea().height);
util.doVerb(OLE.OLEIVERB_SHOW);
//监听事件
final int nEventXXX = 1;// 某事件ID,使用工具查看事件的DispatchID
util.addEventListener(nEventXXX, new OleListener() {
@Override
public void handleEvent(OleEvent event) {
// TODO Auto-generated method stub
System.out.println(event.arguments[0].getString());
}
});
// 调用方法
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("CallMethod");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Variant result = util.execute("SomeMethod", new Variant(
"Parameter Value"));
if (result == null) {
System.out.println("ActiveX method failure!");
}
}
});
// 显示窗口
shell.setSize(800, 700);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}