语音识别控制QQ(转自吴英强)

来源:互联网 发布:淘宝投诉凭证上传失败 编辑:程序博客网 时间:2024/04/27 20:34

语音识别控制QQ

QQ.cpp 单独测试控制qq的各种命令

[cpp] view plaincopy
  1. #include<Windows.h>  
  2.   
  3. void openqq()  
  4. {  
  5.     ShellExecuteA(0, "open""\"C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe\"", 0, 0, 1);  
  6. }  
  7.   
  8. void closeqq()  
  9. {  
  10.     system("taskkill /f /im QQ.exe");  
  11. }  
  12.   
  13. void show()  
  14. {  
  15.     HWND win = FindWindowA("TXGuiFoundation","QQ2013");  
  16.     if (win != NULL)  
  17.     {  
  18.         ShowWindow(win, SW_SHOW);  
  19.     }  
  20. }  
  21.   
  22. void  hide()  
  23. {  
  24.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  25.     if (win != NULL)  
  26.     {  
  27.         ShowWindow(win, SW_HIDE);  
  28.     }  
  29. }  
  30.   
  31.   
  32. void  shang()  
  33. {  
  34.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  35.     if (win != NULL)  
  36.     {  
  37.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  38.         GetWindowRect(win, &rectwind);//获取区域  
  39.         SetWindowPos(win, NULL, rectwind.left, rectwind.top-100, 300, 300, 1);  
  40.     }  
  41. }  
  42.   
  43. void xia()  
  44. {  
  45.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  46.     if (win != NULL)  
  47.     {  
  48.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  49.         GetWindowRect(win, &rectwind);//获取区域  
  50.         SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1);  
  51.     }  
  52. }  
  53.   
  54. void zuo()  
  55. {  
  56.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  57.     if (win != NULL)  
  58.     {  
  59.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  60.         GetWindowRect(win, &rectwind);//获取区域  
  61.         SetWindowPos(win, NULL, rectwind.left-100, rectwind.top , 300, 300, 1);  
  62.     }  
  63. }  
  64.   
  65. void you()  
  66. {  
  67.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  68.     if (win != NULL)  
  69.     {  
  70.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  71.         GetWindowRect(win, &rectwind);//获取区域  
  72.         SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//设置区域位置  
  73.     }  
  74. }  
  75.   
  76. void main()  
  77. {  
  78.     openqq();  
  79.     Sleep(3000);  
  80.     /*show(); 
  81.     Sleep(2000); 
  82.     hide();*/  
  83.     shang();  
  84.     Sleep(2000);  
  85.     shang();  
  86.   
  87.     Sleep(2000);  
  88.     //closeqq();  
  89. }  

speechrecognition.cpp 结合到语音控制qq移动

