📄 sapitutorial0dlg.cpp
字号:
// SapiTutorial0Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "SapiTutorial0.h"
#include "SapiTutorial0Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "sapi0.h"
#define WM_RECOEVENT WM_USER+1
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSapiTutorial0Dlg dialog
CSapiTutorial0Dlg::CSapiTutorial0Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CSapiTutorial0Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSapiTutorial0Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_uShape = -1;
}
void CSapiTutorial0Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSapiTutorial0Dlg)
DDX_Control(pDX, IDC_PLACEHOLDER, m_cPlaceholder);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSapiTutorial0Dlg, CDialog)
//{{AFX_MSG_MAP(CSapiTutorial0Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_RECOEVENT, OnRecoEvent)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSapiTutorial0Dlg message handlers
BOOL CSapiTutorial0Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon((HICON)(LoadImage(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, 0)), FALSE);
if (!InitializeSapi()) {
FinishSapi();
PostMessage(WM_CLOSE);
}
m_cPlaceholder.GetClientRect(&m_cPlaceRect);
m_cPlaceholder.ClientToScreen(&m_cPlaceRect);
ScreenToClient(&m_cPlaceRect);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSapiTutorial0Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSapiTutorial0Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
CDialog::OnPaint();
CBrush br(RGB(0,0,0));
dc.FillSolidRect(m_cPlaceRect, RGB(255,255,255));
dc.FrameRect(m_cPlaceRect, &br);
CPen pen(PS_SOLID, 1, RGB(0,0,0)), *oldpen;
CBrush brush(m_crShape), *oldbrush;
CRect bounds = m_cPlaceRect;
bounds.DeflateRect(20,20);
oldpen = dc.SelectObject(&pen);
oldbrush = dc.SelectObject(&brush);
if (m_uShape == VID_Square) {
dc.FillSolidRect(bounds, m_crShape);
dc.FrameRect(bounds, &br);
} else if (m_uShape == VID_Circle) {
dc.Ellipse(bounds);
} else if (m_uShape == VID_Triangle) {
POINT tripoints[3];
tripoints[0].x = bounds.TopLeft().x + (bounds.Width() / 2);
tripoints[0].y = bounds.TopLeft().y;
tripoints[1].x = bounds.TopLeft().x;
tripoints[1].y = bounds.BottomRight().y;
tripoints[2] = bounds.BottomRight();
dc.Polygon(tripoints, 3);
}
dc.SelectObject(oldpen);
dc.SelectObject(oldbrush);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSapiTutorial0Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
bool CSapiTutorial0Dlg::InitializeSapi() {
if (FAILED(CoInitialize(NULL))) {
AfxMessageBox("Error starting COM");
return false;
}
HRESULT hRes = g_cpEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
if (FAILED(hRes)) {
AfxMessageBox("Error starting SAPI");
return false;
}
hRes = g_cpEngine->CreateRecoContext(&g_cpRecoCtxt);
if (FAILED(hRes)) {
AfxMessageBox("Error creating context");
return false;
}
hRes = g_cpRecoCtxt->SetNotifyWindowMessage(m_hWnd, WM_RECOEVENT, 0, 0);
if (FAILED(hRes)) {
AfxMessageBox("Error creating notification window");
return false;
}
hRes = g_cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION));
if (FAILED(hRes)) {
AfxMessageBox("Error creating interest...seriously");
return false;
}
hRes = g_cpRecoCtxt->CreateGrammar(0, &g_cpCmdGrammar);
if (FAILED(hRes)) {
AfxMessageBox("Error creating grammar");
return false;
}
hRes = g_cpCmdGrammar->LoadCmdFromResource(
NULL,
MAKEINTRESOURCEW(IDR_SAPI0),
L"SRGRAMMAR",
MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL), SPLO_DYNAMIC);
if (FAILED(hRes)) {
AfxMessageBox("Error creating grammar rules");
return false;
}
// Set rules to active, we are now listening for commands
hRes = g_cpCmdGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE );
if (FAILED(hRes)) {
AfxMessageBox("Error setting rule state");
return false;
}
return true;
}
LRESULT CSapiTutorial0Dlg::OnRecoEvent(WPARAM wParam, LPARAM lParam) {
// Event helper class
CSpEvent event;
// Loop processing events while there are any in the queue
while (event.GetFrom(g_cpRecoCtxt) == S_OK)
{
// Look at recognition event only
switch (event.eEventId)
{
case SPEI_RECOGNITION:
ExecuteCommand(event.RecoResult());
break;
}
}
return 0;
}
void CSapiTutorial0Dlg::ExecuteCommand(ISpPhrase *pPhrase) {
SPPHRASE *pElements;
UINT uType = 0;
COLORREF crShape = RGB(0,0,0);
// Get the phrase elements, one of which is the rule id we specified in
// the grammar. Switch on it to figure out which command was recognized.
if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
{
switch ( pElements->Rule.ulId )
{
case VID_Commands:
{
switch(pElements->pProperties->vValue.ulVal )
{
case VID_QuitCmd: PostMessage(WM_CLOSE); break;
case VID_AboutCmd: PostMessage(WM_SYSCOMMAND, IDM_ABOUTBOX, 0); break;
case VID_Gen5Cmd: {
::ShellExecute(m_hWnd, "open", "http://www.generation5.org/", NULL, NULL, SW_SHOW);
break;
}
}
} break;
case VID_MainDraw:
{
const SPPHRASEPROPERTY *pProp = pElements->pProperties;
while (pProp) {
switch(pProp->vValue.ulVal )
{
case VID_Square: uType = VID_Square; break;
case VID_Circle: uType = VID_Circle; break;
case VID_Triangle: uType = VID_Triangle; break;
case VID_Red: crShape = RGB(255,0,0); break;
case VID_Green: crShape = RGB(0,255,0); break;
case VID_Blue: crShape = RGB(0,0,255); break;
case VID_Black: crShape = RGB(0,0,0); break;
case VID_White: crShape = RGB(255,255,255); break;
case VID_Yellow: crShape = RGB(255,255,0); break;
case VID_Gray: crShape = RGB(192,192,192); break;
}
pProp = pProp->pNextSibling;
}
} break;
}
DrawCommand(uType, crShape);
// Free the pElements memory which was allocated for us
::CoTaskMemFree(pElements);
}
}
void CSapiTutorial0Dlg::FinishSapi()
{
// Release grammar, if loaded
if (g_cpCmdGrammar) {
g_cpCmdGrammar.Release();
}
// Release recognition context, if created
if (g_cpRecoCtxt) {
g_cpRecoCtxt->SetNotifySink(NULL);
g_cpRecoCtxt.Release();
}
// Release recognition engine instance, if created
if (g_cpEngine) {
g_cpEngine.Release();
}
}
void CSapiTutorial0Dlg::OnDestroy()
{
CDialog::OnDestroy();
FinishSapi();
}
void CSapiTutorial0Dlg::DrawCommand(UINT uType, COLORREF crShape) {
m_uShape = uType;
m_crShape = crShape;
InvalidateRect(m_cPlaceRect, false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -