基于Visual C++2010与windows SDK fo windows7开发windows7平台的tabletpc应用(1)-手写数学公式输入...
搭建好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
轉載于:https://www.cnblogs.com/yincheng01/archive/2010/03/15/2213280.html
總結
以上是生活随笔為你收集整理的基于Visual C++2010与windows SDK fo windows7开发windows7平台的tabletpc应用(1)-手写数学公式输入...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 向Main函数传递参数(程序启动时
- 下一篇: 硬盘安装 solaris