使用Java開發來電顯示管理軟件范例 |
chen在2010/5/9發表,被瀏覽5826次
|
本文范例源代碼:點擊下載workspace.rar 1、開發環境:Eclipse(Java主流開發工具) (1)需要用到SWT、Visual Editor插件; (2)Eclipse軟件及其相關插件可到 http://www.eclipse.org/downloads/ 下載。 2、需要用到JDComport.ocx (1)JDComport.ocx是高深商公司開發的來電顯示ActiveX控件,下載地址:JDComPort.rar (2)JDComport.ocx使用詳情請參考:http://www.kekzuq5.cn/news/showatc.asp?id=123 (3)在開發之前請先注冊,可直接運行REGJD.bat進行注冊。 3、Java調用ActiveX控件的關鍵是使用OleFrame、OleControlSite、OleAutomation調用OCX控件,invoke調用控件中的函數,addEventListener調用控件中的事件。 4、JDComport.java封裝了JDComport.ocx最重要的函數及事件,其它Java程序可直接調用。
5、VETest.java使用JDComport.ocx實現如下功能: (1)設置來電顯示盒的計算機連接端口,可自動查找,只需在第一次使用(或更換了USB插口)時調用; (2)設置來電顯示參數; (3)按電話鍵時,計算機自動顯示按鍵內容; (4)當有電話打入時,自動顯示來電號碼、時間等內容。 6、下面是JDComport.java和VETest.java的源代碼: JDComport.java代碼 ----------------------------------------------------------------------------------------------------- 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 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 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"); } /* 說明:回撥電話 參數:no:電話號碼 ch:通道號, 通道1-4的ch值為0-3, 對于單口設備ch的值永遠是0 對于雙口設備ch的值只有 0 、1 對于四口設備ch的值有 0 、1、2、3 */ public void DialTele(String no, int ch) { Variant[] vs = new Variant[2]; vs[0] = new Variant(no); vs[1] = new Variant(ch); execute("DialupChanl", vs); } //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; } } ===================================================================================================== VETest.java代碼 ----------------------------------------------------------------------------------------------------- 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); } }); } }
|
|
|