このアプリケーションは、簡単な 手書き 認識アプリケーションを構築する方法を示しています。
このプログラムでは、InkCollector オブジェクトを作成して、ウィンドウと既定の認識エンジン コンテキスト オブジェクトをインク有効にします。 アプリケーションのメニューから "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! をクリックすると、recognize コマンドが受信されます。 アクセスできます。 このコードは、InkDisp オブジェクトからインク 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);
}