天天看點

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

搭建好Visual C++2010與windows SDK fo windows7的開發平台以後,

小試牛刀,檢驗下開發windows7的下的tabletpc應用,

代碼注釋如下

#include <windows.h> #include <comdef.h> #include <msinkaut.h> #include <msinkaut_i.c> //包含windows SDK for windows7的核心tabletpc頭檔案 #include "resource.h" const TCHAR* gc_szAppName = TEXT("Basic Recognition"); // 建立軌迹指針,識别指針,軌迹畫闆指針 IInkCollector * g_pIInkCollector = NULL; IInkDisp * g_pIInkDisp = NULL; IInkRecognizerContext * g_pIInkRecoContext = NULL; / // //清除Com指針 / void CleanUp() // Release all objects { if (g_pIInkRecoContext != NULL) { g_pIInkRecoContext->Release(); g_pIInkRecoContext = NULL; } if (g_pIInkDisp != NULL) { g_pIInkDisp->Release(); g_pIInkDisp = NULL; } if (g_pIInkCollector != NULL) { g_pIInkCollector->Release(); g_pIInkCollector = NULL; } } windows視窗消息循環 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: { //視窗消息建立,注冊視窗,并建立com指針接口 HRESULT hr; hr = CoCreateInstance(CLSID_InkRecognizerContext, NULL, CLSCTX_INPROC_SERVER, IID_IInkRecognizerContext, (void **) &g_pIInkRecoContext); if (FAILED(hr)) { return -1; } hr = CoCreateInstance(CLSID_InkCollector, NULL, CLSCTX_INPROC_SERVER, IID_IInkCollector, (void **) &g_pIInkCollector); if (FAILED(hr)) return -1; hr = g_pIInkCollector->get_Ink(&g_pIInkDisp); if (FAILED(hr)) return -1; hr = g_pIInkCollector->put_hWnd((long)hwnd); if (FAILED(hr)) return -1; hr = g_pIInkCollector->put_Enabled(VARIANT_TRUE); if (FAILED(hr)) return -1; break; } case WM_DESTROY: //退出消息循環,結束程式 PostQuitMessage(0); break; case WM_COMMAND: //清除軌迹 if (wParam == ID_CLEAR) { g_pIInkDisp->DeleteStrokes(0); InvalidateRect(hwnd, NULL, TRUE); } else if (wParam == ID_RECOGNIZE) { //擷取識别結果,并顯示最優化識别結果 HCURSOR hCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT)); //擷取軌迹指針用于識别 IInkStrokes* pIInkStrokes = NULL; HRESULT hr = g_pIInkDisp->get_Strokes(&pIInkStrokes); if (SUCCEEDED(hr)) { hr = g_pIInkRecoContext->putref_Strokes(pIInkStrokes); if (SUCCEEDED(hr)) { //利用識别指針擷取識别結果 IInkRecognitionResult* pIInkRecoResult = NULL; InkRecognitionStatus RecognitionStatus; hr = g_pIInkRecoContext->Recognize(&RecognitionStatus, &pIInkRecoResult); if (SUCCEEDED(hr) && (pIInkRecoResult!= NULL)) { BSTR bstrBestResult = NULL; hr = pIInkRecoResult->get_TopString(&bstrBestResult); pIInkRecoResult->Release(); pIInkRecoResult = NULL; if (SUCCEEDED(hr) && bstrBestResult) { MessageBoxW(hwnd, bstrBestResult, L"識别結果", MB_OK); SysFreeString(bstrBestResult); } } g_pIInkRecoContext->putref_Strokes(NULL); } pIInkStrokes->Release(); } ::SetCursor(hCursor); } else { return DefWindowProc(hwnd, uMsg, wParam, lParam); } break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; } //注冊windows 視窗類名 BOOL RegisterWindowClass(HINSTANCE hInstance) { WNDCLASSEX WndClassEx; WndClassEx.cbSize = sizeof(WndClassEx); WndClassEx.style = CS_HREDRAW | CS_VREDRAW; WndClassEx.lpfnWndProc = WndProc; WndClassEx.cbClsExtra = 0; WndClassEx.cbWndExtra = 0; WndClassEx.hInstance = hInstance; WndClassEx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); WndClassEx.hIconSm = WndClassEx.hIcon; WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW); WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClassEx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); WndClassEx.lpszClassName = gc_szAppName; if (!RegisterClassEx(&WndClassEx)) { return false; } return true; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE , LPSTR , int nCmdShow) { if (!RegisterWindowClass(hInstance)) return 0; CoInitialize(NULL); //建立視窗 HWND hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, gc_szAppName, gc_szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (NULL == hWnd) { return 0; } // 顯示視窗 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 開始消息循環 MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } CleanUp(); CoUninitialize(); return msg.wParam; }

VS2010啟動界面

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

VS2010項目開發

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

程式界面,以及軌迹

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

軌迹識别結果

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

清除筆迹

基于Visual C++2010與windows SDK fo windows7開發windows7平台的tabletpc應用(1)-漢字手寫軌迹輸入...

需要源代碼的請留言留下Email,我給大家發

本文作者專著《Visual C++2010開發權威指南》即将推出,敬請關注,Visual C++2010最近技術,Windows7開發最新技術!