[cpp] view plaincopy
  1. #include <windows.h>  
  2. #include <atlstr.h>  
  3. #include <sphelper.h>  
  4. #include <sapi.h>  
  5. #include<comutil.h>  
  6. #include<string.h>  
  7.   
  8. #pragma comment(lib,"sapi.lib")  
  9. #pragma comment(lib, "comsupp.lib")   
  10.   
  11. #define GID_CMD_GR 333333  
  12. #define WM_RECOEVENT WM_USER+1  
  13.   
  14.  LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);  
  15.   
  16.  void openqq();  
  17.  void you();  
  18.  void zuo();  
  19.  void shang();  
  20.  void xia();  
  21.  void closeqq();  
  22.  void show();  
  23.  void hide();  
  24.   
  25.   
  26.   
  27.  char    szAppName[] = "TsinghuaYincheng";  
  28.  BOOL b_initSR;  
  29.  BOOL b_Cmd_Grammar;  
  30.  CComPtr<ISpRecoContext>m_cpRecoCtxt;  //语音识别程序接口  
  31.  CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法  
  32.  CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎  
  33.  int speak(wchar_t *str);  
  34.   
  35.  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)  
  36.  {  
  37.      HWND        hwnd;  
  38.      MSG         msg;  
  39.      WNDCLASS    wndclass;  
  40.   
  41.      wndclass.cbClsExtra          =0;  
  42.      wndclass.cbWndExtra          =0;  
  43.      wndclass.hbrBackground       =(HBRUSH)GetStockObject(WHITE_BRUSH);  
  44.      wndclass.hCursor             =LoadCursor(NULL,IDC_ARROW);  
  45.      wndclass.hIcon               =LoadIcon(NULL,IDI_APPLICATION);  
  46.      wndclass.hInstance           =hInstance;  
  47.      wndclass.lpfnWndProc         =WndProc;  
  48.      wndclass.lpszClassName       =szAppName;  
  49.      wndclass.lpszMenuName        =NULL;  
  50.      wndclass.style               =CS_HREDRAW|CS_VREDRAW;  
  51.   
  52.      if(!RegisterClass(&wndclass))  
  53.      {  
  54.          MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);  
  55.          return 0;  
  56.      }  
  57.      speak(L"谭胜是一个猥琐男");  
  58.   
  59.      hwnd=CreateWindow(szAppName,  
  60.                        TEXT("传智播客C/C++学院语音识别教程"),  
  61.                        WS_OVERLAPPEDWINDOW,  
  62.                        CW_USEDEFAULT,  
  63.                        CW_USEDEFAULT,  
  64.                        CW_USEDEFAULT,  
  65.                        CW_USEDEFAULT,  
  66.                        NULL,  
  67.                        NULL,  
  68.                        hInstance,  
  69.                        NULL);  
  70.   
  71.      ShowWindow(hwnd,iCmdShow);  
  72.      UpdateWindow(hwnd);  
  73.        
  74.      while(GetMessage(&msg,NULL,0,0))  
  75.      {  
  76.          TranslateMessage(&msg);  
  77.          DispatchMessage(&msg);  
  78.      }  
  79.       
  80.      return msg.wParam;  
  81.  }  
  82.   
  83.  LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)  
  84.  {  
  85.      HDC           hdc;  
  86.      PAINTSTRUCT   ps;  
  87.   
  88.      switch(message)  
  89.      {  
  90.      case WM_CREATE:  
  91.          {  
  92.              //初始化COM端口  
  93.              ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);  
  94.              //创建识别引擎COM实例为共享型  
  95.              HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);  
  96.              //创建识别上下文接口  
  97.              if(SUCCEEDED(hr))  
  98.              {  
  99.                  hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);  
  100.              }  
  101.              else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);  
  102.              //设置识别消息,使计算机时刻监听语音消息  
  103.              if(SUCCEEDED(hr))  
  104.              {  
  105.                  hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);  
  106.              }  
  107.              else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);  
  108.              //设置我们感兴趣的事件  
  109.              if(SUCCEEDED(hr))  
  110.              {  
  111.                  ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);  
  112.                  hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);  
  113.              }  
  114.              else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);  
  115.              //创建语法规则  
  116.              b_Cmd_Grammar=TRUE;  
  117.              if(FAILED(hr))  
  118.              {  
  119.                  MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);  
  120.              }  
  121.              hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);  
  122.              WCHAR wszXMLFile[20]=L"er.xml";  
  123.              MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);  
  124.              hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);  
  125.              if(FAILED(hr))  
  126.              {  
  127.                  MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);  
  128.              }  
  129.              b_initSR=TRUE;  
  130.              //在开始识别时,激活语法进行识别  
  131.              hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);  
  132.              return 0;  
  133.          }  
  134.      case WM_RECOEVENT:  
  135.          {  
  136.              RECT rect;  
  137.              GetClientRect(hwnd,&rect);  
  138.              hdc=GetDC(hwnd);  
  139.              USES_CONVERSION;  
  140.              CSpEvent event;  
  141.              while(event.GetFrom(m_cpRecoCtxt)==S_OK)  
  142.              {  
  143.                  switch(event.eEventId)  
  144.                  {  
  145.                  case SPEI_RECOGNITION:  
  146.                      {  
  147.                          static const WCHAR wszUnrecognized[]=L"<Unrecognized>";  
  148.                          CSpDynamicString dstrText;  
  149.                          //取得识别结果  
  150.                          if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))  
  151.                          {  
  152.                              dstrText=wszUnrecognized;  
  153.                          }  
  154.                          BSTR SRout;  
  155.                          dstrText.CopyToBSTR(&SRout);  
  156.                          char* lpszText2 = _com_util::ConvertBSTRToString(SRout);  
  157.   
  158.                          if(b_Cmd_Grammar)  
  159.                          {  
  160.                              //绘制文本  
  161.                              DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);                                                    
  162.                              if (strcmp("我是学霸",lpszText2)==0)  
  163.                              {      
  164.                                 MessageBoxA(0, lpszText2, lpszText2, 0);  
  165.                              }  
  166.                              if (strcmp("清华土匪", lpszText2) == 0)  
  167.                              {  
  168.                                  MessageBoxA(0, lpszText2, lpszText2, 0);  
  169.                              }  
  170.                              if (strcmp("传智播客", lpszText2) == 0)  
  171.                              {  
  172.                                  MessageBoxA(0, lpszText2, lpszText2, 0);  
  173.                              }  
  174.                              if (strcmp("天下无双", lpszText2) == 0)  
  175.                              {  
  176.                                  wchar_t * str = L"谭胜大爷吹牛皮的功夫天下无双";  
  177.                                  speak(str);  
  178.                              }  
  179.                              if (strcmp("给我上", lpszText2) == 0)  
  180.                              {  
  181.                                  speak(L"亲爱的主人,好");  
  182.                                  shang();  
  183.                              }  
  184.                              if (strcmp("给我下", lpszText2) == 0)  
  185.                              {  
  186.                                  speak(L"亲爱的主人,好");  
  187.                                  xia();  
  188.                              }  
  189.                              if (strcmp("给我左", lpszText2) == 0)  
  190.                              {  
  191.                                  speak(L"亲爱的主人,好");  
  192.                                  zuo();  
  193.                              }  
  194.                              if (strcmp("给我右", lpszText2) == 0)  
  195.                              {  
  196.                                  speak(L"亲爱的主人,好");  
  197.                                  you();  
  198.                              }  
  199.                              if (strcmp("打开企鹅", lpszText2) == 0)  
  200.                              {  
  201.                                  speak(L"亲爱的主人,好");  
  202.                                  openqq();  
  203.                              }  
  204.                              if (strcmp("关闭企鹅", lpszText2) == 0)  
  205.                              {  
  206.                                  speak(L"亲爱的主人,好");  
  207.                                  closeqq();  
  208.                              }  
  209.                              if (strcmp("显示", lpszText2) == 0)  
  210.                              {  
  211.                                  speak(L"亲爱的主人,好");  
  212.                                  show();  
  213.                              }  
  214.                              if (strcmp("隐藏", lpszText2) == 0)  
  215.                              {  
  216.                                  speak(L"亲爱的主人,好");  
  217.                                  hide();  
  218.                              }  
  219.                              if (strcmp("谭胜是谁", lpszText2) == 0)  
  220.                              {  
  221.                                  speak(L"谭胜是横空出世的英雄,百年不遇的天才");  
  222.                              }  
  223.                              if (strcmp("你是谁", lpszText2) == 0)  
  224.                              {  
  225.                                  speak(L"我是你们勇猛勤奋彪悍的谭胜大哥写的语音识别程序");  
  226.                              }  
  227.   
  228.                              if (strcmp("你是笨蛋", lpszText2) == 0)  
  229.                              {  
  230.                                  speak(L"我的创造者谭胜大哥聪明的惊天地泣鬼神");  
  231.                              }  
  232.                              if (strcmp("你是蠢猪", lpszText2) == 0)  
  233.                              {  
  234.                                  speak(L"我固然很蠢,但是我的创造者谭胜大哥聪明的惊天地泣鬼神");  
  235.                              }  
  236.                              if (strcmp("你很好", lpszText2) == 0)  
  237.                              {  
  238.                                  speak(L"我一直很牛逼,多亏谭大爷");  
  239.                              }  
  240.                              if (strcmp("中秋快乐", lpszText2) == 0)  
  241.                              {  
  242.                                  speak(L"谭大爷叫你回家吃月饼");  
  243.                              }  
  244.   
  245.                           
  246.                          }      
  247.                      }  
  248.                  }  
  249.              }  
  250.              return TRUE;  
  251.          }  
  252.      case WM_PAINT:  
  253.          hdc=BeginPaint(hwnd,&ps);  
  254.          EndPaint(hwnd,&ps);  
  255.          return 0;  
  256.      case WM_DESTROY:  
  257.          PostQuitMessage(0);  
  258.          return 0;  
  259.      }  
  260.      return DefWindowProc(hwnd,message,wParam,lParam);  
  261.  }  
  262.   
  263. #pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll     
  264. int speak(wchar_t *str)  
  265. {  
  266.      ISpVoice * pVoice = NULL;  
  267.      ::CoInitialize(NULL);  
  268.      //获取ISpVoice接口:     
  269.      long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);  
  270.      hr = pVoice->Speak(str, 0, NULL);  
  271.      pVoice->Release();  
  272.      pVoice = NULL;  
  273.      //千万不要忘记:     
  274.      ::CoUninitialize();  
  275.      return TRUE;  
  276.  }  
  277.   
  278.   
  279.   
  280.   
  281.   
  282. void openqq()  
  283. {  
  284.     ShellExecuteA(0, "open""\"C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe\"", 0, 0, 1);  
  285. }  
  286.   
  287. void closeqq()  
  288. {  
  289.     system("taskkill /f /im QQ.exe");  
  290. }  
  291.   
  292. void show()  
  293. {  
  294.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  295.     if (win != NULL)  
  296.     {  
  297.         ShowWindow(win, SW_SHOW);  
  298.     }  
  299. }  
  300.   
  301. void  hide()  
  302. {  
  303.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  304.     if (win != NULL)  
  305.     {  
  306.         ShowWindow(win, SW_HIDE);  
  307.     }  
  308. }  
  309.   
  310.   
  311. void  shang()  
  312. {  
  313.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  314.     if (win != NULL)  
  315.     {  
  316.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  317.         GetWindowRect(win, &rectwind);//获取区域  
  318.         SetWindowPos(win, NULL, rectwind.left, rectwind.top - 100, 300, 300, 1);  
  319.     }  
  320. }  
  321.   
  322. void xia()  
  323. {  
  324.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  325.     if (win != NULL)  
  326.     {  
  327.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  328.         GetWindowRect(win, &rectwind);//获取区域  
  329.         SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1);  
  330.     }  
  331. }  
  332.   
  333. void zuo()  
  334. {  
  335.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  336.     if (win != NULL)  
  337.     {  
  338.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  339.         GetWindowRect(win, &rectwind);//获取区域  
  340.         SetWindowPos(win, NULL, rectwind.left - 100, rectwind.top, 300, 300, 1);  
  341.     }  
  342. }  
  343.   
  344. void you()  
  345. {  
  346.     HWND win = FindWindowA("TXGuiFoundation""QQ2013");  
  347.     if (win != NULL)  
  348.     {  
  349.         RECT rectwind;//区域,lefr ,right ,top,bottom  
  350.         GetWindowRect(win, &rectwind);//获取区域  
  351.         SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//设置区域位置  
  352.     }  
  353. }  

