Windows 8 SpellChecking API

在Windows 8下,多了一套很有趣的API,SpellChecking,这套API的作用也是一目了然,是做拼写检查的。这么有趣的一套API怎么能不写个程序玩玩呢,于是我写了个小程序,看了看对英文拼写检查的效果,如图。
20150518202121
拼写检查会给出三个结果,分别是删除,替换和建议,根据不同的结果我们可以调用不同的接口来获得最佳的体验。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SpellCheck.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <atlbase.h>
#include <atlstr.h>
#include <Spellcheck.h>
class CCoInitialize {
public:
CCoInitialize() {
CoInitializeEx(NULL, COINIT_MULTITHREADED);
}
~CCoInitialize() { CoUninitialize(); }
};
LPCWSTR kActionStrings[] = {
L"CORRECTIVE_ACTION_NONE",
L"CORRECTIVE_ACTION_GET_SUGGESTIONS",
L"CORRECTIVE_ACTION_REPLACE",
L"CORRECTIVE_ACTION_DELETE"
};
int _tmain(int argc, _TCHAR* argv[])
{
CCoInitialize com_init;
CComPtr spell_checker_factory;
HRESULT hr = CoCreateInstance(__uuidof(SpellCheckerFactory), NULL, CLSCTX_INPROC_SERVER, __uuidof(spell_checker_factory),
reinterpret_cast(&spell;_checker_factory));
if (FAILED(hr)) {
return 1;
}
LPCWSTR lang_tag = L"en-US";
BOOL suppored = FALSE;
spell_checker_factory->IsSupported(lang_tag, &suppored;);
if (!suppored) {
return 1;
}
CComPtr spell_checker;
hr = spell_checker_factory->CreateSpellChecker(lang_tag, &spell;_checker);
if (FAILED(hr)) {
return 1;
}
WCHAR my_text[] = L"Helloo world, I am am new heere, hvae fun";
wprintf(L"%s\n\n", my_text);
CComPtr spell_errors;
hr = spell_checker->Check(my_text, &spell;_errors);
if (FAILED(hr)) {
return 1;
}
CComPtr spell_error;
while (spell_errors->Next(&spell;_error) == S_OK) {
ULONG index, length;
if (SUCCEEDED(spell_error->get_StartIndex(&index;)) && SUCCEEDED(spell_error->get_Length(&length;))) {
CStringW tmp_str(my_text + index, length);
wprintf(L"%-10s ", tmp_str.GetString());
CORRECTIVE_ACTION action;
if (SUCCEEDED(spell_error->get_CorrectiveAction(&action;))) {
wprintf(L"%-40s ", kActionStrings[action]);
}
if (action == CORRECTIVE_ACTION_DELETE) {
wprintf(L"delete %s\n", tmp_str.GetString());
}
else if (action == CORRECTIVE_ACTION_GET_SUGGESTIONS) {
CComPtr spell_suggestions;
hr = spell_checker->Suggest(tmp_str.GetString(), &spell;_suggestions);
if (FAILED(hr)) {
break;;
}
WCHAR *suggestion_str;
while (spell_suggestions->Next(1, &suggestion;_str, NULL) == S_OK) {
wprintf(L"%s ", suggestion_str);
CoTaskMemFree(suggestion_str);
}
wprintf(L"\n");
}
else if (action == CORRECTIVE_ACTION_REPLACE) {
WCHAR *replace_str;
hr = spell_error->get_Replacement(&replace;_str);
wprintf(L"%s\n", replace_str);
CoTaskMemFree(replace_str);
}
}
spell_error.Release();
}
return 0;
}

Tips

Windows 8.1 GenericMapping对应的ACCESS_MASK

我们在创建或者打开对象的时候需要指定ACCESS_MASK,有的时候为了方便,我们会在ACCESS_MASK的参数中填GenericRead,GenericWrite这样的值,那么对于这些对象来说,这些GenericXXX究竟是什么样的ACCESS_MASK都是保存在对象的GenericMapping中,以下就是Windows 8.1中所有对象的GenericMapping了。
20150502010045

将ACCESS_MASK数字转换成我们看得懂的宏,可以使用我写的一个小网页:
http://0cch.com/accessmask.html

Tips