此應用程式示範如何建置簡單的 手寫 辨識應用程式。
此程式會建立 InkCollector 對象來 筆跡-enable the window and a default recognizer context 物件。 收到從應用程式功能表引發的 “Recognize!” 命令時,收集的筆墨筆劃會傳遞至辨識器內容。 最佳結果字串會顯示在消息框中。
建立 RecognizerContext 物件
在應用程式的 WndProc 程式中,當啟動時收到WM_CREATE訊息時,會建立使用預設辨識器的新辨識器內容。 此內容用於應用程式中的所有辨識。
case WM_CREATE:
{
HRESULT hr;
hr = CoCreateInstance(CLSID_InkRecognizerContext,
NULL, CLSCTX_INPROC_SERVER, IID_IInkRecognizerContext,
(void **) &g_pIInkRecoContext);
if (FAILED(hr))
{
::MessageBox(NULL, TEXT("There are no handwriting recognizers installed.\n"
"You need to have at least one in order to run this sample.\nExiting."),
gc_szAppName, MB_ICONERROR);
return -1;
}
//...
辨識筆劃
當使用者按兩下 Recognize 時,會收到辨識命令! 選單項目。 程式碼會從 InkDisp 物件取得 InkStrokes InkStrokes (pIInkStrokes) 的指標,然後透過呼叫 putref_Strokes 將 InkStrokes 傳遞至辨識器上下文。
case WM_COMMAND:
//...
else if (wParam == ID_RECOGNIZE)
{
// change cursor to the system's Hourglass
HCURSOR hCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
// Get a pointer to the ink stroke collection
// This collection is a snapshot of the entire ink object
IInkStrokes* pIInkStrokes = NULL;
HRESULT hr = g_pIInkDisp->get_Strokes(&pIInkStrokes);
if (SUCCEEDED(hr))
{
// Pass the stroke collection to the recognizer context
hr = g_pIInkRecoContext->putref_Strokes(pIInkStrokes);
if (SUCCEEDED(hr))
{
然後,程式代碼會呼叫 InkRecognizerContext 對象的 Recognize 方法,並傳入 IInkRecognitionResult物件的指標來保存結果。
// Recognize
IInkRecognitionResult* pIInkRecoResult = NULL;
hr = g_pIInkRecoContext->Recognize(&pIInkRecoResult);
if (SUCCEEDED(hr))
{
最後,程式代碼會使用 IInkRecognitionResult 物件的 TopString 屬性,將最高辨識結果擷取到字元串變數中,釋放 IInkRecognitionResult 物件,並在消息框中顯示字串。
// Get the best result of the recognition
BSTR bstrBestResult = NULL;
hr = pIInkRecoResult->get_TopString(&bstrBestResult);
pIInkRecoResult->Release();
pIInkRecoResult = NULL;
// Show the result string
if (SUCCEEDED(hr) && bstrBestResult)
{
MessageBoxW(hwnd, bstrBestResult,
L"Recognition Results", MB_OK);
SysFreeString(bstrBestResult);
} }
請務必在使用過程中重置辨識器的上下文。
// Reset the recognizer context
g_pIInkRecoContext->putref_Strokes(NULL);
}
pIInkStrokes->Release();
}
// restore the cursor
::SetCursor(hCursor);
}