搭建好Visual C++2010與windows SDK fo windows7的開發平台以後,
小試牛刀,檢驗下開發windows7的下的tabletpc應用,這個東西财務記賬比較多,
大家先看效果,然後講解詳細代碼
詳情請見代碼注釋
// Windows 頭檔案
#include <windows.h>
//tabletpc頭檔案
#include <micaut.h>
#include <micaut_i.c>
// 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;
}
需要源代碼,請在本人CSDN部落格留言email