-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.h
More file actions
501 lines (421 loc) · 12.9 KB
/
Copy pathCommon.h
File metadata and controls
501 lines (421 loc) · 12.9 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#pragma once
#include <olectl.h>
#include <shlwapi.h>
#include <stdio.h>
#include <tchar.h>
#ifndef GET_SC_WPARAM
#define GET_SC_WPARAM(wParam) ((INT)wParam & 0xFFF0)
#endif
#define SYSCOMMAND_STUDY_USING_JAPANESE 0x3350
#define SYSCOMMAND_STUDY_USING_ENGLISH 0x3360
enum STUDY_MODE
{
STUDY_MODE_DEFAULT = 0,
STUDY_MODE_USING_ENGLISH = -1,
STUDY_MODE_USING_JAPANESE = +1,
};
extern HINSTANCE g_hInstance;
extern BOOL g_bHighSpeed;
static inline LPTSTR LoadStringDx(INT ids)
{
static TCHAR sz[512];
LoadString(g_hInstance, ids, sz, 512);
return sz;
}
static inline void DoSleep(DWORD dwMilliseconds)
{
if (g_bHighSpeed)
Sleep(dwMilliseconds / 5);
else
Sleep(dwMilliseconds);
}
#define DO_SLEEP DoSleep
STUDY_MODE getStudyMode(VOID);
STUDY_MODE getStudyModeDefault(VOID);
typedef LANGID (WINAPI *FN_GetThreadUILanguage)(VOID);
typedef LANGID (WINAPI *FN_SetThreadUILanguage)(LANGID);
static inline BOOL WonIsWindowsVistaOrGreater(VOID)
{
OSVERSIONINFO osvi = { sizeof(osvi) };
if (!GetVersionEx(&osvi))
return FALSE;
return osvi.dwMajorVersion >= 6;
}
static inline LANGID WonGetThreadUILanguage()
{
static FN_GetThreadUILanguage s_fn = NULL;
if (!s_fn)
s_fn = reinterpret_cast<FN_GetThreadUILanguage>(
GetProcAddress(GetModuleHandleA("kernel32"), "GetThreadUILanguage"));
if (WonIsWindowsVistaOrGreater() && s_fn)
return (*s_fn)();
return LANGIDFROMLCID(GetThreadLocale());
}
static inline LANGID WonSetThreadUILanguage(LANGID LangID)
{
static FN_SetThreadUILanguage s_fn = NULL;
if (!s_fn)
s_fn = reinterpret_cast<FN_SetThreadUILanguage>(
GetProcAddress(GetModuleHandleA("kernel32"), "SetThreadUILanguage"));
if (WonIsWindowsVistaOrGreater() && s_fn)
return (*s_fn)(LangID);
if (SetThreadLocale(MAKELCID(LangID, SORT_DEFAULT)))
return LangID;
return 0;
}
static inline STUDY_MODE
getStudyModeDefault(VOID)
{
if (PRIMARYLANGID(WonGetThreadUILanguage()) == LANG_JAPANESE)
return STUDY_MODE_USING_JAPANESE;
return STUDY_MODE_USING_ENGLISH;
}
static inline STUDY_MODE
getStudyMode(VOID)
{
HKEY hKey;
LSTATUS error;
error = RegOpenKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Katayama Hirofumi MZ\\Moji No Benkyou"),
0, KEY_READ, &hKey);
if (error)
return STUDY_MODE_DEFAULT;
DWORD dwValue = 0;
DWORD cbValue = sizeof(dwValue);
error = RegQueryValueEx(hKey, TEXT("StudyMode"), NULL, NULL, (PBYTE)&dwValue, &cbValue);
RegCloseKey(hKey);
if (error)
return STUDY_MODE_DEFAULT;
switch (dwValue)
{
case STUDY_MODE_USING_ENGLISH:
case STUDY_MODE_USING_JAPANESE:
return (STUDY_MODE)dwValue;
default:
return STUDY_MODE_DEFAULT;
}
}
static inline STUDY_MODE
getStudyModeReal(VOID)
{
STUDY_MODE mode = getStudyMode();
if (mode == STUDY_MODE_DEFAULT)
mode = getStudyModeDefault();
return mode;
}
static inline BOOL
rememberStudyMode(HWND hwnd, STUDY_MODE mode)
{
HKEY hKey;
LSTATUS error;
DWORD dwDisposition;
error = RegCreateKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Katayama Hirofumi MZ\\Moji No Benkyou"),
0, NULL, 0, KEY_WRITE, NULL, &hKey, &dwDisposition);
if (error)
return FALSE;
if (mode == STUDY_MODE_DEFAULT)
{
error = RegDeleteValue(hKey, TEXT("StudyMode"));
}
else
{
DWORD dwValue = (DWORD)mode;
error = RegSetValueEx(hKey, TEXT("StudyMode"), 0, REG_DWORD, (PBYTE)&dwValue, sizeof(dwValue));
}
RegCloseKey(hKey);
if (!error)
MessageBox(hwnd, TEXT("This mode change will take effect after restarting the app.\n\nこの モード へんこう は、アプリ の さいきどう の あと に ゆうこう に なります。"), TEXT("Moji No Benkyou"), MB_ICONINFORMATION);
return !error;
}
static inline BOOL
applyStudyMode(STUDY_MODE mode)
{
switch (mode)
{
case STUDY_MODE_DEFAULT:
break;
case STUDY_MODE_USING_ENGLISH:
WonSetThreadUILanguage(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
break;
case STUDY_MODE_USING_JAPANESE:
WonSetThreadUILanguage(MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT));
break;
}
return TRUE;
}
static inline
BOOL CopyText(HWND hwnd, LPCWSTR text)
{
BOOL ret = FALSE;
if (OpenClipboard(hwnd))
{
EmptyClipboard();
SIZE_T cbText = (lstrlenW(text) + 1) * sizeof(WCHAR);
HGLOBAL hGlobal = GlobalAlloc(GHND | GMEM_SHARE, cbText);
if (hGlobal)
{
LPWSTR psz = (LPWSTR)GlobalLock(hGlobal);
if (psz)
{
CopyMemory(psz, text, cbText);
GlobalUnlock(hGlobal);
SetClipboardData(CF_UNICODETEXT, hGlobal);
ret = TRUE;
}
}
CloseClipboard();
}
return ret;
}
static inline BOOL
smartGetTextExtent(HDC hDC, LPCTSTR text, LONG maxWidth, LPSIZE pSize)
{
SIZE siz;
GetTextExtentPoint32(hDC, text, lstrlen(text), &siz);
if (siz.cx <= maxWidth)
{
*pSize = siz;
return FALSE; // Single line
}
RECT rc = { 0, 0, maxWidth, 0 };
DrawText(hDC, text, -1, &rc, DT_LEFT | DT_TOP | DT_CALCRECT | DT_NOPREFIX | DT_WORDBREAK);
siz.cx = rc.right - rc.left;
siz.cy = rc.bottom - rc.top;
*pSize = siz;
return TRUE; // Multiple line
}
static inline BOOL
smartDrawText(HDC hDC, LPCTSTR text, LPRECT prc, INT maxWidth)
{
SIZE siz;
BOOL multiline = smartGetTextExtent(hDC, text, maxWidth, &siz);
prc->right = prc->left + siz.cx;
prc->bottom = prc->top + siz.cy;
if (multiline)
DrawText(hDC, text, -1, prc, DT_CENTER | DT_TOP | DT_NOPREFIX | DT_WORDBREAK);
else
DrawText(hDC, text, -1, prc, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_WORDBREAK);
return TRUE;
}
BOOL SerializeRegion(std::vector<WORD>& out, HRGN hRgn);
HRGN DeserializeRegion(const WORD *pw, size_t data_size);
BOOL SerializeRegion254(std::vector<BYTE>& out, HRGN hRgn);
HRGN DeserializeRegion254(const BYTE *pb, size_t data_size);
HBITMAP LoadBitmapFromFile(LPCTSTR pszFileName);
BOOL SaveBitmapToFile(LPCTSTR pszFileName, HBITMAP hbm);
HBITMAP CreateBitmapFromRegionGeneric(HRGN hRgn, INT cxy);
HBITMAP CreateBitmapFromRegion(HRGN hRgn);
HBITMAP CreateBitmapFromRegion254(HRGN hRgn);
HRGN CreateRegionFromBitmapGeneric(HBITMAP hbm, INT cxy);
HRGN CreateRegionFromBitmap(HBITMAP hbm);
HRGN CreateRegionFromBitmap254(HBITMAP hbm);
static HBITMAP
LoadGif(HINSTANCE hInst, INT res)
{
HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(res), TEXT("GIF"));
DWORD cbData = ::SizeofResource(hInst, hRsrc);
HGLOBAL hGlobal = ::LoadResource(hInst, hRsrc);
PVOID pvData = ::LockResource(hGlobal);
if (!pvData)
return FALSE;
IStream *pStream = SHCreateMemStream((PBYTE)pvData, cbData);
if (!pStream)
return FALSE;
IPicture *pPicture;
HRESULT hr = OleLoadPicture(pStream, 0, FALSE, IID_IPicture, (void**)&pPicture);
if (FAILED(hr))
{
pStream->Release();
return FALSE;
}
OLE_HANDLE hPic = NULL;
pPicture->get_Handle(&hPic);
HBITMAP hBitmap = (HBITMAP)CopyImage((HBITMAP)hPic, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
pPicture->Release();
pStream->Release();
return hBitmap;
}
// スレッドの優先度を変更する。
class AutoPriority
{
public:
AutoPriority()
{
::SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
}
~AutoPriority()
{
::SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
}
};
static BOOL
MyPlaySound(LPCTSTR pszName)
{
#if 1
TCHAR szFile[MAX_PATH], szCommand[MAX_PATH + 64];
{
AutoPriority auto_priority;
HRSRC hRsrc = ::FindResource(g_hInstance, pszName, TEXT("MP3"));
DWORD cbData = ::SizeofResource(g_hInstance, hRsrc);
HGLOBAL hGlobal = ::LoadResource(g_hInstance, hRsrc);
PVOID pvData = ::LockResource(hGlobal);
if (!pvData)
return FALSE;
TCHAR szTempPath[MAX_PATH];
GetTempPath(_countof(szTempPath), szTempPath);
GetTempFileName(szTempPath, TEXT("MJB"), 0, szFile);
FILE *fout = _tfopen(szFile, TEXT("wb"));
if (!fout)
return FALSE;
fwrite(pvData, cbData, 1, fout);
fclose(fout);
wnsprintf(szCommand, _countof(szCommand), TEXT("open \"%s\" type mpegvideo alias myaudio"), szFile);
}
mciSendString(szCommand, NULL, 0, 0);
mciSendString(TEXT("play myaudio wait"), NULL, 0, 0);
mciSendString(TEXT("close myaudio"), NULL, 0, 0);
DeleteFile(szFile);
return TRUE;
#else
return PlaySound(pszName, g_hInstance, SND_SYNC | SND_RESOURCE | SND_NODEFAULT);
#endif
}
static inline
unsigned __stdcall MyPlaySoundAsyncThreadProc(void *arg)
{
LPCTSTR pszName = (LPCTSTR)arg;
MyPlaySound(pszName);
return 0;
}
static inline BOOL
MyPlaySoundAsync(LPCTSTR pszName)
{
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, MyPlaySoundAsyncThreadProc, (void *)pszName, 0, NULL);
CloseHandle(hThread);
return hThread != NULL;
}
static RECT workarea_from_window(HWND hwnd)
{
#if (WINVER >= 0x0500)
MONITORINFO mi;
mi.cbSize = sizeof(mi);
HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (GetMonitorInfo(hMonitor, &mi))
{
return mi.rcWork;
}
#endif
RECT rc;
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
return rc;
}
static void reposition_point(LPPOINT ppt, SIZE siz, LPCRECT prc)
{
if (ppt->x + siz.cx > prc->right)
ppt->x = prc->right - siz.cx;
if (ppt->y + siz.cy > prc->bottom)
ppt->y = prc->bottom - siz.cy;
if (ppt->x < prc->left)
ppt->x = prc->left;
if (ppt->y < prc->top)
ppt->y = prc->top;
}
static void center_window(HWND hwnd)
{
BOOL bChild = !!(GetWindowStyle(hwnd) & WS_CHILD);
HWND hwndParent;
if (bChild)
hwndParent = ::GetParent(hwnd);
else
hwndParent = ::GetWindow(hwnd, GW_OWNER);
RECT rcWorkArea = workarea_from_window(hwnd);
RECT rcParent;
if (hwndParent)
::GetWindowRect(hwndParent, &rcParent);
else
rcParent = rcWorkArea;
SIZE sizParent = {
rcParent.right - rcParent.left,
rcParent.bottom - rcParent.top
};
RECT rc;
::GetWindowRect(hwnd, &rc);
SIZE siz = { rc.right - rc.left, rc.bottom - rc.top };
POINT pt;
pt.x = rcParent.left + (sizParent.cx - siz.cx) / 2;
pt.y = rcParent.top + (sizParent.cy - siz.cy) / 2;
if (bChild && hwndParent)
{
::GetClientRect(hwndParent, &rcParent);
::MapWindowPoints(hwndParent, NULL, (LPPOINT)&rcParent, 2);
reposition_point(&pt, siz, &rcParent);
::ScreenToClient(hwndParent, &pt);
}
else
{
reposition_point(&pt, siz, &rcWorkArea);
}
::SetWindowPos(hwnd, NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
static inline INT_PTR CALLBACK
AboutDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
center_window(hDlg);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hDlg, IDOK);
break;
case IDCANCEL:
EndDialog(hDlg, IDCANCEL);
break;
}
}
return FALSE;
}
static inline LPVOID MyLoadRes(HINSTANCE hInst, LPCWSTR type, LPCWSTR name, DWORD *psize) {
HRSRC hRsrc = ::FindResource(hInst, name, type);
if (psize)
*psize = ::SizeofResource(hInst, hRsrc);
HGLOBAL hGlobal = ::LoadResource(g_hInstance, hRsrc);
return ::LockResource(hGlobal);
}
static inline bool remember_moji(const std::wstring& str) {
HKEY hKey;
LSTATUS error;
error = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Katayama Hirofumi MZ\\Moji No Benkyou",
0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL);
if (error)
return false;
error = RegSetValueExW(hKey, str.c_str(), 0, REG_SZ, (PBYTE)L"", 0);
RegCloseKey(hKey);
return error == NO_ERROR;
}
static inline bool recall_moji(const std::wstring& str) {
HKEY hKey;
LSTATUS error;
error = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Katayama Hirofumi MZ\\Moji No Benkyou",
0, KEY_READ, &hKey);
if (error)
return false;
error = RegQueryValueExW(hKey, str.c_str(), NULL, NULL, NULL, NULL);
RegCloseKey(hKey);
return error == NO_ERROR;
}
static inline bool forget_moji(const std::wstring& str) {
HKEY hKey;
LSTATUS error;
error = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Katayama Hirofumi MZ\\Moji No Benkyou",
0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL);
if (error)
return false;
error = RegDeleteValueW(hKey, str.c_str());
RegCloseKey(hKey);
return error == NO_ERROR;
}