使用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.kosen.com.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); } }); } }
|
|
|