标签:
程序片段(01):01.字符串.c
内容概要:字符串
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
//00.语音合成与语音识别:
// 语音合成:文字-->语音
// 语音识别:语音-->文字
// 注:任何语音操作都应当使用宽字符!
//01.宽窄字符问题1:
// 1.窄字符采用单字节存储,宽字符采用双字节存储!
// 宽窄字符的"本质区别"在于存储数据的盒子尺寸不一样!
// 2.宽窄字符当中使用sizeof关键字的情况区分:
// 1.sizeof求取的是数据类型所占用的尺寸而不是实际数据本身所占用的尺寸
// 2.但是操作"实际数据本身"的时候都是一样的
// 注:如果项目采用Unicode字符集,那所有非ASCII字符集范围内
// 的字符都必须采用宽字符进行存储(也即是除开英文之外的字符)
// 再加上本地化的设置才能正确的表示文字信息
int main01(void)
{
char ch = ‘A‘;//窄字符-->1个字节:不可以存储单个汉字儿
wchar_t wch = L‘A‘;//宽字符-->2个字节:可以存储单个汉字儿
printf("%d ,%d \n", sizeof(ch), sizeof(wch));
printf("%d ,%d \n", ch, wch);//内容一致-->ASCII码值一样
system("pause");
}
//02.宽窄字符问题2:
// 1.宽窄字符只是存储数据的盒子不一样:
// 如果存储内容一样,那么二进制数据本质一样
// 2.Unicode字符集+(语言环境设置+宽字符使用):
// 才能使得除开标准ASCII编码集的字符之外的字符能够正确显示
// 3.只要在Unicode字符集的情况之下,所有内容都采用宽字符
// 就能保证字符的正确存储与使用
// 注:宽字符也是字符,只不过表象比窄字符多了一个宽字符标识符"L"
// 但还是需要使用单引号(‘‘)进行宽字符标识
int main02(void)
{
char ch = ‘A‘;
putchar(ch);
//wchar_t wch = L‘A‘;
//putwchar(wch);
setlocale(LC_ALL, "zh-CN");
wchar_t wch = L‘我‘;
putwchar(wch);//采用字符集+设定语言环境+使用宽字符+使用宽字符操作函数-->字符正确存储并使用
system("pause");
}
//03.宽窄字符问题3:
// 1.VS默认配置情况下,所有字符都采用窄字符进行存储:
// 因为窄字符对于ASCII编码表当中的字符都能够进行兼容
// 2.代码区的内容不可以进行修改!(编译不报错,运行直接挂掉!)
// 找到代码区+突破权限
// 3.代码区当中的两块儿重要内存(常量池和符号表):
// 常量池:只需要拷贝指针,直接进行访问(类似于函数传参,针对于数组无副本机制)
// 提高内存操作效率
// 符号表:但需要拷贝数据,生成于寄存器(必须通过寄存器进行数据的传递操作)
// 注:只要是代码区的内容,就不可以直接进行修改,无论是常量池内容还是符号表内容
// 但是通过Detours这款工具就可以突破任何Windows程序的代码区"访问权限",并且
// 可以对该代码区的内容进行直接修改"修改权限"
int main03(void)
{
char * pStr = "notepad";
printf("%d \n", sizeof(pStr));
printf("%d \n", sizeof("notepad"));
printf("%p \n", pStr);//该地址属于(内存-->代码区)的地址
//*pStr = ‘A‘;//由于该指针所指向的内存区块儿位于代码区,因此只可读不可写[因为这里访问的字符串位于代码区常量池]
//"notepad"这个字符串属于代码区的常量池当中,该常量池也被称作为只读常量池
//"notepad"字符串都存储于代码区常量池,非字符串常量需要拷贝数据到寄存器才能对内存进行间接操作
//字符串传递的是地址
system("pause");
}
//04.宽窄字符问题4:
// 1.针对于英文,使用宽窄字符的效果都一样,不同的只是存储字符的盒子尺寸不一样
// 宽窄字符的针对对象:非英文|非标准ASCII码表包含字符
// 2.无论是针对于窄字符字符串还是宽字符字符串,该字符串的结尾标识符都是字符‘\0‘
// 注:只不过区分窄字符(‘\0‘)和宽字符(L‘\0‘)所占用的存储空间尺寸不一样!
// (1).strlen和sizeof();的区别(一个是求取有效字符数;一个是求取实际"占用"字节数)
// 区分:"占用"和"实际"两个关键词儿的区别
// (2).sizeof();取值运算符(星号:"*")加字符指针(区分宽窄字符指针),
// 所求字节数为单个相应字符(宽窄)所占用的字节数
// 3.宽窄字符的打印:
// wprintf+L"格式字符串"+格式控制符
// 注:严格区分"格式字符串"和"格式控制符"之间的区别
// 4.字符指针所指向的数据有多重解析方式:
// 字符串层面的意义解析方式(字符串函数)+字符层面的解析范式(sizeof关键字)
// 注:切忌注意!
int main04(void)
{
setlocale(LC_ALL, "zh-CN");
wchar_t * pWchar = L"你好天朝";//代码区常量池
printf("%d, %d \n", sizeof(pWchar), sizeof(*pWchar));//特殊点!->两个字节
printf("%d \n", sizeof(L"你好天朝"));//实际占用内存尺寸!
//printf("%p \n", pWchar);
wprintf(L"%p \n", pWchar);
system("pause");
}
//05.通过字符指针访问字符指针所指向的内存实体,只能访问到单个字符的效果!
// 数组:一旦前置手动初始化,那么后置一定是默认初始化,数据自动清为零
// 无论前置初始化的内容是什么,后置自动初始化的数据都为0
// 注:初始化与赋值的概念不同
// 取值运算符+字符指针-->只能访问到单个字符(区分宽窄字符!)
int main05(void)
{
char * pStr = "notepad";//窄字符字符串,pStr是指针变量,存储的是(代码区常量池)中的常量字符串的首地址
//pStr可以存储不同的指针,属于变量指针
*pStr = ‘A‘;//随时注意指针变量所指向的内存空间的访问权限(代码区只可读,不可写,除非借助Detours)
char str[100] = "calc";
//str = 1;//str作为数组名是常量指针
//拷贝字符串到数组,等同于对数组的前置初始化,后置默认初始化,因此后续数据全部清零
printf("%p \n", str);
//system(str);
*str = ‘X‘;//通过(取值运算符+字符指针)只能访问到单个字符(区分宽窄字符)
system("pause");
}
//06.宽窄字符数组注意事项:
// 1.数组名一定不可以直接进行修改!-->常量指针
// 2.C语言判断两个字符串是否相等的方式strcmp(str1, str2);函数
// 注:sizeof();和strlen();所求取的长度意义不同
// sizeof();求取的是真实的内存尺寸!
// strlen();和wstrlen();只是求取"有效"字符个数!
int main06(void)
{
char str1[100] = "calc";
char str2[100] = "calc1";
//char * p;
//str1 = str2;//比较的是常量指针的值
if (0 == strcmp(str1, str2))
{
printf("相等! \n");
}
else
{
printf("不相等! \n");
}
wchar_t wStr[100] = L"您好天朝";
printf("%d \n", sizeof(wStr));
system("pause");
}
程序片段(02):speechrecognition.cpp
内容概要:语音识别
#include <windows.h>
#include <atlstr.h>
#include <sphelper.h>
#include <sapi.h>
#include<comutil.h>
#include<string.h>//01.所需头文件
#pragma comment(lib,"sapi.lib")
#pragma comment(lib, "comsupp.lib") //02.链接静态库
#define GID_CMD_GR 333333
#define WM_RECOEVENT WM_USER+1
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//03.所需COM组件:通过COM组件实现语音(识别|合成)功能
char szAppName[] = "TsinghuaYincheng";
BOOL b_initSR;
BOOL b_Cmd_Grammar;
CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
int speak(wchar_t *str);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance =hInstance;
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName =szAppName;
wndclass.lpszMenuName =NULL;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
speak(L"尹成是一个猥琐男,曾彬更加猥琐,Hello XiaoBin i love you!!!");//04.语音合成:将文件转化为语音(注:使用宽字符)
//05.关于语音方面的编程概念:
// 1.什么叫做语音识别?什么叫做语音合成?
// 语音识别:将声音转化为文字
// 语音合成:将文字转化为声音
// 2.语音识别必须采用宽字符!
// 配置:项目属性-->Unicode字符集-->宽字符操作(所有都使用宽字符)
// speak(NULL);//表示不说话
hwnd=CreateWindow(szAppName,
TEXT("清华-尹成语音识别教程"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
{
//初始化COM端口
::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
//创建识别引擎COM实例为共享型
HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
//创建识别上下文接口
if(SUCCEEDED(hr))
{
hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
}
else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
//设置识别消息,使计算机时刻监听语音消息
if(SUCCEEDED(hr))
{
hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
}
else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
//设置我们感兴趣的事件
if(SUCCEEDED(hr))
{
ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
}
else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
//创建语法规则
b_Cmd_Grammar=TRUE;
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
}
hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
WCHAR wszXMLFile[20]=L"er.xml";
MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
}
b_initSR=TRUE;
//在开始识别时,激活语法进行识别
hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
return 0;
}
case WM_RECOEVENT:
{
RECT rect;
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
USES_CONVERSION;
CSpEvent event;
while(event.GetFrom(m_cpRecoCtxt)==S_OK)
{
switch(event.eEventId)
{
case SPEI_RECOGNITION:
{
static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
CSpDynamicString dstrText;
//取得识别结果
if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
{
dstrText=wszUnrecognized;
}
BSTR SRout;
dstrText.CopyToBSTR(&SRout);
char* lpszText2 = _com_util::ConvertBSTRToString(SRout);//06.获取语音识别之后所生成的字符串
if(b_Cmd_Grammar)
{
char * str[15] = {"我是学霸","清华土匪","天下无双","吴伟","曾彬",//07.查表法所对应的字符串表
"记事本","计算器","关机","重启","取消","画图板",
"何栋","林振华","聂千琳","赵学辉"};
int i = -1;
for (int j = 0; j < 15;j++)
{
if (strcmp(str[j], lpszText2) == 0)
{
i = j;
MessageBoxA(0, lpszText2, lpszText2, 0);//弹出对话框
DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//在窗体当中写一段儿文字
}
}
switch (i)//08.根据查表法所查询到的结果,进行针对于结果的处理
{
case -1:
break;
case 0:
speak(L"男神是学霸");
break;
case 1:
speak(L"猥琐男尹成");
break;
case 2:
speak(L"曾彬猥琐的天下无创");
break;
case 3:
speak(L"男神帅的惊动了白宫");
break;
case 4:
speak(L"曾彬猥琐的的惊动了白宫");
break;
case 5:
system("start calc");//09.需要采用异步进行打开
break;
case 6:
system("start notepad");
break;
case 7:
system("shutdown -s -t 600");
break;
case 8:
system("shutdown -r -t 600");
break;
case 9:
system("shutdown -a");//10.取消(关机|重启)指令
break;
case 10:
system("start mspaint");
break;
case 11:
speak(L"何栋向三大金刚问好");//11.注意:通过语音识别的联系,可以提高语音识别的精确度!
break;
case 12:
speak(L"振华睡得很晚");//12.Debug模式相比Release模式更加的消耗资源
break;
case 13:
speak(L"聂千琳爱编码");//13.某些COM组件的调用需要在Debug模式下进行
break;
case 14:
speak(L"赵学辉爱编码");//14.经过语音识别训练之后,电脑的语音识别效果更佳!
break;
default:
break;
}
}
}
}
}
return TRUE;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
#pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
int speak(wchar_t *str)
{
ISpVoice * pVoice = NULL;
::CoInitialize(NULL);
//获取ISpVoice接口:
long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
hr = pVoice->Speak(str, 0, NULL);
pVoice->Release();
pVoice = NULL;
//千万不要忘记:
::CoUninitialize();
return TRUE;
}
程序片段(03):speechrecognition.cpp
内容概要:语音识别
#include <windows.h>
#include <atlstr.h>
#include <sphelper.h>
#include <sapi.h>
#include<comutil.h>
#include<string.h>
#pragma comment(lib,"sapi.lib")
#pragma comment(lib, "comsupp.lib")
#define GID_CMD_GR 333333
#define WM_RECOEVENT WM_USER+1
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szAppName[] = "TsinghuaYincheng";
BOOL b_initSR;
BOOL b_Cmd_Grammar;
CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
int speak(wchar_t *str);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance =hInstance;
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName =szAppName;
wndclass.lpszMenuName =NULL;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
speak(L"尹成是一个猥琐男");
// speak(NULL);
hwnd=CreateWindow(szAppName,
TEXT("清华-尹成语音识别教程"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
{
//初始化COM端口
::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
//创建识别引擎COM实例为共享型
HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
//创建识别上下文接口
if(SUCCEEDED(hr))
{
hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
}
else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
//设置识别消息,使计算机时刻监听语音消息
if(SUCCEEDED(hr))
{
hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
}
else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
//设置我们感兴趣的事件
if(SUCCEEDED(hr))
{
ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
}
else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
//创建语法规则
b_Cmd_Grammar=TRUE;
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
}
hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
WCHAR wszXMLFile[20]=L"er.xml";
MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
}
b_initSR=TRUE;
//在开始识别时,激活语法进行识别
hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
return 0;
}
case WM_RECOEVENT:
{
RECT rect;
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
USES_CONVERSION;
CSpEvent event;
while(event.GetFrom(m_cpRecoCtxt)==S_OK)
{
switch(event.eEventId)
{
case SPEI_RECOGNITION:
{
static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
CSpDynamicString dstrText;
//取得识别结果
if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
{
dstrText=wszUnrecognized;
}
BSTR SRout;
dstrText.CopyToBSTR(&SRout);
char* lpszText2 = _com_util::ConvertBSTRToString(SRout);
if(b_Cmd_Grammar)
{
if (strcmp("跳跃",lpszText2)==0)
{
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
}
if (strcmp("趴下", lpszText2) == 0)
{
keybd_event(‘Y‘, 0, 0, 0);//按下
keybd_event(‘Y‘, 0, 2, 0);//松开
}
if (strcmp("后前前", lpszText2) == 0)
{
keybd_event(‘S‘, 0, 0, 0);//按下
keybd_event(‘S‘, 0, 2, 0);//松开
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//01.鼠标左键按下才开始攻击鼠标位置的人物!
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
////MapVirtualKey映射
//keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
//keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("左右前", lpszText2) == 0)
{
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘D‘, 0, 0, 0);//按下
keybd_event(‘D‘, 0, 2, 0);//松开
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (strcmp("火冒三丈", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下//02.C语言函数模拟反斜杠("\")
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(‘G‘, 0, 0, 0);//按下
keybd_event(‘G‘, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN,0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("旋风刀", lpszText2) == 0)
{
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘D‘, 0, 0, 0);//按下
keybd_event(‘D‘, 0, 2, 0);//松开
keybd_event(‘S‘, 0, 0, 0);//按下
keybd_event(‘S‘, 0, 2, 0);//松开
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (strcmp("大枪", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘E‘, 0, 0, 0);//按下
keybd_event(‘E‘, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘P‘, 0, 0, 0);//按下
keybd_event(‘P‘, 0, 2, 0);//松开
keybd_event(‘O‘, 0, 0, 0);//按下
keybd_event(‘O‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
keybd_event(VK_NUMPAD7, 0, 0, 0);//按下 //03.数字键所对应的虚拟映射键
keybd_event(VK_NUMPAD7, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("大刀", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘E‘, 0, 0, 0);//按下
keybd_event(‘E‘, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘P‘, 0, 0, 0);//按下
keybd_event(‘P‘, 0, 2, 0);//松开
keybd_event(‘O‘, 0, 0, 0);//按下
keybd_event(‘O‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
keybd_event(VK_NUMPAD6, 0, 0, 0);//按下
keybd_event(VK_NUMPAD6, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("刺蛇", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘E‘, 0, 0, 0);//按下
keybd_event(‘E‘, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘P‘, 0, 0, 0);//按下
keybd_event(‘P‘, 0, 2, 0);//松开
keybd_event(‘O‘, 0, 0, 0);//按下
keybd_event(‘O‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
keybd_event(VK_NUMPAD4, 0, 0, 0);//按下
keybd_event(VK_NUMPAD4, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("大剑", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘E‘, 0, 0, 0);//按下
keybd_event(‘E‘, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘P‘, 0, 0, 0);//按下
keybd_event(‘P‘, 0, 2, 0);//松开
keybd_event(‘O‘, 0, 0, 0);//按下
keybd_event(‘O‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
keybd_event(VK_NUMPAD1, 0, 0, 0);//按下
keybd_event(VK_NUMPAD1, 0, 2, 0);//松开
keybd_event(VK_NUMPAD5, 0, 0, 0);//按下
keybd_event(VK_NUMPAD5, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
if (strcmp("大锤", lpszText2) == 0)
{
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开 \ang
keybd_event(‘W‘, 0, 0, 0);//按下
keybd_event(‘W‘, 0, 2, 0);//松开
keybd_event(‘E‘, 0, 0, 0);//按下
keybd_event(‘E‘, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);//按下
keybd_event(‘A‘, 0, 2, 0);//松开
keybd_event(‘P‘, 0, 0, 0);//按下
keybd_event(‘P‘, 0, 2, 0);//松开
keybd_event(‘O‘, 0, 0, 0);//按下
keybd_event(‘O‘, 0, 2, 0);//松开
keybd_event(‘N‘, 0, 0, 0);//按下
keybd_event(‘N‘, 0, 2, 0);//松开
keybd_event(VK_SPACE, 0, 0, 0);//按下
keybd_event(VK_SPACE, 0, 2, 0);//松开
keybd_event(VK_NUMPAD1, 0, 0, 0);//按下
keybd_event(VK_NUMPAD1, 0, 2, 0);//松开
keybd_event(VK_NUMPAD8, 0, 0, 0);//按下
keybd_event(VK_NUMPAD8, 0, 2, 0);//松开
//MapVirtualKey映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
}
}
}
}
return TRUE;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
system("taskkill /f /im 语音识别.exe");
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
#pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
int speak(wchar_t *str)
{
ISpVoice * pVoice = NULL;
::CoInitialize(NULL);
//获取ISpVoice接口:
long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
hr = pVoice->Speak(str, 0, NULL);
pVoice->Release();
pVoice = NULL;
//千万不要忘记:
::CoUninitialize();
return TRUE;
}
//04.识别游戏资源文件的修改
//05.区分:游戏引擎和游戏脚本
//06.逻辑文件+剧情文件+资源文件
// 任意进行修改
//07.游戏文件通常采用zip格式进行压缩
// 注:解压缩游戏资源文件需要注意目录层级!
//08.大多数都是PAK文件
程序片段(04):speechrecognition.cpp
内容概要:语音识别
#include <windows.h>
#include <atlstr.h>
#include <sphelper.h>
#include <sapi.h>
#include<comutil.h>
#include<string.h>
#pragma comment(lib,"sapi.lib")
#pragma comment(lib, "comsupp.lib")
#define GID_CMD_GR 333333
#define WM_RECOEVENT WM_USER+1
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szAppName[] = "TsinghuaYincheng";
BOOL b_initSR;
BOOL b_Cmd_Grammar;
CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
int speak(wchar_t *str);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance =hInstance;
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName =szAppName;
wndclass.lpszMenuName =NULL;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
speak(L"尹成是一个猥琐男");
// speak(NULL);
hwnd=CreateWindow(szAppName,
TEXT("清华-尹成语音识别教程"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
{
//初始化COM端口
::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
//创建识别引擎COM实例为共享型
HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
//创建识别上下文接口
if(SUCCEEDED(hr))
{
hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
}
else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
//设置识别消息,使计算机时刻监听语音消息
if(SUCCEEDED(hr))
{
hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
}
else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
//设置我们感兴趣的事件
if(SUCCEEDED(hr))
{
ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
}
else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
//创建语法规则
b_Cmd_Grammar=TRUE;
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
}
hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
WCHAR wszXMLFile[20]=L"er.xml";
MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
}
b_initSR=TRUE;
//在开始识别时,激活语法进行识别
hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
return 0;
}
case WM_RECOEVENT:
{
RECT rect;
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
USES_CONVERSION;
CSpEvent event;
while(event.GetFrom(m_cpRecoCtxt)==S_OK)
{
switch(event.eEventId)
{
case SPEI_RECOGNITION:
{
static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
CSpDynamicString dstrText;
//取得识别结果
if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
{
dstrText=wszUnrecognized;
}
BSTR SRout;
dstrText.CopyToBSTR(&SRout);
char* lpszText2 = _com_util::ConvertBSTRToString(SRout);
if(b_Cmd_Grammar)
{
if (strcmp("我是学霸",lpszText2)==0)
{
MessageBoxA(0, lpszText2, lpszText2, 0);
//DrawText(hdc,TEXT("我是学霸"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
if (strcmp("清华土匪", lpszText2) == 0)
{
MessageBoxA(0, lpszText2, lpszText2, 0);
//DrawText(hdc,TEXT("我是学霸"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
if (strcmp("吴伟", lpszText2) == 0)
{
MessageBoxA(0, lpszText2, lpszText2, 0);
speak(L"吴伟帅的惊动了党中央国务院");
//DrawText(hdc,TEXT("我是学霸"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
if (strcmp("曾彬", lpszText2) == 0)
{
MessageBoxA(0, lpszText2, lpszText2, 0);
speak(L"曾彬猥琐的惊动了妇女偶像");
//DrawText(hdc,TEXT("我是学霸"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
}
}
}
}
return TRUE;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
#pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
int speak(wchar_t *str)
{
ISpVoice * pVoice = NULL;
::CoInitialize(NULL);
//获取ISpVoice接口:
long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
hr = pVoice->Speak(str, 0, NULL);
pVoice->Release();
pVoice = NULL;
//千万不要忘记:
::CoUninitialize();
return TRUE;
}
程序片段(05):热键.c
内容概要:热键
//01.区分:普通应用程序和Win应用程序!
// 主函数(入口点)的不同:
// 普通应用程序:main();
// Win应用程序:WinMain();
// 注:普通应用程序是含有main();函数的,而Dll动态库当中
// 没有main();函数
#include <Windows.h>
//02.Windows应用程序入口函数格式:
// APIENTRY:
// Win应用程序入口标识("APIENTRY")
// cInstance:
// 当前实例(当前窗口句柄)
// pInstance:
// 父级实例(父级窗口句柄)
// cmdLine:
// 命令行参数
// mcmdshow:
// 控制显示或者隐藏
int APIENTRY WinMain(HINSTANCE cInstance, HINSTANCE pInstance, LPSTR cmdLine, int mcmdshow)
{
//03.RegisterHotKey();函数格式:
// NULL:表示给系统窗口注册热键
// 0x001:表示给组合键编号(16进制整数形式)
// MOD_CONTROL | MOD_ALT:
// 表示组合键除开最后一个字符
// ‘M‘:表示组合键最后一个字符
RegisterHotKey(NULL, 0x001, MOD_CONTROL | MOD_ALT, ‘M‘);
RegisterHotKey(NULL, 0x002, MOD_CONTROL | MOD_ALT, ‘N‘);
RegisterHotKey(NULL, 0x003, MOD_CONTROL | MOD_ALT, ‘B‘);
//04.基于Windows窗体的消息机制控制:
// 1.循环获取消息队列当中的消息
// 2.根据消息对列的获取情况判断
// 注:获取队列消息+对获取情况判断
MSG msg;//描述消息的结构体
while (GetMessage(&msg, NULL, 0, 0))//死循环
{
if (WM_HOTKEY == msg.wParam)//热键判断
{
if (0x001 == msg.wParam)
{
//05.控制按键状态的两个宏指令:
// KEYEVENTF_EXTENDEDKEY(0)-->按下
// KEYEVENTF_KEYUP(2)-->松开
//06.字母键的大小写:
// 1.大写字母:w就是大写字母
// 2.小写字母:(shift+w)组合小写字母键
keybd_event(‘S‘, 0, KEYEVENTF_EXTENDEDKEY, 0);//按下
keybd_event(‘S‘, 0, KEYEVENTF_KEYUP, 0);//松开
keybd_event(‘W‘, 0, 0, 0);
keybd_event(‘W‘, 0, 2, 0);
keybd_event(‘W‘, 0, 0, 0);
keybd_event(‘W‘, 0, 2, 0);
//07.鼠标事件状态控制:
// 采用mouse_event();函数控制
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
else if (0x002 == msg.wParam)
{
//08.按键(‘\‘)用VK_OME_102进行表示
keybd_event(VK_OEM_102, 0, 0, 0);//按下
keybd_event(VK_OEM_102, 0, 2, 0);//松开
keybd_event(‘A‘, 0, 0, 0);
keybd_event(‘A‘, 0, 2, 0);
keybd_event(‘N‘, 0, 0, 0);
keybd_event(‘N‘, 0, 2, 0);
keybd_event(‘G‘, 0, 0, 0);
keybd_event(‘G‘, 0, 2, 0);//表示顺序按下\ang
//09.MapVirtualKey映射:
// 字典虚拟键映射
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0);//按下
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 2, 0);//松开
}
else
{
keybd_event(‘A‘, 0, 0, 0);
keybd_event(‘A‘, 0, 2, 0);
keybd_event(‘D‘, 0, 0, 0);
keybd_event(‘D‘, 0, 2, 0);
keybd_event(‘S‘, 0, 0, 0);
keybd_event(‘S‘, 0, 2, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
//MessageBoxA(0, "即将推出!", "确定", 0);
//10.取消注册热键:
// NULL:表示系统
// 0x001:组合键标识ID
//UnregisterHotKey(NULL, 0x001);
//UnregisterHotKey(NULL, 0x001);
//UnregisterHotKey(NULL, 0x003);
}
}
}
//11.注册的快捷键给系统:
// 于是其他程序在运行过程中;
// 只要触发了快捷键,就会自动触发按键函数
// 触动按键(进行字符输入)
//the modifier of hot key:
//#define MOD_ALT 0x0001
//#define MOD_CONTROL 0x0002
//#define MOD_SHIFT 0x0004
//--------------------------------------
//#define MOD_LEFT 0x8000
//#define MOD_RIGHT 0x4000
//--------------------------------------
//#define MOD_ON_KEYUP 0x0800
//#define MOD_IGORE_ALL_MODIFIER 0x0400
标签:
原文地址:http://blog.csdn.net/itcastcpp/article/details/50771980