⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog_database.cpp

📁 声音识别系统的源代码
💻 CPP
字号:
// Dialog_Database.cpp : implementation file
//

#include "stdafx.h"
#include "VoiceCtrl.h"
#include "Dialog_Database.h"
#include "Dialog_Study.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Dialog_Database dialog


Dialog_Database::Dialog_Database(CWnd* pParent /*=NULL*/)
	: CDialog(Dialog_Database::IDD, pParent)
{
	//{{AFX_DATA_INIT(Dialog_Database)
	//}}AFX_DATA_INIT
    //init code
    	// TODO: Add extra initialization here
    ///////////////////////////////////////////////////     
    //  
   

}

BOOL Dialog_Database::OnInitDialog() 
{
	CDialog::OnInitDialog(); 
	// TODO: Add extra initialization here
	int status=0; 

	//初始化列表框格式    
    m_listctrl.InsertColumn(0,_T("Index"),LVCFMT_LEFT,20,0);
 	m_listctrl.InsertColumn(1,_T("Command"),LVCFMT_LEFT,50,1);
 	m_listctrl.InsertColumn(2,_T("Meaning"),LVCFMT_LEFT,70,2);
 	m_listctrl.InsertColumn(3,_T("FileName"),LVCFMT_LEFT,100,3);
   	//设置ListCtrl可以整行选择和网格条纹
    DWORD styles = m_listctrl.GetExtendedStyle();
    m_listctrl.SetExtendedStyle(styles|LVS_EX_FULLROWSELECT);
    m_listctrl.SetBkColor(RGB(0xFF, 0xFF, 0xE0));
    m_listctrl.SetTextBkColor(RGB(0x82, 0xFF, 0xE0));
 
	//读取ini文件初始化列表框数据
    status=ReadFile();  
        
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void Dialog_Database::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Dialog_Database)
	DDX_Control(pDX, IDC_ListCrtl, m_listctrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(Dialog_Database, CDialog)
	//{{AFX_MSG_MAP(Dialog_Database)
	ON_BN_CLICKED(IDC_BUTTON_Delete, OnBUTTONDelete)
	ON_BN_CLICKED(IDC_BUTTON_Add, OnBUTTONAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Dialog_Database message handlers

void Dialog_Database::OnOK() 
{    //关闭窗口	
	CDialog::OnOK();
    
}

void Dialog_Database::OnBUTTONDelete() 
{    //删除选中的一行
	 // TODO: Add your control notification handler code here
     //1 删除这一行    

     //1 删除一个项目
     int c=0,nItem=0;
     int thisselectitem=0;
	 int status=0;
     // CString tt,thistype;
 	 CString index;
	 CString filename;
     int SelectItem;

	 POSITION pos = m_listctrl.GetFirstSelectedItemPosition();
     if (pos == NULL){ 
	    MessageBox(_T("No items were selected!"),_T("!!"));
		return ;
	 }
     else
	 {  
       SelectItem=m_listctrl.GetNextSelectedItem(pos);
       filename=m_listctrl.GetItemText(SelectItem,3);
	   m_listctrl.DeleteItem(SelectItem);
       SelectItem=count;
	   count--; 
	 }   
	   
	 for(c=0;c<count;c++){
       index.Format(_T("%d"),c+1);    
       m_listctrl.SetItemText(c,0,index);   
	 }
     //调用写文件 方法 ,将改动响应到文件
	 //2 重写文件
     status=WriteFile();    
     //3 删除相应的声音文件     
     DeleteFile(filename);	   

}
int Dialog_Database::ReadFile()
{
	 //int count=0;
	 int mark=0;
     int nBeg=0;
	 int nEnd=0;

     CFile file;     	 
	 CString alltext;  
     CString index,command,meaning,filename;    
     
	 TCHAR pbuf[2000];

	 if(!file.Open(_T("database.txt"),CFile::modeRead)){
	     return 0;
	 }
	 if(file.GetLength()==0) return 0;     
	 
     int a=file.GetLength();  
	 file.Read(pbuf,a);     
     alltext=CString(pbuf);  //	  	 
     
     count=0;
	 // 
     while(1)
	 {	             
         nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);   
         command=alltext.Mid(nBeg+1,nEnd-nBeg-2);
        
		 if(nBeg<1 && 1==mark){
		     break;
		 }
         mark=1;
		 //MessageBox(command);
         
         nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);          
		 filename=alltext.Mid(nBeg+1,nEnd-nBeg-2);
         //MessageBox(filename);

		 nBeg=alltext.Find('=',nEnd);   
         nEnd=alltext.Find('\n',nBeg+1);          
		 meaning=alltext.Mid(nBeg+1,nEnd-nBeg-2);
	     //MessageBox(meaning);

         index.Format(_T("%d"),count);          

         m_listctrl.InsertItem(count,_T("")); 
         m_listctrl.SetItemText(count,0,index);
	     m_listctrl.SetItemText(count,1,command);
         m_listctrl.SetItemText(count,2,meaning);
         m_listctrl.SetItemText(count,3,filename);
         count++;	         
	 }   	 
     file.Close(); 
     return 1;
}


int Dialog_Database::WriteFile()
{  
	//将listctrl的内容重新覆盖文件	 
     CString index;
     CString command;    //命令
	 CString meaning;    //含义
	 CString filename;   //文件名	  
	 int length=0 ;
	 int i=0;  
	 CFile file;

	 file.Open(_T("database.txt"),CFile::modeCreate|CFile::modeWrite);
     	 
	 for(i=0;i<count;i++){
	     index=m_listctrl.GetItemText(i,0);
         command =m_listctrl.GetItemText(i,1);
		 meaning =m_listctrl.GetItemText(i,2);
		 filename =m_listctrl.GetItemText(i,3);
		  
         length=2*command.GetLength();
    	 file.Write(_T("command=")+command+_T("\r\n"),16+length+4);  //写 command
         length=2*filename.GetLength();   
         file.Write(_T("filename=")+filename+_T("\r\n"),18+length+4);//写 filename
         length=2*meaning.GetLength();  
         file.Write(_T("meaning=")+meaning+_T("\r\n"),16+length+4);  //写 meaning
	   		    
     }//for   
      
    
     file.Close();
	 return 1;
}

void Dialog_Database::OnBUTTONAdd() 
{   //添加新的声音文件到语音库
	// TODO: Add your control notification handler code here
	Dialog_Study mystudy;
	mystudy.DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -