`
ppg
  • 浏览: 15553 次
  • 来自: 上海
社区版块
存档分类
最新评论

java 用swt调用com组建之ie组建基础版

阅读更多
首先必须要感谢我的同事lig的研究,在我换工作之际把些许工作成果分享。
内容版本会陆续更新。
java可以通过swt提供的方法调用windows的com组建,譬如ie,甚至可以控制页面中每一个dom的内容、事件等。
附件是swt的包。
首先是最基本的内嵌ie浏览器,打开网页。
建立SuperOcx.java

import org.eclipse.swt.ole.win32.OleAutomation;
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);
	}
}

然后建立ie的ocx组建
IEOCX.java
import org.eclipse.swt.SWT;

public class IEOCX extends SuperOcx {
	public IEOCX(Composite parent, int style) {
		super(parent, style);
		setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
		createContents();
	}

	protected void createContents() {
		setLayout(new FillLayout());
		try {
			OleFrame frame = new OleFrame(this, SWT.NO_TRIM);
			controlSite = new OleControlSite(frame, SWT.NO_TRIM, "Shell.Explorer");
			controlSite.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
			controlSite.setRedraw(true);
			controlSite.setLayoutDeferred(true);
			controlSite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
			controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
			controlSite.pack();
			player = new OleAutomation(controlSite);
			setFocus();
		} catch (SWTException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (SWTError e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

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

	public OleAutomation getElementById(String id) {
		Variant v[] = new Variant[1];
		v[0] = new Variant(id);
		return execute("getElementById", v).getAutomation();
	}

	@Override
	public void addEventListener(int eventID, OleListener listener) {
		super.addEventListener(eventID, listener);
	}

	@Override
	public void removeEventListener(int eventID, OleListener listener) {
		super.removeEventListener(eventID, listener);
	}
}


最后是主程序,用swt绘制窗口并加载ie,打开网页
Player.java
import org.eclipse.swt.SWT;

public class Player {
	protected Shell shell;
	private IEOCX ie;

	/**
	 * 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();
		}
		display.dispose();
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(850, 600);
		shell.setText("SWT Application");
		ie = new IEOCX(shell, SWT.NONE);
		ie.setSize(850, 600);
		String[] methodName = new String[] { "Navigate", "URL" };
		String url="www.baidu.com";
		Variant[] v = new Variant[1];
		v[0] = new Variant(url);
		ie.executes(methodName, v);

	}
}


这个是最简单的调用过程,之后会更新复杂的部分。
  • swt.jar (1.8 MB)
  • 下载次数: 25
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics