Swt java embedded ActiveX control, swtactivex
The org. eclipse. SWT. ole. win32 package in SWT/JFace development application can be used to support embedded OLE and ActiveX.
The usage is as follows:
// Create an OleFrame as an OLE (or ActiveX) Framework
OleFrame oleFrame = new OleFrame (this, SWT. NONE );
// Create an ActiveX container. The classID is the classid of ActiveX, which can be found in the registry.
OleControlSite oleControl = new OleControlSite (oleFrame, SWT. NONE, "classID ");
// OleAutomation class is used to execute methods in ActiveX
OleAutomation oleAutomation = new OleAutomation (oleControl );
// Display ActiveX in application
OleControl. doVerb (OLE. OLEIVERB_SHOW );
The specific process of calling the method in AcitveX:
1. Call methods without Parameters
// Obtain the ID of the Method Name. The Method Name is the specific Method Name in ActiveX.
Int [] regspid = oleAutomation. getIDsOfNames (new String [] {"MethodName "});
Int dispIdMember = regspid [0];
// Method call
OleAutomation. invoke (dispIdMember );
2. Call methods with Parameters
// Obtain the ID of the Method Name. The Method Name is the specific Method Name in ActiveX.
Int [] regspid = oleAutomation. getIDsOfNames (new String [] {"MethodName "});
Int dispIdMember = regspid [0];
// Set the parameters of the method. The length of the Variant array is the number of MethodName method parameters.
// Assume 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 call
OleAutomation. invoke (dispIdMember, rgvarg );
Call OLE Exemple: Java program Embedded 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 );
}
}
SWTCall ActiveXSummary
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, );
}
}
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 );
}
}