開發(fā)來電顯示及錄音程序的C#范例 |
chen在2010/3/7發(fā)表,被瀏覽9126次
此文章共有 2 頁
1
2
|
功能: (1)來電顯示,并把來電記錄保存到數(shù)據(jù)庫; (2)可設(shè)置狀態(tài),讓來電顯示管理器自動錄音,并把聲音文件名保存到數(shù)據(jù)庫。 源程序JDTest.rar:(點擊下載) 控件包JDComPort.rar(點擊下載 ) 詳情請看:來電管理設(shè)備的ActiveX控件(OCX)開發(fā)文檔 相關(guān)文檔:C#來電顯示管理器開發(fā)示例程序 源代碼如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.InteropServices; using System.Data.OleDb; namespace JDTest { public partial class Form1 : Form { const byte DEVCOUNT = 4; //1個來電盒最多可連接4條電話線 public string dbFileName, wavPath; public TPressKey[] aPK = new TPressKey[DEVCOUNT]; //記錄每條電話線的狀態(tài) public void InitAPK() { for (byte i = 0; i < DEVCOUNT; i++) { if (aPK[i] == null) aPK[i] = new TPressKey(); aPK[i].initPressKey(); } } public TPressKey FindPKey(string devid) { for (byte i = 0; i < DEVCOUNT; i++) if (aPK[i].devid == devid) return aPK[i]; for (byte i = 0; i < DEVCOUNT; i++) if (aPK[i].devid == "") { aPK[i].devid = devid; aPK[i].t = DateTime.Now; return aPK[i]; } return aPK[0]; } public TPressKey FindDialPK() { for (byte i = 0; i < DEVCOUNT; i++) if (aPK[i].b == JDStatus.Dial) return aPK[i]; return aPK[0]; } //-------------------------------------------------------------------- public Form1() { InitializeComponent(); dbFileName = System.Windows.Forms.Application.StartupPath + "\\db1.mdb"; wavPath = System.Windows.Forms.Application.StartupPath + "\\Rec\\"; IniFile ini = new IniFile("JD2000ocx.ini"); if (ini.IniExistFile()) wavPath = ini.IniReadValue("System", "WavFilePath", wavPath); } private void button1_Click(object sender, EventArgs e) { //設(shè)置來電管理器連接端口,第一次連接時使用 axJDComponent1.SetupPorts(); } private void button2_Click(object sender, EventArgs e) { axJDComponent1.Open(); } private void button3_Click(object sender, EventArgs e) { //設(shè)置錄音方式 axJDComponent1.SetSate(); } private void axJDComponent1_OnOpen(object sender, EventArgs e) { textBox1.Text = "JD Open\r\n"; InitAPK(); } private void axJDComponent1_OnClose(object sender, EventArgs e) { textBox1.Text += "JD Close\r\n"; } private string connectionString() { return "Provider=Microsoft.Jet.OLEDB.4.0; User ID=Admin; Password=; Data Source=" + dbFileName; } private string appendRecord(string devid, string s, DateTime t) { string id = DateTime.Now.ToString("yyMMddhhmmssfff"); OleDbConnection conn = new OleDbConnection(connectionString()); conn.Open(); OleDbCommand cmd = new OleDbCommand("Insert Into Tele (id, devid, s, t) Values('" + id + "','" + devid + "','" + s + "',CDate('" + t.ToString("yyyy-MM-dd hh:mm:ss") + "'))", conn); cmd.ExecuteNonQuery(); conn.Close(); return id; } private void updateRecord(TPressKey pk) { OleDbConnection conn = new OleDbConnection(connectionString()); conn.Open(); string str = "Update Tele Set k=" + ((int)pk.b).ToString(); if (File.Exists(wavPath + pk.wavfile)) str += ", wav='" + pk.wavfile + "'"; str += " Where id='" + pk.id + "'"; if (pk.b==JDStatus.Talk) str += " and devid='" + pk.devid + "'"; //textBox1.AppendText(str + "\r\n"); OleDbCommand cmd = new OleDbCommand(str, conn); cmd.ExecuteNonQuery(); conn.Close(); //textBox1.AppendText("Update Success!\r\n"); } private void axJDComponent1_OnRead(object sender, AxJDCompPort.IJDComponentEvents_OnReadEvent e) { //當(dāng)有電話打入時 DateTime t = DateTime.FromOADate(e.t); TPressKey pk; if (e.devid == "D") pk = FindDialPK(); else pk = FindPKey(e.devid); pk.wavfile = e.waveFile; pk.id = appendRecord(e.devid, e.s, t); textBox1.AppendText(e.devid + " " + e.s + " " + t.ToString("MM-dd hh:mm:ss") + "\r\n"); //textBox1.AppendText(pk.devid+" : "+pk.wavfile + "\r\n"); } private void axJDComponent1_OnKeyPress(object sender, AxJDCompPort.IJDComponentEvents_OnKeyPressEvent e) { textBox1.AppendText(e.devid + " " + e.key + "\r\n"); TPressKey pk = FindPKey(e.devid); switch (e.key) { case "R": //響鈴 pk.setStatus(JDStatus.Ring); break; case "T": //提取話筒 if ((pk.b == JDStatus.Ring) & (pk.keyInterval() < 3000)) //3秒內(nèi) { if (pk.id != "") { textBox1.AppendText(e.devid + " " + "接聽來電" + "\r\n"); updateRecord(pk); } pk.setStatus(JDStatus.Talk); //來電已接聽 } else { pk.setStatus(JDStatus.Dial); } break; case "H": //放下話筒 if (File.Exists(wavPath + pk.wavfile)) { textBox1.AppendText("錄音:" + pk.wavfile + "\r\n"); updateRecord(pk); } pk.id = ""; pk.wavfile = ""; pk.setStatus(JDStatus.Idel); break; } } //===================================================================== public class IniFile { //文件INI名稱 public string Path; ////聲明讀寫INI文件的API函數(shù) [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] private static extern void GetWindowsDirectory(StringBuilder WinDir, int count); //類的構(gòu)造函數(shù),傳遞INI文件名 public IniFile(string inipath) { if ((inipath.Substring(2,1)==":")||(inipath.StartsWith("\\\\"))) Path = inipath; else Path = getWindowsDir() + "\\" + inipath; } public string getWindowsDir() { StringBuilder temp = new StringBuilder(255); GetWindowsDirectory(temp, 255); return temp.ToString(); }
|
|
|
|