搭建好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启动界面
VS2010项目开发
程序界面,以及轨迹
轨迹识别结果
清除笔迹
需要源代码的请留言留下Email,我给大家发
本文作者专著《Visual C++2010开发权威指南》即将推出,敬请关注,Visual C++2010最近技术,Windows7开发最新技术!
原文链接: http://blog.csdn.net/yincheng01/article/details/5067989