1 |
#define STRICT |
2 |
static char *WinMisc_id = |
3 |
"@(#)Copyright (C) NTT-IT 1998-2002 -- winmisc.cpp -- Ver1.00b2"; |
4 |
/* ========================================================================== |
5 |
Project Name : Universal Library |
6 |
Outline : WinMisc Function |
7 |
Create : 1998-02-20(Wed) |
8 |
Update : 2002-09-25(Wed) |
9 |
Copyright : S.Hayakawa NTT-IT |
10 |
Reference : |
11 |
======1=========2=========3=========4=========5=========6=========7======= */ |
12 |
#include "winmisc.h" |
13 |
|
14 |
|
15 |
/* ========================================================================== |
16 |
Function Name : (void) SetDlgPos() |
17 |
Outline : ダイアログの位置を移動する |
18 |
Arguments : HWND hWnd (in) ダイアログのウインドウハンドル |
19 |
: int pos (in) 移動場所を示す値 |
20 |
Return Value : なし |
21 |
Reference : |
22 |
Renewal : |
23 |
Notes : |
24 |
Attention : |
25 |
Up Date : |
26 |
======1=========2=========3=========4=========5=========6=========7======= */ |
27 |
void SetDlgPos(HWND hWnd, int pos) |
28 |
{ |
29 |
int x; |
30 |
int y; |
31 |
RECT rect; |
32 |
|
33 |
::GetWindowRect(hWnd, &rect); |
34 |
|
35 |
int cx = ::GetSystemMetrics(SM_CXFULLSCREEN); |
36 |
int cy = ::GetSystemMetrics(SM_CYFULLSCREEN); |
37 |
int xsize = rect.right - rect.left; |
38 |
int ysize = rect.bottom - rect.top; |
39 |
|
40 |
switch (pos) { |
41 |
case POSITION_LEFTTOP: |
42 |
x = 0; |
43 |
y = 0; |
44 |
break; |
45 |
case POSITION_LEFTBOTTOM: |
46 |
x = 0; |
47 |
y = cy - ysize; |
48 |
break; |
49 |
case POSITION_RIGHTTOP: |
50 |
x = cx - xsize; |
51 |
y = 0; |
52 |
break; |
53 |
case POSITION_RIGHTBOTTOM: |
54 |
x = cx - xsize; |
55 |
y = cy - ysize; |
56 |
break; |
57 |
case POSITION_CENTER: |
58 |
x = (cx - xsize) / 2; |
59 |
y = (cy - ysize) / 2; |
60 |
break; |
61 |
case POSITION_OUTSIDE: |
62 |
x = cx; |
63 |
y = cy; |
64 |
break; |
65 |
} |
66 |
|
67 |
::MoveWindow(hWnd, x, y, xsize, ysize, TRUE); |
68 |
} |
69 |
|
70 |
/* ========================================================================== |
71 |
Function Name : (BOOL) EnableItem() |
72 |
Outline : ダイアログアイテムを有効/無効にする |
73 |
Arguments : HWND hWnd (in) ダイアログのハンドル |
74 |
: int idControl (in) ダイアログアイテムの ID |
75 |
: BOOL flag |
76 |
Return Value : 成功 TRUE |
77 |
: 失敗 FALSE |
78 |
Reference : |
79 |
Renewal : |
80 |
Notes : |
81 |
Attention : |
82 |
Up Date : |
83 |
======1=========2=========3=========4=========5=========6=========7======= */ |
84 |
BOOL EnableItem(HWND hWnd, int idControl, BOOL flag) |
85 |
{ |
86 |
HWND hWndItem; |
87 |
|
88 |
if ((hWndItem = ::GetDlgItem(hWnd, idControl)) == NULL) |
89 |
return FALSE; |
90 |
|
91 |
return ::EnableWindow(hWndItem, flag); |
92 |
} |
93 |
|
94 |
/* ========================================================================== |
95 |
Function Name : (void) EncodePassword() |
96 |
Outline : パスワードをエンコード(?)する。 |
97 |
Arguments : char cPassword (in) 変換する文字列 |
98 |
: int cEncodePassword (out) 変換された文字列 |
99 |
Return Value : なし |
100 |
Reference : |
101 |
Renewal : |
102 |
Notes : |
103 |
Attention : |
104 |
Up Date : |
105 |
======1=========2=========3=========4=========5=========6=========7======= */ |
106 |
void EncodePassword(TCHAR *cPassword, TCHAR *cEncodePassword) |
107 |
{ |
108 |
DWORD dwCnt; |
109 |
DWORD dwPasswordLength = ::lstrlen(cPassword); |
110 |
|
111 |
for (dwCnt = 0; dwCnt < dwPasswordLength; dwCnt++) |
112 |
cEncodePassword[dwPasswordLength - 1 - dwCnt] = cPassword[dwCnt] ^ 0xff; |
113 |
|
114 |
cEncodePassword[dwPasswordLength] = '\0'; |
115 |
} |
116 |
|
117 |
/* ========================================================================== |
118 |
Function Name : (BOOL) SaveFileDlg() |
119 |
Outline : 「名前を指定して保存」ダイアログを開き、指定されたファイル |
120 |
: パスを指定されたアイテムに送る |
121 |
Arguments : HWND hWnd (in) 親ウインドウのハンドル |
122 |
: UINT editCtl (in) アイテムの ID |
123 |
: char *title (in) ウインドウタイトル |
124 |
: char *filter (in) 表示するファイルのフィルタ |
125 |
: char *defaultDir (in) デフォルトのパス |
126 |
Return Value : 成功 TRUE |
127 |
: 失敗 FALSE |
128 |
Reference : |
129 |
Renewal : |
130 |
Notes : |
131 |
Attention : |
132 |
Up Date : |
133 |
======1=========2=========3=========4=========5=========6=========7======= */ |
134 |
BOOL SaveFileDlg(HWND hWnd, UINT editCtl, TCHAR *title, TCHAR *filter, TCHAR *defaultDir) |
135 |
{ |
136 |
TCHAR *szDirName; |
137 |
TCHAR szFile[MAX_PATH] = _T(""); |
138 |
TCHAR szPath[MAX_PATH]; |
139 |
OPENFILENAME ofn; |
140 |
|
141 |
szDirName = (TCHAR *) malloc(MAX_PATH); |
142 |
|
143 |
if (editCtl == 0xffffffff) { |
144 |
::GetDlgItemText(hWnd, editCtl, szDirName, MAX_PATH); |
145 |
::lstrcpy(szDirName, defaultDir); |
146 |
} |
147 |
|
148 |
if (*szDirName == '"') |
149 |
szDirName++; |
150 |
if (szDirName[::lstrlen(szDirName) - 1] == '"') |
151 |
szDirName[::lstrlen(szDirName) - 1] = '\0'; |
152 |
|
153 |
TCHAR *ptr = _tcsrchr(szDirName, '\\'); |
154 |
if (ptr == NULL) { |
155 |
::lstrcpy(szFile, szDirName); |
156 |
if (defaultDir != NULL && *szDirName == 0) |
157 |
::lstrcpy(szDirName, defaultDir); |
158 |
} else { |
159 |
*ptr = 0; |
160 |
::lstrcpy(szFile, ptr + 1); |
161 |
} |
162 |
|
163 |
memset(&ofn, 0, sizeof(ofn)); |
164 |
ofn.lStructSize = sizeof(OPENFILENAME); |
165 |
ofn.hwndOwner = hWnd; |
166 |
ofn.lpstrFilter = filter; |
167 |
ofn.nFilterIndex = 1; |
168 |
ofn.lpstrFile = szFile; |
169 |
ofn.nMaxFile = sizeof(szFile); |
170 |
ofn.lpstrTitle = title; |
171 |
ofn.lpstrInitialDir = szDirName; |
172 |
ofn.Flags = OFN_HIDEREADONLY; |
173 |
|
174 |
if (::GetSaveFileName(&ofn) == FALSE) { |
175 |
free(szDirName); |
176 |
return FALSE; |
177 |
} |
178 |
|
179 |
::lstrcpy(szPath, ofn.lpstrFile); |
180 |
|
181 |
::SetDlgItemText(hWnd, editCtl, szPath); |
182 |
::lstrcpy(defaultDir, szPath); |
183 |
|
184 |
free(szDirName); |
185 |
|
186 |
return TRUE; |
187 |
} |
188 |
|
189 |
/* ========================================================================== |
190 |
Function Name : (BOOL) OpenFileDlg() |
191 |
Outline : 「ファイルを開く」ダイアログを開き、指定されたファイル |
192 |
: パスを指定されたアイテムに送る |
193 |
Arguments : HWND hWnd (in) 親ウインドウのハンドル |
194 |
: UINT editCtl (in) アイテムの ID |
195 |
: char *title (in) ウインドウタイトル |
196 |
: char *filter (in) 表示するファイルのフィルタ |
197 |
: char *defaultDir (in) デフォルトのパス |
198 |
Return Value : 成功 TRUE |
199 |
: 失敗 FALSE |
200 |
Reference : |
201 |
Renewal : |
202 |
Notes : |
203 |
Attention : |
204 |
Up Date : |
205 |
======1=========2=========3=========4=========5=========6=========7======= */ |
206 |
BOOL OpenFileDlg(HWND hWnd, UINT editCtl, TCHAR *title, TCHAR *filter, TCHAR *defaultDir) |
207 |
{ |
208 |
TCHAR *szDirName; |
209 |
TCHAR szFile[MAX_PATH] = _T(""); |
210 |
TCHAR szPath[MAX_PATH]; |
211 |
OPENFILENAME ofn; |
212 |
|
213 |
szDirName = (TCHAR *) malloc(MAX_PATH); |
214 |
|
215 |
if (editCtl != 0xffffffff) { |
216 |
::GetDlgItemText(hWnd, editCtl, szDirName, MAX_PATH); |
217 |
::lstrcpy(szDirName, defaultDir); |
218 |
} |
219 |
|
220 |
if (*szDirName == '"') |
221 |
szDirName++; |
222 |
if (szDirName[::lstrlen(szDirName) - 1] == '"') |
223 |
szDirName[::lstrlen(szDirName) - 1] = '\0'; |
224 |
|
225 |
TCHAR *ptr = _tcsrchr(szDirName, '\\'); |
226 |
if (ptr == NULL) { |
227 |
::lstrcpy(szFile, szDirName); |
228 |
if (defaultDir != NULL && *szDirName == 0) |
229 |
::lstrcpy(szDirName, defaultDir); |
230 |
} else { |
231 |
*ptr = 0; |
232 |
::lstrcpy(szFile, ptr + 1); |
233 |
} |
234 |
|
235 |
memset(&ofn, 0, sizeof(ofn)); |
236 |
ofn.lStructSize = sizeof(OPENFILENAME); |
237 |
ofn.hwndOwner = hWnd; |
238 |
ofn.lpstrFilter = filter; |
239 |
ofn.nFilterIndex = 1; |
240 |
ofn.lpstrFile = szFile; |
241 |
ofn.nMaxFile = sizeof(szFile); |
242 |
ofn.lpstrTitle = title; |
243 |
ofn.lpstrInitialDir = szDirName; |
244 |
ofn.Flags = OFN_HIDEREADONLY | OFN_NODEREFERENCELINKS; |
245 |
|
246 |
if (::GetOpenFileName(&ofn) == FALSE) { |
247 |
free(szDirName); |
248 |
return FALSE; |
249 |
} |
250 |
|
251 |
::lstrcpy(szPath, ofn.lpstrFile); |
252 |
|
253 |
::SetDlgItemText(hWnd, editCtl, szPath); |
254 |
::lstrcpy(defaultDir, szPath); |
255 |
|
256 |
free(szDirName); |
257 |
|
258 |
return TRUE; |
259 |
} |
260 |
|
261 |
/* ========================================================================== |
262 |
Function Name : (int CALLBACK) BrowseCallbackProc() |
263 |
Outline : BrowseForFolder()のコールバック関数。 |
264 |
Arguments : HWND hWnd (in) 親ウインドウのハンドル |
265 |
: UINT uMsg (in) ウインドウタイトル |
266 |
: LPARAM lParam (in) LPARAM |
267 |
: LPARAM lpData (in) BROWSEINFO の lParam |
268 |
Return Value : |
269 |
Reference : |
270 |
Renewal : |
271 |
Notes : |
272 |
Attention : |
273 |
Up Date : |
274 |
======1=========2=========3=========4=========5=========6=========7======= */ |
275 |
int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData) |
276 |
{ |
277 |
switch (uMsg) { |
278 |
case BFFM_INITIALIZED: |
279 |
::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, lpData); |
280 |
break; |
281 |
case BFFM_SELCHANGED: |
282 |
break; |
283 |
} |
284 |
|
285 |
return 0; |
286 |
} |
287 |
|
288 |
/* ========================================================================== |
289 |
Function Name : (BOOL) GetModulePath() |
290 |
Outline : この関数を実行したモジュールのパスを取得する。 |
291 |
Arguments : char szPath (out) モジュールのパス |
292 |
: DWORD dwMaxPath (in) szPathの大きさ |
293 |
Return Value : 成功 TRUE |
294 |
: 失敗 FALSE |
295 |
Reference : |
296 |
Renewal : |
297 |
Notes : |
298 |
Attention : |
299 |
Up Date : |
300 |
======1=========2=========3=========4=========5=========6=========7======= */ |
301 |
BOOL GetModulePath(TCHAR *szPath, DWORD dwMaxPath) |
302 |
{ |
303 |
TCHAR *pt; |
304 |
|
305 |
if (::GetModuleFileName(NULL, szPath, dwMaxPath) == FALSE) |
306 |
return FALSE; |
307 |
|
308 |
pt = _tcsrchr(szPath, '\\'); |
309 |
*pt = '\0'; |
310 |
|
311 |
return TRUE; |
312 |
} |
313 |
|
314 |
/* ========================================================================== |
315 |
Function Name : (UINT) GetResourceType() |
316 |
Outline : 指定されたパスのドライブ情報を取得する |
317 |
Arguments : LPCTSTR lpszPath (in) ドライブ情報を知りたいパス |
318 |
Return Value : リソースタイプ |
319 |
Reference : |
320 |
Renewal : |
321 |
Notes : |
322 |
Attention : |
323 |
Up Date : |
324 |
======1=========2=========3=========4=========5=========6=========7======= */ |
325 |
UINT GetResourceType(LPCTSTR lpszPath) |
326 |
{ |
327 |
UINT ret; |
328 |
TCHAR szCurrentPath[MAX_PATH]; |
329 |
|
330 |
if (::GetCurrentDirectory(MAX_PATH, szCurrentPath) == 0) |
331 |
return 0; |
332 |
|
333 |
if (::SetCurrentDirectory(lpszPath) == FALSE) |
334 |
return 0; |
335 |
|
336 |
ret = ::GetDriveType(NULL); |
337 |
|
338 |
::SetCurrentDirectory(szCurrentPath); |
339 |
|
340 |
return ret; |
341 |
} |
342 |
|
343 |
/* ========================================================================== |
344 |
Function Name : (char *) pathTok() |
345 |
Outline : パス用の strtok |
346 |
Arguments : char *str (in) トークン解析するパス |
347 |
: char *separator (in) トークン文字列 |
348 |
Return Value : 成功 切り出した文字列 |
349 |
: 失敗 NULL |
350 |
Reference : |
351 |
Renewal : |
352 |
Notes : |
353 |
Attention : |
354 |
Up Date : |
355 |
======1=========2=========3=========4=========5=========6=========7======= */ |
356 |
TCHAR *PathTok(TCHAR *str, TCHAR *separator) |
357 |
{ |
358 |
static TCHAR *sv_str; |
359 |
|
360 |
if (str != NULL) |
361 |
sv_str = str; |
362 |
else if (sv_str != NULL) |
363 |
str = sv_str; |
364 |
else |
365 |
return NULL; |
366 |
|
367 |
while (*str != '\0' && _tcschr(separator, *str) != NULL) |
368 |
str++; |
369 |
|
370 |
if (*str == '\"') { |
371 |
for (sv_str = ++str; *sv_str != '\0' && *sv_str != '\"'; sv_str++) |
372 |
; |
373 |
*sv_str++ = '\0'; |
374 |
} else { |
375 |
for (sv_str=str ; *sv_str != '\0'; sv_str++) { |
376 |
if (_tcschr(separator, *sv_str) != NULL) { |
377 |
*sv_str++ = '\0'; |
378 |
break; |
379 |
} |
380 |
} |
381 |
} |
382 |
if (sv_str != str) |
383 |
return str; |
384 |
else |
385 |
return sv_str = NULL; |
386 |
} |
387 |
|
388 |
TCHAR *lstrstri(TCHAR *s1, TCHAR *s2) |
389 |
{ |
390 |
DWORD dwLen1= ::lstrlen(s1); |
391 |
DWORD dwLen2= ::lstrlen(s2); |
392 |
|
393 |
for (DWORD dwCnt = 0; dwCnt <= dwLen1; dwCnt++) { |
394 |
// VS2005でビルドエラーとなるため dwCnt2 宣言を追加 (2006.2.18 yutaka) |
395 |
DWORD dwCnt2; |
396 |
for (dwCnt2 = 0; dwCnt2 <= dwLen2; dwCnt2++) |
397 |
if (tolower(s1[dwCnt + dwCnt2]) != tolower(s2[dwCnt2])) |
398 |
break; |
399 |
if (dwCnt2 > dwLen2) |
400 |
return s1 + dwCnt; |
401 |
} |
402 |
|
403 |
return NULL; |
404 |
} |
405 |
|
406 |
BOOL SetForceForegroundWindow(HWND hWnd) |
407 |
{ |
408 |
#ifndef SPI_GETFOREGROUNDLOCKTIMEOUT |
409 |
#define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000 |
410 |
#define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001 |
411 |
#endif |
412 |
DWORD foreId, targId, svTmOut; |
413 |
|
414 |
foreId = ::GetWindowThreadProcessId(::GetForegroundWindow(), NULL); |
415 |
targId = ::GetWindowThreadProcessId(hWnd, NULL); |
416 |
::AttachThreadInput(targId, foreId, TRUE); |
417 |
::SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, (void *)&svTmOut, 0); |
418 |
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, 0); |
419 |
BOOL ret = ::SetForegroundWindow(hWnd); |
420 |
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (void *)svTmOut, 0); |
421 |
::AttachThreadInput(targId, foreId, FALSE); |
422 |
|
423 |
return ret; |
424 |
} |
425 |
|
426 |
void UTIL_get_lang_msg(PCHAR key, PCHAR buf, int buf_len, PCHAR def, PCHAR iniFile) |
427 |
{ |
428 |
GetI18nStr("TTMenu", key, buf, buf_len, def, iniFile); |
429 |
} |
430 |
|
431 |
int UTIL_get_lang_font(PCHAR key, HWND dlg, PLOGFONT logfont, HFONT *font, PCHAR iniFile) |
432 |
{ |
433 |
if (GetI18nLogfont("TTMenu", key, logfont, |
434 |
GetDeviceCaps(GetDC(dlg),LOGPIXELSY), |
435 |
iniFile) == FALSE) { |
436 |
return FALSE; |
437 |
} |
438 |
|
439 |
if ((*font = CreateFontIndirect(logfont)) == NULL) { |
440 |
return FALSE; |
441 |
} |
442 |
|
443 |
return TRUE; |
444 |
} |