【基于Visual C++2010与windows SDK for windows7开发平台的tabletpc应用--手写数学公式输入】
搭建好Visual C++2010与windows SDK fo windows7的开发平台以后,
小试牛刀,检验下开发windows7的下的tabletpc应用,这个东西财务记账比较多,
详情请见代码注释
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
// Windows 头文件
#include
//tabletpc头文件
#include
#include
// Asserts header
#include "assert.h"
#define ASSERT assert
#include "resource.h" // main symbols, including command IDs
#include "EventSinks.h" // 声明事件
#include "MathInputControl.h" // 定义数学输入头文件
const WCHAR gc_wszAppName[] = L"CSDN Math Input Control ";
// 数学输入控件指针
CMathInputControlHost* g_pMathInputControlHost;
//初始化
HRESULT CMathInputControlHost::Init(HWND hWnd, HWND hWndEdit)
{
HRESULT hr;
m_hWnd = hWnd;
m_hWndEdit = hWndEdit;
// 创建对象
hr = CoCreateInstance(CLSID_MathInputControl,
NULL,
CLSCTX_INPROC_SERVER,
IID_IMathInputControl,
(void **)&m_pIMathInputControl);
if (FAILED(hr))
{
// 失败则返回
ASSERT("failed" && FALSE);
return hr;
}
// 让数学输入控件自动适应变化
LONG right = mc_left + mc_width;
LONG bottom = mc_top + mc_height;
hr = m_pIMathInputControl->SetPosition(mc_left, mc_top, right, bottom);
if (FAILED(hr))
{
ASSERT("Failed to set Math Input Control position." && FALSE);
return hr;
}
m_pIMathInputControl->EnableExtendedButtons(VARIANT_TRUE);
m_pEventListener = new CMathInputControlEventListener(this);
if (!m_pEventListener)
{
ASSERT("Failed to create event listener for Math Input Control.");
return E_FAIL;
}
// 开始识别数学控件输入
hr = m_pEventListener->AdviseMathInputControl(m_pIMathInputControl);
if (FAILED(hr))
{
// 识别笔迹事件
ASSERT("Failed to advise on MIC events" && FALSE);
return hr;
}
return S_OK;
}
HRESULT CMathInputControlHost::OnMICInsert(
BSTR bstrRecoResultMathML
)
{
if (!m_hWndEdit)
{
ASSERT("Edit box control is not initialized." && FALSE);
return E_UNEXPECTED;
}
// 显示识别结果
SetWindowText(m_hWndEdit, (LPCWSTR)bstrRecoResultMathML);
// 隐藏控件
HideMIC();
return S_OK;
}
//关闭识别
HRESULT CMathInputControlHost::OnMICClose(void)
{
return HideMIC();
}
//清理识别结果
HRESULT CMathInputControlHost::OnMICClear(void)
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
if (!m_hWndEdit)
{
ASSERT("Edit box control is not initialized." && FALSE);
return E_UNEXPECTED;
}
LONG left, right, top, bottom;
hr = m_pIMathInputControl->GetPosition(&left, &top, &right, &bottom);
if (FAILED(hr))
{
ASSERT("Failed to get minimal window position." && FALSE);
return E_FAIL;
}
right = mc_left + mc_width;
bottom = mc_top + mc_height;
hr = m_pIMathInputControl->SetPosition(left, top, right, bottom);
if (FAILED(hr))
{
ASSERT("Failed to set window position." && FALSE);
return E_FAIL;
}
// 清理识别结果
SetWindowText(m_hWndEdit, L"");
return hr;
}
//显示控件
LRESULT CMathInputControlHost::OnMICShow()
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
VARIANT_BOOL vbShown = VARIANT_FALSE;
hr = m_pIMathInputControl->IsVisible(&vbShown);
if (FAILED(hr))
{
ASSERT("Failed to get visibility" && FALSE);
return E_FAIL;
}
if (vbShown != VARIANT_TRUE)
{
hr = m_pIMathInputControl->Show();
ASSERT("Failed to show Math Input Control window" && SUCCEEDED(hr));
}
return hr;
}
//隐藏控件
HRESULT CMathInputControlHost::HideMIC()
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
VARIANT_BOOL vbShown = VARIANT_FALSE;
hr = m_pIMathInputControl->IsVisible(&vbShown);
if (FAILED(hr))
{
ASSERT("Failed to get visibility" && FALSE);
return E_FAIL;
}
if (vbShown == VARIANT_TRUE)
{
hr = m_pIMathInputControl->Hide();
ASSERT("Failed to hide Math Input Control window" && SUCCEEDED(hr));
}
return hr;
}
//清理
void CleanUp()
{
// Release all objects
if (g_pMathInputControlHost != NULL)
{
delete g_pMathInputControlHost;
}
CoUninitialize();
}
//消息循环
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
{
// 重新设置输入区间的大小
HWND hWndEdit = g_pMathInputControlHost->GetEditWindow();
MoveWindow(
hWndEdit,
0,
LOWORD(lParam),
HIWORD(lParam),
TRUE
);
}
break;
case WM_COMMAND:
if (wParam == ID_SHOW)
{
g_pMathInputControlHost->OnMICShow();
}
else
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
//注册窗口类名
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 = NULL;
WndClassEx.hIconSm = NULL;
WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClassEx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
WndClassEx.lpszClassName = gc_wszAppName;
if (!RegisterClassEx(&WndClassEx))
{
MessageBox(NULL, L"Failed to register window class!",
gc_wszAppName, MB_ICONERROR);
false;
}
return true;
}
//起始窗体初始化
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE /* hPrevInstance */,
LPWSTR /* lpCmdLine */,
int nCmdShow)
{
if (!RegisterWindowClass(hInstance))
{
return 0;
}
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
CleanUp();
return 0;
}
// 创建程序窗体
HWND hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
gc_wszAppName,
gc_wszAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL, .
hInstance,
NULL
);
if (NULL == hWnd)
{
MessageBox(NULL, L"Error creating the window", L"Error",
MB_OK | MB_ICONINFORMATION);
CleanUp();
return 0;
}
//创建文本框接受识别结果
HWND hWndEdit = CreateWindow(
L"edit",
NULL,
// Specifies the style. of the window being created.
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL,
0,
0,
0,
0,
hWnd,
(HMENU)ID_EDIT,
hInstance,
NULL
);
if (NULL == hWnd)
{
MessageBox(NULL, L"Error creating the edit box control", L"Error",
MB_OK | MB_ICONINFORMATION);
CleanUp();
return 0;
}
// 创建数学监听控件与开始监听数学监听控件事件
g_pMathInputControlHost = new CMathInputControlHost();
if (!g_pMathInputControlHost)
{
ASSERT("Failed to create Math Input Control host.");
CleanUp();
return -1;
}
// 初始化数学控件
hr = g_pMathInputControlHost->Init(hWnd, hWndEdit);
if (FAILED(hr))
{
ASSERT("Failed to initialize Math Input Control host.");
CleanUp();
return -1;
}
// 显示主窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 开始消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CleanUp();
return (int)msg.wParam;
}
// Windows 头文件
#include
//tabletpc头文件
#include
#include
// Asserts header
#include "assert.h"
#define ASSERT assert
#include "resource.h" // main symbols, including command IDs
#include "EventSinks.h" // 声明事件
#include "MathInputControl.h" // 定义数学输入头文件
const WCHAR gc_wszAppName[] = L"CSDN Math Input Control ";
// 数学输入控件指针
CMathInputControlHost* g_pMathInputControlHost;
//初始化
HRESULT CMathInputControlHost::Init(HWND hWnd, HWND hWndEdit)
{
HRESULT hr;
m_hWnd = hWnd;
m_hWndEdit = hWndEdit;
// 创建对象
hr = CoCreateInstance(CLSID_MathInputControl,
NULL,
CLSCTX_INPROC_SERVER,
IID_IMathInputControl,
(void **)&m_pIMathInputControl);
if (FAILED(hr))
{
// 失败则返回
ASSERT("failed" && FALSE);
return hr;
}
// 让数学输入控件自动适应变化
LONG right = mc_left + mc_width;
LONG bottom = mc_top + mc_height;
hr = m_pIMathInputControl->SetPosition(mc_left, mc_top, right, bottom);
if (FAILED(hr))
{
ASSERT("Failed to set Math Input Control position." && FALSE);
return hr;
}
m_pIMathInputControl->EnableExtendedButtons(VARIANT_TRUE);
m_pEventListener = new CMathInputControlEventListener(this);
if (!m_pEventListener)
{
ASSERT("Failed to create event listener for Math Input Control.");
return E_FAIL;
}
// 开始识别数学控件输入
hr = m_pEventListener->AdviseMathInputControl(m_pIMathInputControl);
if (FAILED(hr))
{
// 识别笔迹事件
ASSERT("Failed to advise on MIC events" && FALSE);
return hr;
}
return S_OK;
}
HRESULT CMathInputControlHost::OnMICInsert(
BSTR bstrRecoResultMathML
)
{
if (!m_hWndEdit)
{
ASSERT("Edit box control is not initialized." && FALSE);
return E_UNEXPECTED;
}
// 显示识别结果
SetWindowText(m_hWndEdit, (LPCWSTR)bstrRecoResultMathML);
// 隐藏控件
HideMIC();
return S_OK;
}
//关闭识别
HRESULT CMathInputControlHost::OnMICClose(void)
{
return HideMIC();
}
//清理识别结果
HRESULT CMathInputControlHost::OnMICClear(void)
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
if (!m_hWndEdit)
{
ASSERT("Edit box control is not initialized." && FALSE);
return E_UNEXPECTED;
}
LONG left, right, top, bottom;
hr = m_pIMathInputControl->GetPosition(&left, &top, &right, &bottom);
if (FAILED(hr))
{
ASSERT("Failed to get minimal window position." && FALSE);
return E_FAIL;
}
right = mc_left + mc_width;
bottom = mc_top + mc_height;
hr = m_pIMathInputControl->SetPosition(left, top, right, bottom);
if (FAILED(hr))
{
ASSERT("Failed to set window position." && FALSE);
return E_FAIL;
}
// 清理识别结果
SetWindowText(m_hWndEdit, L"");
return hr;
}
//显示控件
LRESULT CMathInputControlHost::OnMICShow()
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
VARIANT_BOOL vbShown = VARIANT_FALSE;
hr = m_pIMathInputControl->IsVisible(&vbShown);
if (FAILED(hr))
{
ASSERT("Failed to get visibility" && FALSE);
return E_FAIL;
}
if (vbShown != VARIANT_TRUE)
{
hr = m_pIMathInputControl->Show();
ASSERT("Failed to show Math Input Control window" && SUCCEEDED(hr));
}
return hr;
}
//隐藏控件
HRESULT CMathInputControlHost::HideMIC()
{
HRESULT hr = S_OK;
if (!m_pIMathInputControl)
{
ASSERT("Math Input Control not initialized" && FALSE);
return E_UNEXPECTED;
}
VARIANT_BOOL vbShown = VARIANT_FALSE;
hr = m_pIMathInputControl->IsVisible(&vbShown);
if (FAILED(hr))
{
ASSERT("Failed to get visibility" && FALSE);
return E_FAIL;
}
if (vbShown == VARIANT_TRUE)
{
hr = m_pIMathInputControl->Hide();
ASSERT("Failed to hide Math Input Control window" && SUCCEEDED(hr));
}
return hr;
}
//清理
void CleanUp()
{
// Release all objects
if (g_pMathInputControlHost != NULL)
{
delete g_pMathInputControlHost;
}
CoUninitialize();
}
//消息循环
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
{
// 重新设置输入区间的大小
HWND hWndEdit = g_pMathInputControlHost->GetEditWindow();
MoveWindow(
hWndEdit,
0,
LOWORD(lParam),
HIWORD(lParam),
TRUE
);
}
break;
case WM_COMMAND:
if (wParam == ID_SHOW)
{
g_pMathInputControlHost->OnMICShow();
}
else
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
//注册窗口类名
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 = NULL;
WndClassEx.hIconSm = NULL;
WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClassEx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
WndClassEx.lpszClassName = gc_wszAppName;
if (!RegisterClassEx(&WndClassEx))
{
MessageBox(NULL, L"Failed to register window class!",
gc_wszAppName, MB_ICONERROR);
false;
}
return true;
}
//起始窗体初始化
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE /* hPrevInstance */,
LPWSTR /* lpCmdLine */,
int nCmdShow)
{
if (!RegisterWindowClass(hInstance))
{
return 0;
}
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
CleanUp();
return 0;
}
// 创建程序窗体
HWND hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
gc_wszAppName,
gc_wszAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL, .
hInstance,
NULL
);
if (NULL == hWnd)
{
MessageBox(NULL, L"Error creating the window", L"Error",
MB_OK | MB_ICONINFORMATION);
CleanUp();
return 0;
}
//创建文本框接受识别结果
HWND hWndEdit = CreateWindow(
L"edit",
NULL,
// Specifies the style. of the window being created.
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL,
0,
0,
0,
0,
hWnd,
(HMENU)ID_EDIT,
hInstance,
NULL
);
if (NULL == hWnd)
{
MessageBox(NULL, L"Error creating the edit box control", L"Error",
MB_OK | MB_ICONINFORMATION);
CleanUp();
return 0;
}
// 创建数学监听控件与开始监听数学监听控件事件
g_pMathInputControlHost = new CMathInputControlHost();
if (!g_pMathInputControlHost)
{
ASSERT("Failed to create Math Input Control host.");
CleanUp();
return -1;
}
// 初始化数学控件
hr = g_pMathInputControlHost->Init(hWnd, hWndEdit);
if (FAILED(hr))
{
ASSERT("Failed to initialize Math Input Control host.");
CleanUp();
return -1;
}
// 显示主窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 开始消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CleanUp();
return (int)msg.wParam;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9462618/viewspace-667109/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9462618/viewspace-667109/