您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java內(nèi)嵌activeX控件怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java內(nèi)嵌activeX控件怎么使用”吧!
這里用的是SWT/JFace開發(fā)application中SWT自帶的org.eclipse.swt.ole.win32 包可以支持內(nèi)嵌OLE和ActiveX。
具體用法如下:
//創(chuàng)建一個OleFrame做為OLE(或ActiveX)的框架
OleFrame oleFrame = new OleFrame(this, SWT.NONE);
//創(chuàng)建ActiveX的容器,其中的classID是ActiveX的classid,在注冊表中可以找到
OleControlSite oleControl = new OleControlSite(oleFrame, SWT.NONE, “classID”);
//OleAutomation類用來執(zhí)行ActiveX中的方法
OleAutomation oleAutomation = new OleAutomation(oleControl);
//將ActiveX顯示在application中
oleControl.doVerb(OLE.OLEIVERB_SHOW);
調(diào)用AcitveX中方法的具體過程:
1、不帶參數(shù)的方法調(diào)用
//獲取Method Name的ID,Method Name為ActiveX中具體的方法名
int[] regspid = oleAutomation.getIDsOfNames(new String[] { "MethodName" });
int dispIdMember = regspid[0];
//方法調(diào)用
oleAutomation.invoke(dispIdMember);
2、帶參數(shù)的方法調(diào)用
//獲取Method Name的ID,Method Name為ActiveX中具體的方法名
int[] regspid = oleAutomation.getIDsOfNames(new String[] { "MethodName" });
int dispIdMember = regspid[0];
//設置方法的具體參數(shù)。Variant數(shù)組的長度為MethodName方法參數(shù)的個數(shù)
//假設有四個參數(shù)
Variant[] rgvarg = new Variant[4];
rgvarg[0] = new Variant(fileID);
rgvarg[1] = new Variant(itdsURL);
rgvarg[2] = new Variant(idType);
rgvarg[3] = new Variant(reportURL);
//方法調(diào)用
oleAutomation.invoke(dispIdMember, rgvarg);
調(diào)用OLE Exemple:Java程序內(nèi)嵌Word應用程序
package test.swt;
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
public class ActiveXTest
{
private Shell sShell = null;
private Button button = null;
private OleClientSite clientSite;
public static void main(String[] args)
{
Display display =Display.getDefault();
ActiveXTest thisClass = new ActiveXTest();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell()
{
GridData gridData = new GridData();
gridData.horizontalSpan = 2;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
sShell = new Shell();
sShell.setText("Shell");
sShell.setLayout(gridLayout);
sShell.setSize(new Point(800, 600));
OleFrame frame = new OleFrame(sShell, SWT.NONE);
button = new Button(sShell, SWT.NONE);
button.setLayoutData(gridData);
button.setText("Save");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
{
clientSite.save(new File("d:/test.docx"),true);
}
});
frame.setSize(800,600);
clientSite = new OleClientSite(frame, SWT.NONE,"Word.Document.8");
clientSite.setSize(400,400);
clientSite.doVerb(OLE.OLEIVERB_SHOW);
}
}
SWT調(diào)用ActiveX簡單總結(jié)
public class SWT_ActivexUtil {
private OleFrame _frame;
private OleControlSite _site;
private OleAutomation _auto;
SWT_ActivexUtil(String activexId, OleControlSite site){
if(site == null){
Shell shell = new 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);
}
}
到此,相信大家對“java內(nèi)嵌activeX控件怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。