读书人

java 用swt调用com组装之ie组建加强版

发布时间: 2012-09-16 17:33:17 作者: rapoo

java 用swt调用com组建之ie组建加强版
首先因为是调用ie的组建,所以有些机器默认的不是ie,就可能无法直接获取。所以有两点需要做的,1是设置ie为默认浏览器,2是先开一下ie。
先说一下,这个代码是通过进程调用,也可以理解为java进程和ie进程之间的通信。
另外,一些代码也是学习重用了网上的一些代码,但还是做了一些修改。
最后的效果是,打开一个自己预先写的网页,3秒钟之后会自动点击网页上的一个按钮,将html预先的值写入一个textarea中,然后java端获取这个值。
然后是代码部分
1,和之前一样,有一个超类SuperOcx.java

package outIE;import org.eclipse.swt.ole.win32.OleAutomation;import org.eclipse.swt.ole.win32.OleControlSite;import org.eclipse.swt.ole.win32.OleListener;import org.eclipse.swt.ole.win32.Variant;import org.eclipse.swt.widgets.Composite;public class SuperOcx extends Composite {protected OleAutomation player;protected OleControlSite controlSite;protected int Type;/** * @param parent * @param style */public SuperOcx(Composite parent, int style) {super(parent, style);}protected int getType() {return Type;}protected void setType(int type) {Type = type;}public int getID(String name) {try {int[] ids = player.getIDsOfNames(new String[] { name });if (ids.length >= 0)return ids[0];} catch (RuntimeException e) {e.printStackTrace();}return -1;}public int[] getIDs(String... name) {try {int[] ids = player.getIDsOfNames(name);if (ids.length >= 0)return ids;} catch (RuntimeException e) {e.printStackTrace();}return null;}public Variant execute(String methodName) {int mid = getID(methodName);if (mid < 0)return null;Variant rtnv = player.invoke(mid);return rtnv;}public Variant getProperty(String parameter){int mid = getID(parameter);if(mid<0)return null;return player.getProperty(mid);}public Variant execute(String methodName, Variant[] parameter) {int mid = getID(methodName);if (mid < 0)return null;Variant rtnv = player.invoke(mid, parameter);return rtnv;}public Variant executes(String[] methodName, Variant[] parameter) {int[] mid = getIDs(methodName);if (mid == null)return null;int[] x = new int[1];x[0] = mid[1];Variant rtnv = player.invoke(mid[0], parameter, x);return rtnv;}/** * 添加事件监听 *  * @param eventID * @param listener */public void addEventListener(int eventID, OleListener listener) {controlSite.addEventListener(eventID, listener);}/** * 移除事件监听 *  * @param eventID * @param listener */public void removeEventListener(int eventID, OleListener listener) {controlSite.removeEventListener(eventID, listener);}}


2,然后是外部调用ie的方式的封装,这里面的main方法可以试一下,是启动ie进程
AutomationClientSite.java
package outIE;import org.eclipse.swt.SWT;import org.eclipse.swt.SWTException;import org.eclipse.swt.internal.ole.win32.COM;import org.eclipse.swt.internal.ole.win32.GUID;import org.eclipse.swt.internal.ole.win32.IUnknown;import org.eclipse.swt.ole.win32.OLE;import org.eclipse.swt.ole.win32.OleAutomation;import org.eclipse.swt.ole.win32.OleClientSite;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.Shell;public class AutomationClientSite extends OleClientSite {public AutomationClientSite(Composite parent, int style, String progId) {super(parent, style);try {appClsid = getClassID(progId);if (appClsid == null)OLE.error(OLE.ERROR_INVALID_CLASSID);int[] address = new int[1];int result = COM.CoCreateInstance(getClassID(progId), 0, COM.CLSCTX_INPROC_SERVER | COM.CLSCTX_LOCAL_SERVER, COM.IIDIUnknown, address);if (result != COM.S_OK)OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);objIUnknown = new IUnknown(address[0]);} catch (SWTException e) {dispose();disposeCOMInterfaces();throw e;}}protected GUID getClassID(String progId) {GUID guid = new GUID();char[] buffer = null;if (progId != null) {int count = progId.length();buffer = new char[count + 1];progId.getChars(0, count, buffer, 0);}if (COM.CLSIDFromProgID(buffer, guid) != COM.S_OK) {int result = COM.CLSIDFromString(buffer, guid);if (result != COM.S_OK)OLE.error(result);}return guid;}public static void main(String[] args) {Shell composite = new Shell();OleFrame frame = new OleFrame(composite, SWT.NONE);AutomationClientSite client = new AutomationClientSite(frame, SWT.NONE, "InternetExplorer.Application");OleAutomation ole = new OleAutomation(client);int[] ids = ole.getIDsOfNames(new String[] { "visible" });ole.setProperty(ids[0], new Variant(true));}}