er.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GRAMMAR LANGID="804">  
  3.   <DEFINE>  
  4.     <ID NAME="CMD" VAL="10"/>  
  5.     </DEFINE>  
  6.   <RULE NAME="COMMAND" ID="CMD" TOPLEVEL="ACTIVE">  
  7.     <L>  
  8.       <P>我是学霸</P>  
  9.       <P>清华土匪</P>  
  10.       <P>传智播客</P>  
  11.       <P>天下无双</P>  
  12.       <P>给我上</P>我  
  13.       <P>给我下</P>  
  14.       <P>给我左</P>  
  15.       <P>给我右</P>  
  16.       <P>打开企鹅</P>  
  17. http://yyk.familydoctor.com.cn/20538/Content_3388.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3467.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3466.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3465.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3446.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3390.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3389.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3387.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3386.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3385.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3384.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3383.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3382.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3381.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3070.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3069.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3067.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3066.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3061.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3055.html      <P>关闭企鹅</P>  
  18.       <P>显示</P>  
  19.       <P>隐藏</P>  
  20.       <P>谭胜是谁</P>  
  21.       <P>你是谁</P>  
  22.       <P>你是笨蛋</P>  
  23.       <P>你是蠢猪</P>  
  24.       <P>你很好</P>  
  25.       <P>中秋快乐</P>  
  26.       </L>  
  27.     </RULE>  
  28.   </GRAMMAR>  

