-
Notifications
You must be signed in to change notification settings - Fork 0
/
im01.cpp
66 lines (57 loc) · 1.82 KB
/
im01.cpp
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
#include <Windows.h>
#include <iostream>
#pragma comment(lib, "imm32")
#define IMC_GETCONVERSIONMODE 1
#define IMC_SETCONVERSIONMODE 2
#define IMC_GETOPENSTATUS 5
#define IMC_SETOPENSTATUS 6
namespace IME {
HWND GetFocusedWindow();
BOOL GetOpenStatus(HWND hwnd);
void SetOpenStatus(BOOL status, HWND hwnd);
DWORD GetConversionMode(HWND hwnd);
void SetConversionMode(DWORD mode, HWND hwnd);
};
int main(int argc, char* argv[]) {
HWND hwnd = IME::GetFocusedWindow();
if (argc > 1) {
IME::SetOpenStatus(argv[1][0] != '0' ? TRUE : FALSE, hwnd);
if (argv[1][1] && argv[1][2]) {
DWORD mode = atoi(argv[1] + 2);
IME::SetConversionMode(mode, hwnd);
}
}
else {
std::cout << IME::GetOpenStatus(hwnd) << "-" << IME::GetConversionMode(hwnd);
}
return 0;
}
namespace IME {
HWND GetFocusedWindow() {
HWND foreHwnd = GetForegroundWindow();
if (foreHwnd != nullptr) {
GUITHREADINFO guiThreadInfo = {sizeof(GUITHREADINFO)};
if (GetGUIThreadInfo(GetWindowThreadProcessId(foreHwnd, nullptr), &guiThreadInfo)) {
return guiThreadInfo.hwndFocus ? guiThreadInfo.hwndFocus : foreHwnd;
}
return foreHwnd;
}
return nullptr;
}
BOOL GetOpenStatus(HWND hwnd) {
DWORD_PTR status;
SendMessageTimeoutW(ImmGetDefaultIMEWnd(hwnd), WM_IME_CONTROL, IMC_GETOPENSTATUS, 0, 0, 200, &status);
return status ? TRUE : FALSE;
}
void SetOpenStatus(BOOL status, HWND hwnd) {
SendMessageTimeoutW(ImmGetDefaultIMEWnd(hwnd), WM_IME_CONTROL, IMC_SETOPENSTATUS, status, 0, 200, nullptr);
}
DWORD GetConversionMode(HWND hwnd) {
DWORD_PTR mode;
SendMessageTimeoutW(ImmGetDefaultIMEWnd(hwnd), WM_IME_CONTROL, IMC_GETCONVERSIONMODE, 0, 0, 200, &mode);
return mode;
}
void SetConversionMode(DWORD mode, HWND hwnd) {
SendMessageTimeoutW(ImmGetDefaultIMEWnd(hwnd), WM_IME_CONTROL, IMC_SETCONVERSIONMODE, mode, 0, 200, nullptr);
}
}