3,IEFrame.java 这个是对于IE控件的进一步封装
package outIE;import org.eclipse.swt.SWT;import org.eclipse.swt.SWTError;import org.eclipse.swt.SWTException;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.ole.win32.OleAutomation;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.Composite;/** * IE控制 *  */public class IEFrame extends SuperOcx {public AutomationClientSite automationClientSite;/** * session event */public static final int OnStatusChanged = 1;///** * Create the composite *  * @param parent * @param style */public IEFrame(Composite parent, int style) {super(parent, style);createContents();}protected void createContents() {setLayout(new FillLayout());try {OleFrame frame = new OleFrame(this, SWT.NO_TRIM);automationClientSite = new AutomationClientSite(frame, SWT.NONE, "InternetExplorer.Application");player = new OleAutomation(automationClientSite);int[] ids = player.getIDsOfNames(new String[] { "visible" });player.setProperty(ids[0], new Variant(true));} catch (SWTException e) {throw new RuntimeException(e.getMessage(), e);} catch (SWTError e) {throw new RuntimeException(e.getMessage(), e);}}/** * 跳转网址 * @param rgvarg * @param rgdispidNamedArgs * @return */public long Navigate(String rgvarg, int rgdispidNamedArgs) {Variant v[] = new Variant[2];v[0] = new Variant(rgvarg);v[1] = new Variant(rgdispidNamedArgs);return execute("Navigate", v).getLong();}/** * 用来获取dom的元素 * @param id * @return */public OleAutomation getElementById(String id) {Variant v[] = new Variant[1];v[0] = new Variant(id);return execute("getElementById", v).getAutomation();}@Overridepublic void addEventListener(int eventID, OleListener listener) {super.addEventListener(eventID, listener);}@Overridepublic void removeEventListener(int eventID, OleListener listener) {super.removeEventListener(eventID, listener);}}

4,调用,里面有注释,可以自己看
OutIEPlayer.java
/** *  */package outIE;import org.eclipse.swt.SWT;import org.eclipse.swt.ole.win32.OleAutomation;import org.eclipse.swt.ole.win32.Variant;import org.eclipse.swt.widgets.Shell;public class OutIEPlayer {/** * @param args * @throws InterruptedException  */public static void main(String[] args) throws InterruptedException {Shell shell = new Shell(SWT.CLOSE | SWT.MIN);IEFrame ieFrame = new IEFrame(shell, SWT.NONE);String url =System.getProperty("user.dir") + "\\test.html";String[] methodName = new String[] { "Navigate", "URL" };Variant[] rgvarg = new Variant[1];rgvarg[0] = new Variant(url);ieFrame.executes(methodName, rgvarg);Variant vdoc = ieFrame.getProperty("document");// 获取ie的Document元素OleAutomation doc = vdoc.getAutomation();// 转成oleVariant vplayer = doc.invoke(doc.getIDsOfNames(new String[] { "getElementById" })[0], new Variant[] { new Variant("play") });Thread.sleep(3000);// 登录模拟clickvplayer.getAutomation().invoke(vplayer.getAutomation().getIDsOfNames(new String[] { "click" })[0]);//获取listinfo元素Variant VtextArea= doc.invoke(doc.getIDsOfNames(new String[] { "getElementById" })[0], new Variant[] { new Variant("listinfo") });OleAutomation textArea=VtextArea.getAutomation();//获取value属性的idint listinfoID = textArea.getIDsOfNames(new String[] { "value" })[0];//获取内容Variant list=textArea.getProperty(listinfoID);System.out.println(": "+list.getString());}}

附件是用eclipse建立的工程,有基础班和加强版两个例子,仅供参考。

读书人网 >编程

热点推荐