CSTest/文字转语音/文字转语音Dlg.cpp

200 lines
4.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 文字转语音Dlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "文字转语音.h"
#include "文字转语音Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C文字转语音Dlg 对话框
C文字转语音Dlg::C文字转语音Dlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MY_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void C文字转语音Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER1, m_sound);
DDX_Control(pDX, IDC_SLIDER2, m_velocity);
}
BEGIN_MESSAGE_MAP(C文字转语音Dlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &C文字转语音Dlg::OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, &C文字转语音Dlg::OnBnClickedCancel)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, &C文字转语音Dlg::OnNMCustomdrawSlider1)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, &C文字转语音Dlg::OnNMCustomdrawSlider2)
ON_BN_CLICKED(IDC_BUTTON1, &C文字转语音Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// C文字转语音Dlg 消息处理程序
BOOL C文字转语音Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
SetDlgItemInt(IDC_EDIT2,1);
m_sound.SetRange(0, 100);
m_velocity.SetRange(0, 20);
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice);
if (!SUCCEEDED(hr))
{
MessageBox(_T("语音库初始化失败"));
exit(0);
}
m_sound.SetPos(100);
m_velocity.SetPos(10);
pVoice->SetRate(0); //播放速度(-10 10
pVoice->SetVolume(100); //音量
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void C文字转语音Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR C文字转语音Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void C文字转语音Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CString str;
GetDlgItemText(IDC_EDIT1, str);
if(str !=_T(""))
{
int count = GetDlgItemInt(IDC_EDIT2);
for (int i = 0; i < count; i++)
{
pVoice->Speak(str, 0, NULL);
}
}
//CDialogEx::OnOK();
}
void C文字转语音Dlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
pVoice->Release();
pVoice = NULL;
::CoUninitialize();
CDialogEx::OnCancel();
}
void C文字转语音Dlg::OnNMCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
SetDlgItemInt(IDC_STATIC1,m_sound.GetPos());
pVoice->SetVolume(m_sound.GetPos()); //音量
*pResult = 0;
}
void C文字转语音Dlg::OnNMCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
SetDlgItemInt(IDC_STATIC2, m_velocity.GetPos());
pVoice->SetRate(m_velocity.GetPos() - 10);
*pResult = 0;
}
void C文字转语音Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
// 构造打开文件对话框
CFileDialog fileDlg(TRUE, _T("txt"), NULL, 0, szFilter, this);
CString strFilePath;
// 显示打开文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
strFilePath = fileDlg.GetPathName();
SetDlgItemText(IDC_EDIT1, strFilePath);
CFile file;
if (file.Open(strFilePath, CFile::modeRead))
{
char* buf = new char[file.GetLength() + 1];
buf[file.GetLength()] = '\0';
file.Read(buf, file.GetLength());
file.Close();
CString str;
str = buf;
delete[]buf;
SetDlgItemText(IDC_EDIT1, str);
}
else
{
MessageBox(_T("文件打开失败"));
}
}
else
{
MessageBox(_T("用户取消选择文件"));
}
}