diff --git a/CStest.sln b/CStest.sln index 5f3231a..19ca5da 100644 --- a/CStest.sln +++ b/CStest.sln @@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决 EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "读写文件", "读写文件\读写文件.vcxproj", "{07C6887F-DBA0-4974-9B3D-8F36FB2A914E}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "文字转语音", "文字转语音\文字转语音.vcxproj", "{FDBA484C-9BC5-4323-AC2A-E0D538A17E83}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -93,6 +95,14 @@ Global {07C6887F-DBA0-4974-9B3D-8F36FB2A914E}.Release|x64.Build.0 = Release|x64 {07C6887F-DBA0-4974-9B3D-8F36FB2A914E}.Release|x86.ActiveCfg = Release|Win32 {07C6887F-DBA0-4974-9B3D-8F36FB2A914E}.Release|x86.Build.0 = Release|Win32 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Debug|x64.ActiveCfg = Debug|x64 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Debug|x64.Build.0 = Debug|x64 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Debug|x86.ActiveCfg = Debug|Win32 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Debug|x86.Build.0 = Debug|Win32 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Release|x64.ActiveCfg = Release|x64 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Release|x64.Build.0 = Release|x64 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Release|x86.ActiveCfg = Release|Win32 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/文字转语音/My.rc b/文字转语音/My.rc new file mode 100644 index 0000000..c1c815f Binary files /dev/null and b/文字转语音/My.rc differ diff --git a/文字转语音/framework.h b/文字转语音/framework.h new file mode 100644 index 0000000..5672fb5 --- /dev/null +++ b/文字转语音/framework.h @@ -0,0 +1,49 @@ +#pragma once + +#ifndef VC_EXTRALEAN +#define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料 +#endif + +#include "targetver.h" + +#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的 + +// 关闭 MFC 的一些常见且经常可放心忽略的隐藏警告消息 +#define _AFX_ALL_WARNINGS + +#include // MFC 核心组件和标准组件 +#include // MFC 扩展 + + +#include // MFC 自动化类 + + + +#ifndef _AFX_NO_OLE_SUPPORT +#include // MFC 对 Internet Explorer 4 公共控件的支持 +#endif +#ifndef _AFX_NO_AFXCMN_SUPPORT +#include // MFC 对 Windows 公共控件的支持 +#endif // _AFX_NO_AFXCMN_SUPPORT + +#include // MFC 支持功能区和控制条 + + + + + + + + + +#ifdef _UNICODE +#if defined _M_IX86 +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") +#elif defined _M_X64 +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") +#else +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") +#endif +#endif + + diff --git a/文字转语音/pch.cpp b/文字转语音/pch.cpp new file mode 100644 index 0000000..b6fb8f4 --- /dev/null +++ b/文字转语音/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件 + +#include "pch.h" + +// 当使用预编译的头时,需要使用此源文件,编译才能成功。 diff --git a/文字转语音/pch.h b/文字转语音/pch.h new file mode 100644 index 0000000..9660927 --- /dev/null +++ b/文字转语音/pch.h @@ -0,0 +1,13 @@ +// pch.h: 这是预编译标头文件。 +// 下方列出的文件仅编译一次,提高了将来生成的生成性能。 +// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。 +// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。 +// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。 + +#ifndef PCH_H +#define PCH_H + +// 添加要在此处预编译的标头 +#include "framework.h" + +#endif //PCH_H diff --git a/文字转语音/res/My.rc2 b/文字转语音/res/My.rc2 new file mode 100644 index 0000000..7417cbf Binary files /dev/null and b/文字转语音/res/My.rc2 differ diff --git a/文字转语音/res/文字转语音.ico b/文字转语音/res/文字转语音.ico new file mode 100644 index 0000000..d56fbcd Binary files /dev/null and b/文字转语音/res/文字转语音.ico differ diff --git a/文字转语音/resource.h b/文字转语音/resource.h new file mode 100644 index 0000000..6c74541 --- /dev/null +++ b/文字转语音/resource.h @@ -0,0 +1,24 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ 生成的包含文件。 +// 供 My.rc 使用 +// +#define IDD_MY_DIALOG 102 +#define IDR_MAINFRAME 128 +#define IDC_SLIDER1 1000 +#define IDC_SLIDER2 1001 +#define IDC_STATIC1 1002 +#define IDC_STATIC2 1003 +#define IDC_EDIT1 1004 +#define IDC_EDIT2 1005 +#define IDC_BUTTON1 1006 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 130 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 1007 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/文字转语音/targetver.h b/文字转语音/targetver.h new file mode 100644 index 0000000..79934a3 --- /dev/null +++ b/文字转语音/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。 + +//如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并 +// 将 _WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。 + +#include diff --git a/文字转语音/文字转语音.cpp b/文字转语音/文字转语音.cpp new file mode 100644 index 0000000..277964a --- /dev/null +++ b/文字转语音/文字转语音.cpp @@ -0,0 +1,107 @@ + +// 文字转语音.cpp: 定义应用程序的类行为。 +// + +#include "pch.h" +#include "framework.h" +#include "文字转语音.h" +#include "文字转语音Dlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#endif + + +// C文字转语音App + +BEGIN_MESSAGE_MAP(C文字转语音App, CWinApp) + ON_COMMAND(ID_HELP, &CWinApp::OnHelp) +END_MESSAGE_MAP() + + +// C文字转语音App 构造 + +C文字转语音App::C文字转语音App() +{ + // 支持重新启动管理器 + m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART; + + // TODO: 在此处添加构造代码, + // 将所有重要的初始化放置在 InitInstance 中 +} + + +// 唯一的 C文字转语音App 对象 + +C文字转语音App theApp; + + +// C文字转语音App 初始化 + +BOOL C文字转语音App::InitInstance() +{ + // 如果一个运行在 Windows XP 上的应用程序清单指定要 + // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, + //则需要 InitCommonControlsEx()。 否则,将无法创建窗口。 + INITCOMMONCONTROLSEX InitCtrls; + InitCtrls.dwSize = sizeof(InitCtrls); + // 将它设置为包括所有要在应用程序中使用的 + // 公共控件类。 + InitCtrls.dwICC = ICC_WIN95_CLASSES; + InitCommonControlsEx(&InitCtrls); + + CWinApp::InitInstance(); + + + AfxEnableControlContainer(); + + // 创建 shell 管理器,以防对话框包含 + // 任何 shell 树视图控件或 shell 列表视图控件。 + CShellManager *pShellManager = new CShellManager; + + // 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题 + CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); + + // 标准初始化 + // 如果未使用这些功能并希望减小 + // 最终可执行文件的大小,则应移除下列 + // 不需要的特定初始化例程 + // 更改用于存储设置的注册表项 + // TODO: 应适当修改该字符串, + // 例如修改为公司或组织名 + SetRegistryKey(_T("应用程序向导生成的本地应用程序")); + + C文字转语音Dlg dlg; + m_pMainWnd = &dlg; + INT_PTR nResponse = dlg.DoModal(); + if (nResponse == IDOK) + { + // TODO: 在此放置处理何时用 + // “确定”来关闭对话框的代码 + } + else if (nResponse == IDCANCEL) + { + // TODO: 在此放置处理何时用 + // “取消”来关闭对话框的代码 + } + else if (nResponse == -1) + { + TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n"); + TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n"); + } + + // 删除上面创建的 shell 管理器。 + if (pShellManager != nullptr) + { + delete pShellManager; + } + +#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS) + ControlBarCleanUp(); +#endif + + // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, + // 而不是启动应用程序的消息泵。 + return FALSE; +} + diff --git a/文字转语音/文字转语音.h b/文字转语音/文字转语音.h new file mode 100644 index 0000000..97774b6 --- /dev/null +++ b/文字转语音/文字转语音.h @@ -0,0 +1,32 @@ + +// 文字转语音.h: PROJECT_NAME 应用程序的主头文件 +// + +#pragma once + +#ifndef __AFXWIN_H__ + #error "在包含此文件之前包含 'pch.h' 以生成 PCH" +#endif + +#include "resource.h" // 主符号 + + +// C文字转语音App: +// 有关此类的实现,请参阅 文字转语音.cpp +// + +class C文字转语音App : public CWinApp +{ +public: + C文字转语音App(); + +// 重写 +public: + virtual BOOL InitInstance(); + +// 实现 + + DECLARE_MESSAGE_MAP() +}; + +extern C文字转语音App theApp; diff --git a/文字转语音/文字转语音.vcxproj b/文字转语音/文字转语音.vcxproj new file mode 100644 index 0000000..cd5e074 --- /dev/null +++ b/文字转语音/文字转语音.vcxproj @@ -0,0 +1,215 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + {FDBA484C-9BC5-4323-AC2A-E0D538A17E83} + MFCProj + 文字转语音 + 10.0 + + + + Application + true + v143 + Unicode + Static + + + Application + false + v143 + true + Unicode + Static + + + Application + true + v143 + Unicode + Static + + + Application + false + v143 + true + Unicode + Static + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + true + _WINDOWS;_DEBUG;%(PreprocessorDefinitions) + pch.h + + + Windows + + + false + true + _DEBUG;%(PreprocessorDefinitions) + + + 0x0804 + _DEBUG;%(PreprocessorDefinitions) + $(IntDir);%(AdditionalIncludeDirectories) + + + + + Use + Level3 + true + WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) + pch.h + + + Windows + + + false + true + _DEBUG;%(PreprocessorDefinitions) + + + 0x0804 + _DEBUG;%(PreprocessorDefinitions) + $(IntDir);%(AdditionalIncludeDirectories) + + + + + Use + Level3 + true + true + true + WIN32;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) + pch.h + + + Windows + true + true + + + false + true + NDEBUG;%(PreprocessorDefinitions) + + + 0x0804 + NDEBUG;%(PreprocessorDefinitions) + $(IntDir);%(AdditionalIncludeDirectories) + + + + + Use + Level3 + true + true + true + _WINDOWS;NDEBUG;%(PreprocessorDefinitions) + pch.h + + + Windows + true + true + + + false + true + NDEBUG;%(PreprocessorDefinitions) + + + 0x0804 + NDEBUG;%(PreprocessorDefinitions) + $(IntDir);%(AdditionalIncludeDirectories) + + + + + + + + + + + + + Create + Create + Create + Create + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/文字转语音/文字转语音.vcxproj.filters b/文字转语音/文字转语音.vcxproj.filters new file mode 100644 index 0000000..8bf3396 --- /dev/null +++ b/文字转语音/文字转语音.vcxproj.filters @@ -0,0 +1,63 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 资源文件 + + + + + 资源文件 + + + + + 资源文件 + + + \ No newline at end of file diff --git a/文字转语音/文字转语音Dlg.cpp b/文字转语音/文字转语音Dlg.cpp new file mode 100644 index 0000000..04e3d88 --- /dev/null +++ b/文字转语音/文字转语音Dlg.cpp @@ -0,0 +1,199 @@ + +// 文字转语音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(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(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(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(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("用户取消选择文件")); + } +} diff --git a/文字转语音/文字转语音Dlg.h b/文字转语音/文字转语音Dlg.h new file mode 100644 index 0000000..98bdabd --- /dev/null +++ b/文字转语音/文字转语音Dlg.h @@ -0,0 +1,44 @@ + +// 文字转语音Dlg.h: 头文件 +// + +#pragma once +#include + + +// C文字转语音Dlg 对话框 +class C文字转语音Dlg : public CDialogEx +{ +// 构造 +public: + C文字转语音Dlg(CWnd* pParent = nullptr); // 标准构造函数 + +// 对话框数据 +#ifdef AFX_DESIGN_TIME + enum { IDD = IDD_MY_DIALOG }; +#endif + + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 + + +// 实现 +protected: + HICON m_hIcon; + + // 生成的消息映射函数 + virtual BOOL OnInitDialog(); + afx_msg void OnPaint(); + afx_msg HCURSOR OnQueryDragIcon(); + DECLARE_MESSAGE_MAP() +public: + afx_msg void OnBnClickedOk(); + + ISpVoice* pVoice = NULL; + CSliderCtrl m_sound; + CSliderCtrl m_velocity; + afx_msg void OnBnClickedCancel(); + afx_msg void OnNMCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult); + afx_msg void OnNMCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult); + afx_msg void OnBnClickedButton1(); +};