SWT Java Inline ActiveX control

Source: Internet
Author: User
Tags ole

This is used in SWT/JFACE development application The ORG.ECLIPSE.SWT.OLE.WIN32 package that SWT comes with can support inline OLE and ActiveX.
The specific usage is as follows:
Create a oleframe as an OLE (or ActiveX) framework
Oleframe oleframe = new Oleframe (this, SWT. NONE);
Create an ActiveX container where the ClassID is an ActiveX ClassID that can be found in the registry
Olecontrolsite OLEControl = new Olecontrolsite (Oleframe, SWT. NONE, "ClassID");
The Oleautomation class is used to execute methods in ActiveX
Oleautomation oleautomation = new oleautomation (OLEControl);
To display ActiveX in application
Olecontrol.doverb (OLE. Oleiverb_show);

To invoke the specific procedure of a method in Acitvex:
1. Method calls without parameters
Get method name Id,method name for ActiveX
int[] Regspid = oleautomation.getidsofnames (new string[] {"MethodName"});
int dispidmember = regspid[0];
Method invocation
Oleautomation.invoke (Dispidmember);

2. Method calls with parameters
Get method name Id,method name for ActiveX
int[] Regspid = oleautomation.getidsofnames (new string[] {"MethodName"});
int dispidmember = regspid[0];
Sets the specific parameters of the method. The length of the variant array is the number of MethodName method parameters
Suppose there are four parameters
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);
Method invocation
Oleautomation.invoke (Dispidmember, Rgvarg);

Calling the OLE Exemple:java program to embed a Word application

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 Call the ActiveX Brief summary

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

SWT Java Inline ActiveX control

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.