Eclipse开发:在Java中调用ActiveX控件(OCX控件)示例

Eclipse开发:在Java中调用ActiveX控件(OCX控件)示例

本范例以使用来电显示ActiveX控件JDComport.ocx为例,说明怎样在Java中集成ActiveX控件。

 

1、开发环境:EclipseJava主流开发工具)

   1)需要用到SWTVisual Editor插件;

   2Eclipse软件及其相关插件可到 http://www.eclipse.org/downloads/ 下载。

   

2、需要用到JDComport.ocx

   1JDComport.ocx是高深商开发的来电显示ActiveX控件,下载地址:http://www.kosen.com.cn/news/admin/attachments/month_0907/JDComPort.rar

   2JDComport.ocx使用详情请参考:http://www.kosen.com.cn/news/showatc.asp?id=123

   3)在开发之前请先注册,可直接运行REGJD.bat进行注册。

 

3Java调用ActiveX控件的关键

Java调用ActiveX控件的关键是使用OleFrameOleControlSiteOleAutomation调用OCX控件,invoke调用控件中的函数,addEventListener调用控件中的事件。

 

    OLE调用的关键代码:

 

            Shell shell = new Shell();

            _frame = new OleFrame(shell, SWT.NONE);

            _site = new OleControlSite(_frame, SWT.NONE, "JDCompPort.JDComponent");

            _auto = new OleAutomation(_site);

    调用控件中的函数:

 

            int[] ids = _auto.getIDsOfNames(new String[]{methodName});

            Variant rtnv = _auto.invoke(ids[0]);

 

    调用控件中的事件(Event):

 

            _site.addEventListener(int eventID, OleListener listener);

 

            jdc.addEventListener(jdc.idOnOpen, new OleListener(){

                @Override

                public void handleEvent(OleEvent event) {

                     String key = jdc.extractEventArgument("VT_BSTR", event.arguments[0].toString());

                     String devid = jdc.extractEventArgument("VT_BSTR", event.arguments[1].toString());

                     textArea.append("\r\n"+key+"  "+devid);

                }

            });

 

4JDComport.java封装了JDComport.ocx最重要的函数及事件

代码如下:

import org.eclipse.swt.SWT;

import org.eclipse.swt.ole.win32.OleAutomation;

import org.eclipse.swt.ole.win32.OleControlSite;

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.Shell;

 

 

public class JDComport {

   

    private OleFrame _frame;

    private OleControlSite _site;

    private OleAutomation _auto;

   

    public int idOnOpen=1;

    public int idOnClose=2;

    public int idOnRead=3;

    public int idOnKeyPress=4;

    public int idOnWaveIn=5;

   

    JDComport(){

            Shell shell = new Shell();

            _frame = new OleFrame(shell, SWT.NONE);

            _site = new OleControlSite(_frame, SWT.NONE, "JDCompPort.JDComponent");

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

    }

 

    public void openComport()

    {

        execute("Open");

    }

   

    public void setupComport()

    {

        execute("SetupPorts");

    }

   

    public void setJDState()

    {

        execute("SetSate");

    }

   

    //VT_BSTR{38259081} -> 39259081

    public String extractEventArgument(String pres, String argu)

    {

        if(argu.startsWith(pres))

           return argu.substring(pres.length()+1, argu.length()-1);

       else

           return argu;

    }

 }

 

 

--------------------------------------------------------------------------------

 

 

5VETest.java使用JDComport.ocx实现如下功能:

   1)设置来电显示盒的计算机连接端口,可自动查找,只需在第一次使用(或更换了USB插口)时调用;

   2)设置来电显示参数;

   3)按电话键时,计算机自动显示按键内容;

   4)当有电话打入时,自动显示来电号码、时间等内容;

 

   5VETest.java代码如下,特别请注意addEventListener()的使用。

 

import java.text.SimpleDateFormat;

import java.util.Calendar;

 

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.OleEvent;

import org.eclipse.swt.ole.win32.OleListener;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

 

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import org.eclipse.swt.graphics.Font;

 

/**

 * @author chen lx

 *

 */