语音控制游戏

[cpp] view plaincopy
  1. #include <windows.h>  
  2. #include <atlstr.h>  
  3. #include <sphelper.h>  
  4. #include <sapi.h>  
  5. #include<comutil.h>  
  6. #include<string.h>  
  7.   
  8.   
  9. #pragma comment(lib,"sapi.lib")  
  10. #pragma comment(lib, "comsupp.lib")   
  11.   
  12. #define GID_CMD_GR 333333  
  13. #define WM_RECOEVENT WM_USER+1  
  14.   
  15.  LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);  
  16.   
  17.  char    szAppName[] = "TsinghuaYincheng";  
  18.  BOOL b_initSR;  
  19.  BOOL b_Cmd_Grammar;  
  20.  CComPtr<ISpRecoContext>m_cpRecoCtxt;  //语音识别程序接口  
  21.  CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法  
  22.  CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎  
  23.  int speak(wchar_t *str);  
  24.   
  25.  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)  
  26.  {  
  27.      HWND        hwnd;  
  28.      MSG         msg;  
  29.      WNDCLASS    wndclass;  
  30.   
  31.      wndclass.cbClsExtra          =0;  
  32.      wndclass.cbWndExtra          =0;  
  33.      wndclass.hbrBackground       =(HBRUSH)GetStockObject(WHITE_BRUSH);  
  34.      wndclass.hCursor             =LoadCursor(NULL,IDC_ARROW);  
  35.      wndclass.hIcon               =LoadIcon(NULL,IDI_APPLICATION);  
  36.      wndclass.hInstance           =hInstance;  
  37.      wndclass.lpfnWndProc         =WndProc;  
  38.      wndclass.lpszClassName       =szAppName;  
  39.      wndclass.lpszMenuName        =NULL;  
  40.      wndclass.style               =CS_HREDRAW|CS_VREDRAW;  
  41.   
  42.      if(!RegisterClass(&wndclass))  
  43.      {  
  44.          MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);  
  45.          return 0;  
  46. http://yyk.familydoctor.com.cn/20538/Content_3538.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3542.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3482.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3541.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3540.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3539.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3512.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3511.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3510.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3509.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3508.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3475.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3476.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3473.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3472.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3471.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3469.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3477.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3478.htmlhttp://yyk.familydoctor.com.cn/20538/Content_3479.html     }  
  47.      speak(L"谭胜是一个猥琐男");  
  48.   
  49.      hwnd=CreateWindow(szAppName,  
  50.                        TEXT("传智播客C/C++学院语音识别教程"),  
  51.                        WS_OVERLAPPEDWINDOW,  
  52.                        CW_USEDEFAULT,  
  53.                        CW_USEDEFAULT,  
  54.                        CW_USEDEFAULT,  
  55.                        CW_USEDEFAULT,  
  56.                        NULL,  
  57.                        NULL,  
  58.                        hInstance,  
  59.                        NULL);  
  60.   
  61.      ShowWindow(hwnd,iCmdShow);  
  62.      UpdateWindow(hwnd);  
  63.        
  64.      while(GetMessage(&msg,NULL,0,0))  
  65.      {  
  66.          TranslateMessage(&msg);  
  67.          DispatchMessage(&msg);  
  68.      }  
  69.       
  70.      return msg.wParam;  
  71.  }  
  72.   
  73.  LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)  
  74.  {  
  75.      HDC           hdc;  
  76.      PAINTSTRUCT   ps;  
  77.   
  78.      switch(message)  
  79.      {  
  80.      case WM_CREATE:  
  81.          {  
  82.              //初始化COM端口  
  83.              ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);  
  84.              //创建识别引擎COM实例为共享型  
  85.              HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);  
  86.              //创建识别上下文接口  
  87.              if(SUCCEEDED(hr))  
  88.              {  
  89.                  hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);  
  90.              }  
  91.              else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);  
  92.              //设置识别消息,使计算机时刻监听语音消息  
  93.              if(SUCCEEDED(hr))  
  94.              {  
  95.                  hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);  
  96.              }  
  97.              else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);  
  98.              //设置我们感兴趣的事件  
  99.              if(SUCCEEDED(hr))  
  100.              {  
  101.                  ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);  
  102.                  hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);  
  103.              }  
  104.              else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);  
  105.              //创建语法规则  
  106.              b_Cmd_Grammar=TRUE;  
  107.              if(FAILED(hr))  
  108.              {  
  109.                  MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);  
  110.              }  
  111.              hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);  
  112.              WCHAR wszXMLFile[20]=L"er.xml";  
  113.              MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);  
  114.              hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);  
  115.              if(FAILED(hr))  
  116.              {  
  117.                  MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);  
  118.              }  
  119.              b_initSR=TRUE;  
  120.              //在开始识别时,激活语法进行识别  
  121.              hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);  
  122.              return 0;  
  123.          }  
  124.      case WM_RECOEVENT:  
  125.          {  
  126.              RECT rect;  
  127.              GetClientRect(hwnd,&rect);  
  128.              hdc=GetDC(hwnd);  
  129.              USES_CONVERSION;  
  130.              CSpEvent event;  
  131.              while(event.GetFrom(m_cpRecoCtxt)==S_OK)  
  132.              {  
  133.                  switch(event.eEventId)  
  134.                  {  
  135.                  case SPEI_RECOGNITION:  
  136.                      {  
  137.                          static const WCHAR wszUnrecognized[]=L"<Unrecognized>";  
  138.                          CSpDynamicString dstrText;  
  139.                          //取得识别结果  
  140.                          if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))  
  141.                          {  
  142.                              dstrText=wszUnrecognized;  
  143.                          }  
  144.                          BSTR SRout;  
  145.                          dstrText.CopyToBSTR(&SRout);  
  146.                          char* lpszText2 = _com_util::ConvertBSTRToString(SRout);  
  147.   
  148.                          if(b_Cmd_Grammar)  
  149.                          {  
  150.                              DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);  
  151.                              if (strcmp("旋风刀",lpszText2)==0)  
  152.                              {      
  153.                                  keybd_event('A', 0, 0, 0);//按下  
  154.                                  keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起  
  155.                                  Sleep(10);  
  156.                                  keybd_event('D', 0, 0, 0);//按下  
  157.                                  keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//弹起  
  158. http://yyk.familydoctor.com.cn/20538/Content_3060.html
    http://yyk.familydoctor.com.cn/20538/Content_3059.html
    http://yyk.familydoctor.com.cn/20538/Content_3058.html
    http://yyk.familydoctor.com.cn/20538/Content_3056.html
    http://yyk.familydoctor.com.cn/20538/Content_3002.html
    http://yyk.familydoctor.com.cn/20538/Content_3039.html
    http://yyk.familydoctor.com.cn/20538/Content_3042.html
    http://yyk.familydoctor.com.cn/20538/Content_3043.html
    http://yyk.familydoctor.com.cn/20538/Content_3046.html
    http://yyk.familydoctor.com.cn/20538/Content_3048.html
    http://yyk.familydoctor.com.cn/20538/Content_3049.html
    http://yyk.familydoctor.com.cn/20538/Content_3051.html
    http://yyk.familydoctor.com.cn/20538/Content_3053.html
    http://yyk.familydoctor.com.cn/20538/Content_3054.html
    http://yyk.familydoctor.com.cn/20538/Content_2997.html
    http://yyk.familydoctor.com.cn/20538/Content_3001.html
    http://yyk.familydoctor.com.cn/20538/Content_3000.html
    http://yyk.familydoctor.com.cn/20538/Content_2995.html
    http://yyk.familydoctor.com.cn/20538/Content_2994.html
    http://yyk.familydoctor.com.cn/20538/Content_2993.html                                 Sleep(10);  
  159.                                  keybd_event('W', 0, 0, 0);//按下  
  160.                                  keybd_event('W', 0, KEYEVENTF_KEYUP, 0);//弹起  
  161.                                  Sleep(10);  
  162.                                  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);  
  163.                                  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);  
  164.   
  165.                                   
  166.                              }  
  167.                              if (strcmp("大风吹", lpszText2) == 0)  
  168.                              {  
  169.                                  //sww l  
  170.                                  keybd_event('A', 0, 0, 0);//按下  
  171.                                  keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起  
  172.                                  Sleep(10);  
  173.                                  keybd_event('D', 0, 0, 0);//按下  
  174.                                  keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//弹起  
  175.                                  Sleep(10);  
  176.                                  keybd_event('S', 0, 0, 0);//按下  
  177.                                  keybd_event('S', 0, KEYEVENTF_KEYUP, 0);//弹起  
  178.                                  Sleep(10);  
  179.                                  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);  
  180.                                  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);  
  181.                                    
  182.                              }  
  183.                              if (strcmp("充气", lpszText2) == 0)  
  184.                              {  
  185.                                  //   \ang  
  186.                                  keybd_event(VK_OEM_102, 0, 0, 0);//按下  
  187.                                  keybd_event(VK_OEM_102, 0, KEYEVENTF_KEYUP, 0);//弹起  
  188.                                  Sleep(10);  
  189.                                  keybd_event('A', 0, 0, 0);//按下  
  190.                                  keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//弹起  
  191.                                  Sleep(10);  
  192.                                  keybd_event('N', 0, 0, 0);//按下  
  193.                                  keybd_event('N', 0, KEYEVENTF_KEYUP, 0);//弹起  
  194.                                  Sleep(10);  
  195.                                  keybd_event('G', 0, 0, 0);//按下6igrk;)移植  
  196.                                  keybd_event('G', 0, KEYEVENTF_KEYUP, 0);//弹起  
  197.                                  Sleep(10);  
  198.                                  keybd_event(VK_RETURN, 0, 0, 0);//按下6igrk;)移植  
  199.                                  keybd_event(VK_RETURN,  0, KEYEVENTF_KEYUP, 0);//弹起  
  200.   
  201.                               
  202.                              }  
  203.                              if (strcmp("跳跃", lpszText2) == 0)  
  204.                              {  
  205.   
  206.                                  keybd_event(VK_SPACE, 0, 0, 0);//按下  
  207.                                  keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0);//弹起  
  208.   
  209.   
  210.                              }  
  211.                              if (strcmp("趴下", lpszText2) == 0)  
  212.                              {  
  213.   
  214.   
  215.                              }  
  216.                              if (strcmp("前进", lpszText2) == 0)  
  217.                              {  
  218.   
  219.   
  220.                              }  
  221.                              if (strcmp("后退", lpszText2) == 0)  
  222.                              {  
  223.   
  224.   
  225.                              }  
  226.                           
  227.                          }      
  228.                      }  
  229.                  }  
  230.              }  
  231.              return TRUE;  
  232.          }  
  233.      case WM_PAINT:  
  234.          hdc=BeginPaint(hwnd,&ps);  
  235.          EndPaint(hwnd,&ps);  
  236.          return 0;  
  237.      case WM_DESTROY:  
  238.          PostQuitMessage(0);  
  239.          return 0;  
  240.      }  
  241.      return DefWindowProc(hwnd,message,wParam,lParam);  
  242.  }  
  243.   
  244. #pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll     
  245. int speak(wchar_t *str)  
  246. {  
  247.      ISpVoice * pVoice = NULL;  
  248.      ::CoInitialize(NULL);  
  249.      //获取ISpVoice接口:     
  250.      long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);  
  251.      hr = pVoice->Speak(str, 0, NULL);  
  252.      pVoice->Release();  
  253.      pVoice = NULL;  
  254.      //千万不要忘记:     
  255.      ::CoUninitialize();  
  256.      return TRUE;  
  257.  }  

er.xml与cpp文件放在同一目录下

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GRAMMAR LANGID="804">  
  3.   <DEFINE>  
  4.     <ID NAME="CMD" VAL="10"/>  
  5.     </DEFINE>  
  6.   <RULE NAME="COMMAND" ID="CMD" TOPLEVEL="ACTIVE">  
  7.     <L>  
  8.       <P>旋风刀</P>  
  9.       <P>大风吹</P>  
  10.       <P>充气</P>  
  11.       <P>跳跃</P>  
  12.       <P>趴下</P>  
  13.       <P>前进</P>  
  14.       <P>后退</P>  
  15.       </L>  
  16.     </RULE>  
  17.   </GRAMMAR>  

0 0
原创粉丝点击