望江門外——謝窮的Blog

          分享別人的經典 不如自己締造經典

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            6 Posts :: 1 Stories :: 1 Comments :: 0 Trackbacks

          本范例以使用來電顯示ActiveX控件JDComport.ocx為例,說明怎樣在Java中集成ActiveX控件。

           

          1、開發環境:EclipseJava主流開發工具)

             1)需要用到SWT、Visual 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進行注冊。

           

          3、Java調用ActiveX控件的關鍵

          Java調用ActiveX控件的關鍵是使用OleFrameOleControlSite、OleAutomation調用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);

                          }

                      });

           

          4、JDComport.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;

              }

           }

           

           

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

           

           

          5、VETest.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);

             }

            });

           }

           

          }
          posted on 2011-06-21 16:41 望江門外 閱讀(9640) 評論(1)  編輯  收藏 所屬分類: SWT

          Feedback

          # re: Eclipse開發:在Java中調用ActiveX控件(OCX控件)示例[未登錄] 2016-06-01 17:54 aaa
          fadfa  回復  更多評論
            

          主站蜘蛛池模板: 九寨沟县| 万山特区| 德清县| 肥乡县| 孟连| 东乌珠穆沁旗| 黑龙江省| 久治县| 徐闻县| 苗栗县| 新化县| 城固县| 慈溪市| 屯昌县| 富宁县| 海晏县| 特克斯县| 龙口市| 常熟市| 盐山县| 昌吉市| 苍溪县| 江永县| 山阴县| 博乐市| 修水县| 高雄县| 禹州市| 藁城市| 涟源市| 托克逊县| 辰溪县| 岑巩县| 太仆寺旗| 修文县| 禄劝| 南涧| 星座| 遂昌县| 浏阳市| 建始县|