public class VETest {

 

 private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="107,15"

 private Button button2 = null;

 private Button button1 = null;

 private Text textArea = null;

 private JDComport jdc=null;  //  @jve:decl-index=0:

 

 /**

  * @param args

  */

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  /* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)

   * for the correct SWT library path in order to run with the SWT dlls.

   * The dlls are located in the SWT plugin jar. 

   * For example, on Windows the Eclipse SWT 3.1 plugin jar is:

   *       installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar

   */

  Display display = Display.getDefault();

  VETest thisClass = new VETest();

  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;

  gridData.heightHint = -1;

  gridData.verticalSpan = 6;

  GridData gridData1 = new GridData();

  GridLayout gridLayout = new GridLayout();

  gridLayout.numColumns = 2;

  sShell = new Shell();

  sShell.setText("Shell");

  sShell.setMaximized(false);

  sShell.setLayout(gridLayout);

  sShell.setSize(new Point(355, 118));

  button1 = new Button(sShell, SWT.NONE);

  button1.setText("打开端口");

  button2 = new Button(sShell, SWT.NONE);

  button2.setText("端口设置");

  button2.setLayoutData(gridData1);

  textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);

  textArea.setText("---------------------------------------------");

  textArea.setFont(new Font(Display.getDefault(), "宋体", 10, SWT.NORMAL));

  textArea.setLayoutData(gridData);

  button1.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {

   public void mouseDown(org.eclipse.swt.events.MouseEvent e) {

    jdc.openComport();

   }

  });

  button2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {

   public void mouseDown(org.eclipse.swt.events.MouseEvent e) {

    jdc.setupComport();

   }

  });

 

  jdc = new JDComport();

  textArea.append("\r\n");

  jdc.addEventListener(jdc.idOnOpen, new OleListener(){

   @Override

   public void handleEvent(OleEvent event) {

    textArea.append("\r\nJD端口已打开 ");

   }

  });

  jdc.addEventListener(jdc.idOnClose, new OleListener(){

   @Override

   public void handleEvent(OleEvent event) {

    textArea.append("\r\nJD端口已关闭 ");

   }

  });

  // OnKeyPress(const key: WideString; const devid: WideString);

  jdc.addEventListener(jdc.idOnKeyPress, new OleListener(){

   @Override

   public void handleEvent(OleEvent event) {

    // TODO Auto-generated method stub

    String key = jdc.extractEventArgument("VT_BSTR", event.arguments[0].toString());

    String devid = jdc.extractEventArgument("VT_BSTR", event.arguments[1].toString());

    textArea.append("\r\n"+key+"  "+devid);

   }

  });

  // OnRead(const s: WideString; t: Double; const devid: WideString; const WaveFile: WideString)

  jdc.addEventListener(jdc.idOnRead, new OleListener(){

   @Override

   public void handleEvent(OleEvent event) {

    // TODO Auto-generated method stub

    String s = jdc.extractEventArgument("VT_BSTR", event.arguments[0].toString());

    String t = jdc.extractEventArgument("VT_R8", event.arguments[1].toString());

    String devid = jdc.extractEventArgument("VT_BSTR", event.arguments[2].toString());

    String wf = jdc.extractEventArgument("VT_BSTR", event.arguments[3].toString());

    //来电时间从Double转换为日期型

    Double d=Double.valueOf(t);

    try   {  

         Calendar base = Calendar.getInstance();  

         //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  

         SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

         //base.setTime(format.parse("1899-12-30"));

         base.set(1899, 11, 30, 0, 0, 0);

         base.add(Calendar.DATE, d.intValue());  

         base.add(Calendar.MILLISECOND,(int)((d % 1) * 24 * 60 * 60 * 1000));  

         t=outFormat.format(base.getTime());  

    }  

    catch   (ParseException   e)   {  

         e.printStackTrace();     

    }

    textArea.append("\r\n"+s+"  "+t.toString()+"  "+devid+"  "+wf);

   }

  });

 }

 

}

你可能感兴趣的:(Eclipse开发:在Java中调用ActiveX控件(OCX控件)示例)