www.pudn.com > HMRspeech.rar > speechDlg.cpp, change:2009-11-21,size:9920b


// speechDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "speech.h" 
#include "speechDlg.h" 
#include "ListenDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSpeechDlg dialog 
 
CSpeechDlg::CSpeechDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CSpeechDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CSpeechDlg) 
	m_strText = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CSpeechDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSpeechDlg) 
	DDX_Control(pDX, IDC_PICTURE, m_mouth); 
	DDX_Control(pDX, IDC_LIST, m_ListVoices); 
	DDX_Text(pDX, IDC_EDIT, m_strText); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CSpeechDlg, CDialog) 
	//{{AFX_MSG_MAP(CSpeechDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonSpeak) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonStop) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonResume) 
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButtonListen) 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(WM_TTSEVENT, OnMouthEvent) // 响应自定义消息WM_TTSEVENT 
	ON_EN_CHANGE(IDC_EDIT, &CSpeechDlg::OnEnChangeEdit) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSpeechDlg message handlers 
 
BOOL CSpeechDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	InitTextToSpeech(); 
	InitMouthImageList(); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CSpeechDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CSpeechDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CPaintDC dc(this); // device context for painting 
		CDialog::OnPaint(); 
		// Draw into memory DC 
		m_cMouthList.Draw(&dc, 0,  
					m_cMouthRect.TopLeft(), 
					INDEXTOOVERLAYMASK(m_iMouthBmp)); 
		if (m_iMouthBmp%6 == 2) 
		{ 
			m_cMouthList.Draw(&dc, WEYESNAR, m_cMouthRect.TopLeft(), 0); 
		} 
		else if (m_iMouthBmp%6 == 5) 
		{ 
			m_cMouthList.Draw(&dc, WEYESCLO, m_cMouthRect.TopLeft(), 0); 
		} 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CSpeechDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
// 初始化语音引擎并找出系统中的所有 
// 语音语言显示在语音列表中 
void CSpeechDlg::InitTextToSpeech() 
{ 
	if (! m_TextToSpeech.Initialize(m_hWnd)) 
		AfxMessageBox(m_TextToSpeech.GetErrorString()); 
	long lCount = m_TextToSpeech.GetVoiceCount(); 
	WCHAR* pszID; 
	for (long l=0; l<lCount; ++l) 
	{ 
		m_TextToSpeech.GetVoice(&pszID, l); 
		m_ListVoices.AddString(CString(pszID)); 
	} 
	m_TextToSpeech.GetVoice(&pszID, -1); 
	m_ListVoices.SelectString(0, CString(pszID)); 
} 
 
// 初始化朗读者图像列表 
void CSpeechDlg::InitMouthImageList() 
{ 
	m_mouth.GetClientRect(&m_cMouthRect); 
	m_mouth.ClientToScreen(&m_cMouthRect); 
	ScreenToClient(&m_cMouthRect); 
	m_mouth.ShowWindow(SW_HIDE); 
	CBitmap bmp; 
	m_cMouthList.Create(CHARACTER_WIDTH, CHARACTER_HEIGHT, ILC_COLOR32 | 
		ILC_MASK, 1, 0); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICFULL)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH2)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH3)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH4)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH5)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH6)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH7)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH8)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH9)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH10)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH11)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH12)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICMOUTH13)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICEYESNAR)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	bmp.LoadBitmap(MAKEINTRESOURCE(IDB_MICEYESCLO)); 
	m_cMouthList.Add(&bmp, RGB(255,0,255)); 
	bmp.Detach(); 
	m_cMouthList.SetOverlayImage(1, 1); 
	m_cMouthList.SetOverlayImage(2, 2); 
	m_cMouthList.SetOverlayImage(3, 3); 
	m_cMouthList.SetOverlayImage(4, 4); 
	m_cMouthList.SetOverlayImage(5, 5); 
	m_cMouthList.SetOverlayImage(6, 6); 
	m_cMouthList.SetOverlayImage(7, 7); 
	m_cMouthList.SetOverlayImage(8, 8); 
	m_cMouthList.SetOverlayImage(9, 9); 
	m_cMouthList.SetOverlayImage(10, 10); 
	m_cMouthList.SetOverlayImage(11, 11); 
	m_cMouthList.SetOverlayImage(12, 12); 
	m_cMouthList.SetOverlayImage(13, 13); 
	m_cMouthList.SetOverlayImage(14, WEYESNAR); 
	m_cMouthList.SetOverlayImage(15, WEYESCLO); 
} 
 
// 朗读按钮回调函数 
void CSpeechDlg::OnButtonSpeak()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	if (FAILED(m_TextToSpeech.Speak(m_strText.AllocSysString(), SPF_ASYNC))) 
		AfxMessageBox(m_TextToSpeech.GetErrorString()); 
} 
 
// 暂停按钮回调函数 
void CSpeechDlg::OnButtonStop()  
{ 
	// TODO: Add your control notification handler code here 
	m_TextToSpeech.Pause(); 
} 
 
// 继续按钮回调函数 
void CSpeechDlg::OnButtonResume()  
{ 
	// TODO: Add your control notification handler code here 
	m_TextToSpeech.Resume(); 
} 
 
// 选择列表回调函数 
// 将选中的语音语言设为当前的语音 
void CSpeechDlg::OnSelchangeList()  
{ 
	// TODO: Add your control notification handler code here 
	CString sVoice; 
	int nIndex = m_ListVoices.GetCurSel(); 
	m_ListVoices.GetText(nIndex, sVoice); 
	BSTR bstr = sVoice.AllocSysString(); 
	if (FAILED(m_TextToSpeech.SetVoice(&bstr))) 
		AfxMessageBox(m_TextToSpeech.GetErrorString()); 
} 
 
// WM_TTSEVENT消息处理函数 
// 使朗读者的嘴型随着所朗读的文字变化 
// 该消息指示出一个语音朗读事件已完成,即已发出某个音节 
LRESULT CSpeechDlg::OnMouthEvent(WPARAM wParam, LPARAM lParam) 
{ 
	CSpEvent event; 
	while(event.GetFrom(m_TextToSpeech.m_IpVoice) == S_OK) 
	{ 
		switch (event.eEventId) 
		{ 
		case SPEI_VISEME: 
			m_iMouthBmp = g_iMapVisemeToImage[event.Viseme()]; 
			InvalidateRect(m_cMouthRect, false); 
			break; 
		} 
	} 
	return 0; 
} 
 
// 打开听写对话框 
void CSpeechDlg::OnButtonListen()  
{ 
	// TODO: Add your control notification handler code here 
 
	CListenDlg *pListenDlg = new CListenDlg(); 
	pListenDlg->Create(IDD_LISTEN_DIALOG, NULL); 
	pListenDlg->ShowWindow(SW_SHOW); 
	// OnOK(); 
} 
 
void CSpeechDlg::OnEnChangeEdit() 
{ 
	// TODO:  如果该控件是 RICHEDIT 控件,则它将不会 
	// 发送该通知,除非重写 CDialog::OnInitDialog() 
	// 函数并调用 CRichEditCtrl().SetEventMask(), 
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。 
 
	// TODO:  在此添加控件通知处理程序代码 
}