20160209.CCPP體系詳解(0019天)
程式片段(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
相關文章
- 20160217.CCPP體系詳解(0027天)
- 20160124.CCPP詳解體系(0003天)
- 20160125.CCPP詳解體系(0004天)
- 20160126.CCPP體系詳解(0005天)
- 20160127.CCPP體系詳解(0006天)
- 20160130.CCPP體系詳解(0009天)
- 20160203.CCPP體系詳解(0013天)
- 20160211.CCPP體系詳解(0021天)
- 20160213.CCPP體系詳解(0023天)
- 20160214.CCPP體系詳解(0024天)
- 20160215.CCPP體系詳解(0025天)
- 20160224.CCPP體系詳解(0034天)
- 20160218.CCPP體系詳解(0028天)
- 20160219.CCPP體系詳解(0029天)
- 手遊《天地劫》的三天體驗——深度系統剖析及玩法詳解
- 20160122.CCPP詳解體系(0001天)
- 20160123.CCPP詳解體系(0002天)
- 20160128.CCPP體系詳解(0007天)
- 20160129.CCPP體系詳解(0008天)
- 20160131.CCPP體系詳解(0010天)
- 20160204.CCPP體系詳解(0014天)
- 20160205.CCPP體系詳解(0015天)
- 20160210.CCPP體系詳解(0020天)
- 20160212.CCPP體系詳解(0022天)
- 20160207.CCPP體系詳解(0017天)
- 20160225.CCPP體系詳解(0035天)
- 20160226.CCPP體系詳解(0036天)
- 20160227.CCPP體系詳解(0037天)
- 20160222.CCPP體系詳解(0032天)
- 20160221.CCPP體系詳解(0031天)
- 20160201.CCPP體系詳解(0011天)
- 20160202.CCPP體系詳解(0012天)
- 20160216.CCPP體系詳解(0026天)
- 20160206.CCPP體系詳解(0016天)
- 20160208.CCPP體系詳解(0018天)
- 20160223.CCPP體系詳解(0033天)
- 20160220.CCPP體系詳解(0030天)
- MySQL體系結構詳解MySql