望江門外——謝窮的Blog

          分享別人的經(jīng)典 不如自己締造經(jīng)典

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            6 Posts :: 1 Stories :: 1 Comments :: 0 Trackbacks

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

           

          1、開發(fā)環(huán)境:EclipseJava主流開發(fā)工具)

             1)需要用到SWTVisual Editor插件;

             2Eclipse軟件及其相關(guān)插件可到 http://www.eclipse.org/downloads/ 下載。

            

          2、需要用到JDComport.ocx

             1JDComport.ocx是高深商開發(fā)的來電顯示ActiveX控件,下載地址:http://www.kosen.com.cn/news/admin/attachments/month_0907/JDComPort.rar

             2JDComport.ocx使用詳情請(qǐng)參考:http://www.kosen.com.cn/news/showatc.asp?id=123

             3)在開發(fā)之前請(qǐng)先注冊(cè),可直接運(yùn)行REGJD.bat進(jìn)行注冊(cè)。

           

          3Java調(diào)用ActiveX控件的關(guān)鍵

          Java調(diào)用ActiveX控件的關(guān)鍵是使用OleFrameOleControlSiteOleAutomation調(diào)用OCX控件,invoke調(diào)用控件中的函數(shù),addEventListener調(diào)用控件中的事件。

           

              OLE調(diào)用的關(guān)鍵代碼:

           

                      Shell shell = new Shell();

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

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

                      _auto = new OleAutomation(_site);

              調(diào)用控件中的函數(shù):

           

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

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

           

              調(diào)用控件中的事件(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最重要的函數(shù)及事件

          代碼如下:

          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實(shí)現(xiàn)如下功能:

             1)設(shè)置來電顯示盒的計(jì)算機(jī)連接端口,可自動(dòng)查找,只需在第一次使用(或更換了USB插口)時(shí)調(diào)用;

             2)設(shè)置來電顯示參數(shù);

             3)按電話鍵時(shí),計(jì)算機(jī)自動(dòng)顯示按鍵內(nèi)容;

             4)當(dāng)有電話打入時(shí),自動(dòng)顯示來電號(hào)碼、時(shí)間等內(nèi)容;

           

             5VETest.java代碼如下,特別請(qǐng)注意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("端口設(shè)置");

            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端口已關(guān)閉 ");

             }

            });

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

              //來電時(shí)間從Double轉(zhuǎn)換為日期型

              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) 評(píng)論(1)  編輯  收藏 所屬分類: SWT

          Feedback

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


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 漾濞| 油尖旺区| 读书| 瑞昌市| 梅州市| 临海市| 镇雄县| 新巴尔虎左旗| 吕梁市| 灵宝市| 左贡县| 南投县| 沅陵县| 开封市| 黄冈市| 池州市| 涡阳县| 抚州市| 井陉县| 林口县| 哈尔滨市| 营口市| 湟中县| 蓬安县| 石首市| 巴东县| 永安市| 清原| 宾阳县| 万荣县| 敦煌市| 崇义县| 新营市| 瓦房店市| 皮山县| 永和县| 鄂伦春自治旗| 沅陵县| 姚安县| 班玛县| 镇原县|