swt java 內(nèi)嵌ActiveX控件

這里用的是SWT/JFace開發(fā)application中SWT自帶的org.eclipse.swt.ole.win32 包可以支持內(nèi)嵌OLE和ActiveX漫蛔。 具體用法如下:

//創(chuàng)建一個(gè)OleFrame做為OLE(或ActiveX)的框架

OleFrame oleFrame = new OleFrame(this, SWT.NONE);

//創(chuàng)建ActiveX的容器毁葱,其中的classID是ActiveX的classid,在注冊(cè)表中可以找到

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[] { "Method Name" });

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[] { "Method Name" });

int dispIdMember = regspid[0];

//設(shè)置方法的具體參數(shù)。Variant數(shù)組的長(zhǎng)度為Method Name方法參數(shù)的個(gè)數(shù)

//假設(shè)有四個(gè)參數(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應(yīng)用程序

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ǎn)單總結(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

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

}

}

關(guān)注獲取更多技術(shù)文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末豆挽,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子券盅,更是在濱河造成了極大的恐慌帮哈,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锰镀,死亡現(xiàn)場(chǎng)離奇詭異娘侍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)泳炉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門憾筏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人花鹅,你說我怎么就攤上這事氧腰。” “怎么了刨肃?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵古拴,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我真友,道長(zhǎng)黄痪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任锻狗,我火速辦了婚禮,結(jié)果婚禮上焕参,老公的妹妹穿的比我還像新娘轻纪。我一直安慰自己,他們只是感情好叠纷,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布刻帚。 她就那樣靜靜地躺著,像睡著了一般涩嚣。 火紅的嫁衣襯著肌膚如雪崇众。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天航厚,我揣著相機(jī)與錄音顷歌,去河邊找鬼。 笑死幔睬,一個(gè)胖子當(dāng)著我的面吹牛眯漩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼赦抖,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼舱卡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起队萤,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤轮锥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后要尔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舍杜,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年盈电,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蝴簇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡匆帚,死狀恐怖熬词,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吸重,我是刑警寧澤互拾,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站嚎幸,受9級(jí)特大地震影響颜矿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嫉晶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一骑疆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧替废,春花似錦箍铭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至状答,卻和暖如春冷守,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惊科。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工拍摇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人馆截。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓授翻,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子堪唐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理巡语,服務(wù)發(fā)現(xiàn),斷路器淮菠,智...
    卡卡羅2017閱讀 134,699評(píng)論 18 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法男公,類相關(guān)的語法,內(nèi)部類的語法合陵,繼承相關(guān)的語法枢赔,異常的語法,線程的語...
    子非魚_t_閱讀 31,662評(píng)論 18 399
  • 一拥知、 1踏拜、請(qǐng)用Java寫一個(gè)冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,386評(píng)論 0 6
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,814評(píng)論 0 11
  • 說來慚愧速梗,畢業(yè)三年,后期轉(zhuǎn)行襟齿,以至如今還是作為與應(yīng)屆生同等待遇的職場(chǎng)的螺絲釘?shù)拇嬖凇?轉(zhuǎn)行前做過銷售姻锁,任職于一家房...
    阡陌十六閱讀 1,361評(píng)論 0 0