QQ.cpp 單獨測試控制qq的各種命令
#includevoid openqq() { ShellExecuteA(0, open, C:\Program Files (x86)\Tencent\QQ\QQProtect\Bin\QQProtect.exe, 0, 0, 1); } void closeqq() { system(taskkill /f /im QQ.exe); } void show() { HWND win = FindWindowA(TXGuiFoundation,QQ2013); if (win != NULL) { ShowWindow(win, SW_SHOW); } } void hide() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { ShowWindow(win, SW_HIDE); } } void shang() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left, rectwind.top-100, 300, 300, 1); } } void xia() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1); } } void zuo() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left-100, rectwind.top , 300, 300, 1); } } void you() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//設置區域位置 } } void main() { openqq(); Sleep(3000); /*show(); Sleep(2000); hide();*/ shang(); Sleep(2000); shang(); Sleep(2000); //closeqq(); }
speechrecognition.cpp 結合到語音控制qq移動
#include#include #include #include #include #include #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); void openqq(); void you(); void zuo(); void shang(); void xia(); void closeqq(); void show(); void hide(); char szAppName[] = TsinghuaYincheng; BOOL b_initSR; BOOL b_Cmd_Grammar; CComPtr m_cpRecoCtxt; //語音識別程序接口 CComPtr m_cpCmdGramma; //識別語法 CComPtr 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譚勝是一個猥瑣男); hwnd=CreateWindow(szAppName, TEXT(傳智播客C/C++學院語音識別教程), 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]=Ler.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 ; 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) { //繪制文本 DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); if (strcmp(我是學霸,lpszText2)==0) { MessageBoxA(0, lpszText2, lpszText2, 0); } if (strcmp(清華土匪, lpszText2) == 0) { MessageBoxA(0, lpszText2, lpszText2, 0); } if (strcmp(傳智播客, lpszText2) == 0) { MessageBoxA(0, lpszText2, lpszText2, 0); } if (strcmp(天下無雙, lpszText2) == 0) { wchar_t * str = L譚勝大爺吹牛皮的功夫天下無雙; speak(str); } if (strcmp(給我上, lpszText2) == 0) { speak(L親愛的主人,好); shang(); } if (strcmp(給我下, lpszText2) == 0) { speak(L親愛的主人,好); xia(); } if (strcmp(給我左, lpszText2) == 0) { speak(L親愛的主人,好); zuo(); } if (strcmp(給我右, lpszText2) == 0) { speak(L親愛的主人,好); you(); } if (strcmp(打開企鵝, lpszText2) == 0) { speak(L親愛的主人,好); openqq(); } if (strcmp(關閉企鵝, lpszText2) == 0) { speak(L親愛的主人,好); closeqq(); } if (strcmp(顯示, lpszText2) == 0) { speak(L親愛的主人,好); show(); } if (strcmp(隱藏, lpszText2) == 0) { speak(L親愛的主人,好); hide(); } if (strcmp(譚勝是誰, lpszText2) == 0) { speak(L譚勝是橫空出世的英雄,百年不遇的天才); } if (strcmp(你是誰, lpszText2) == 0) { speak(L我是你們勇猛勤奮彪悍的譚勝大哥寫的語音識別程序); } if (strcmp(你是笨蛋, lpszText2) == 0) { speak(L我的創造者譚勝大哥聰明的驚天地泣鬼神); } if (strcmp(你是蠢豬, lpszText2) == 0) { speak(L我固然很蠢,但是我的創造者譚勝大哥聰明的驚天地泣鬼神); } if (strcmp(你很好, lpszText2) == 0) { speak(L我一直很牛逼,多虧譚大爺); } if (strcmp(中秋快樂, lpszText2) == 0) { speak(L譚大爺叫你回家吃月餅); } } } } } 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; } void openqq() { ShellExecuteA(0, open, C:\Program Files (x86)\Tencent\QQ\QQProtect\Bin\QQProtect.exe, 0, 0, 1); } void closeqq() { system(taskkill /f /im QQ.exe); } void show() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { ShowWindow(win, SW_SHOW); } } void hide() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { ShowWindow(win, SW_HIDE); } } void shang() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left, rectwind.top - 100, 300, 300, 1); } } void xia() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left, rectwind.top + 100, 300, 300, 1); } } void zuo() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left - 100, rectwind.top, 300, 300, 1); } } void you() { HWND win = FindWindowA(TXGuiFoundation, QQ2013); if (win != NULL) { RECT rectwind;//區域,lefr ,right ,top,bottom GetWindowRect(win, &rectwind);//獲取區域 SetWindowPos(win, NULL, rectwind.left + 100, rectwind.top, 300, 300, 1);//設置區域位置 } }
er.xml
我是學霸
清華土匪
傳智播客
天下無雙
給我上
我給我下
給我左
給我右
打開企鵝
關閉企鵝
顯示
隱藏
譚勝是誰
你是誰
你是笨蛋
你是蠢豬
你很好
中秋快樂
#include#include #include #include #include #include #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 m_cpRecoCtxt; //語音識別程序接口 CComPtr m_cpCmdGramma; //識別語法 CComPtr 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譚勝是一個猥瑣男); hwnd=CreateWindow(szAppName, TEXT(傳智播客C/C++學院語音識別教程), 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]=Ler.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 ; 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) { DrawText(hdc, TEXT(lpszText2), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); if (strcmp(旋風刀,lpszText2)==0) { keybd_event('A', 0, 0, 0);//按下 keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('D', 0, 0, 0);//按下 keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('W', 0, 0, 0);//按下 keybd_event('W', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } if (strcmp(大風吹, lpszText2) == 0) { //sww l keybd_event('A', 0, 0, 0);//按下 keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('D', 0, 0, 0);//按下 keybd_event('D', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('S', 0, 0, 0);//按下 keybd_event('S', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } if (strcmp(充氣, lpszText2) == 0) { // ang keybd_event(VK_OEM_102, 0, 0, 0);//按下 keybd_event(VK_OEM_102, 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('A', 0, 0, 0);//按下 keybd_event('A', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('N', 0, 0, 0);//按下 keybd_event('N', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event('G', 0, 0, 0);//按下6igrk;)移植 keybd_event('G', 0, KEYEVENTF_KEYUP, 0);//彈起 Sleep(10); keybd_event(VK_RETURN, 0, 0, 0);//按下6igrk;)移植 keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);//彈起 } if (strcmp(跳躍, lpszText2) == 0) { keybd_event(VK_SPACE, 0, 0, 0);//按下 keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0);//彈起 } if (strcmp(趴下, lpszText2) == 0) { } if (strcmp(前進, lpszText2) == 0) { } if (strcmp(後退, lpszText2) == 0) { } } } } } 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; }
er.xml與cpp文件放在同一目錄下
旋風刀
大風吹
充氣
跳躍
趴下
前進
後退