OSDN Git Service

[Fix] Windows版でマクロ実行中は画面をマウス選択不可に制限する
[hengbandforosx/hengbandosx.git] / src / main-win.cpp
1 /*!
2  * @file main-win.cpp
3  * @brief Windows版固有実装(メインエントリポイント含む)
4  * @date 2018/03/16
5  * @author Hengband Team
6  * @details
7  *
8  * <h3>概要</h3>
9  * Windows98かその前後の頃を起点としたAPI実装。
10  * 各種のゲームエンジンは無論、
11  * DirectXといった昨今描画に標準的となったライブラリも用いていない。
12  * タイルの描画処理などについては、現在動作の詳細を検証中。
13  *
14  * <h3>フォーク元の概要</h3>
15  * <p>
16  * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
17  *
18  * This software may be copied and distributed for educational, research,
19  * and not for profit purposes provided that this copyright and statement
20  * are included in all such copies.
21  * </p>
22  * <p>
23  * This file helps Angband work with Windows computers.
24  *
25  * To use this file, use an appropriate "Makefile" or "Project File",
26  * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
27  * make sure to obtain various extra files as described below.
28  *
29  * The official compilation uses the CodeWarrior Pro compiler, which
30  * includes a special project file and precompilable header file.
31  * </p>
32  *
33  * <p>
34  * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
35  * and/or color redefinitions.
36  * </p>
37  *
38  * <p>
39  * The "lib/user/font-win.prf" contains attr/char mappings for wall.bmp.
40  * </p>
41  *
42  * <p>
43  * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
44  * special bitmap files in "lib/xtra/graf", which are activated by a menu
45  * item.
46  * </p>
47  *
48  * <p>
49  * Compiling this file, and using the resulting executable, requires
50  * several extra files not distributed with the standard Angband code.
51  * All of these extra files can be found in the "ext-win" archive.
52  * </p>
53  *
54  * <p>
55  * The "term_xtra_win_clear()" function should probably do a low-level
56  * clear of the current window, and redraw the borders and other things,
57  * if only for efficiency.
58  * </p>
59  *
60  * <p>
61  * A simpler method is needed for selecting the "tile size" for windows.
62  * </p>
63  *
64  * <p>
65  * The various "warning" messages assume the existance of the "screen.w"
66  * window, I think, and only a few calls actually check for its existance,
67  * this may be okay since "NULL" means "on top of all windows". (?)  The
68  * user must never be allowed to "hide" the main window, or the "menubar"
69  * will disappear.
70  * </p>
71  *
72  * <p>
73  * Initial framework (and most code) by Ben Harrison (benh@phial.com).
74  *
75  * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
76  *
77  * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
78  * and Chris R. Martin (crm7479@tam2000.tamu.edu).
79  * </p>
80  */
81
82 #ifdef WINDOWS
83
84 #include "cmd-io/cmd-save.h"
85 #include "cmd-visual/cmd-draw.h"
86 #include "core/game-play.h"
87 #include "core/player-processor.h"
88 #include "core/score-util.h"
89 #include "core/scores.h"
90 #include "core/special-internal-keys.h"
91 #include "core/stuff-handler.h"
92 #include "core/visuals-reseter.h"
93 #include "floor/floor-events.h"
94 #include "game-option/runtime-arguments.h"
95 #include "game-option/special-options.h"
96 #include "io/files-util.h"
97 #include "io/input-key-acceptor.h"
98 #include "io/record-play-movie.h"
99 #include "io/signal-handlers.h"
100 #include "io/write-diary.h"
101 #include "main-win/commandline-win.h"
102 #include "main-win/graphics-win.h"
103 #include "main-win/main-win-bg.h"
104 #include "main-win/main-win-file-utils.h"
105 #include "main-win/main-win-mci.h"
106 #include "main-win/main-win-menuitem.h"
107 #include "main-win/main-win-music.h"
108 #include "main-win/main-win-sound.h"
109 #include "main-win/main-win-utils.h"
110 #include "main/angband-initializer.h"
111 #include "main/sound-of-music.h"
112 #include "monster-floor/monster-lite.h"
113 #include "save/save.h"
114 #include "system/angband.h"
115 #include "system/player-type-definition.h"
116 #include "system/system-variables.h"
117 #include "term/gameterm.h"
118 #include "term/screen-processor.h"
119 #include "term/term-color-types.h"
120 #include "util/angband-files.h"
121 #include "util/int-char-converter.h"
122 #include "util/string-processor.h"
123 #include "view/display-messages.h"
124 #include "view/display-scores.h"
125 #include "wizard/spoiler-util.h"
126 #include "wizard/wizard-spoiler.h"
127 #include "world/world.h"
128
129 #include <cstdlib>
130 #include <locale>
131 #include <string>
132
133 #include <commdlg.h>
134 #include <direct.h>
135
136 /*
137  * Window names
138  */
139 LPCWSTR win_term_name[] = { L"Hengband", L"Term-1", L"Term-2", L"Term-3", L"Term-4", L"Term-5", L"Term-6", L"Term-7" };
140
141 /*!
142  * @struct term_data
143  * @brief ターム情報構造体 / Extra "term" data
144  * @details
145  * <p>
146  * pos_x / pos_y は各タームの左上点座標を指す。
147  * </p>
148  * <p>
149  * tile_wid / tile_hgt は[ウィンドウ]メニューのタイルの幅/高さを~を
150  * 1ドットずつ調整するステータスを指す。
151  * また、フォントを変更すると都度自動調整される。
152  * </p>
153  * <p>
154  * Note the use of "font_want" for the names of the font file requested by
155  * the user.
156  * </p>
157  */
158 typedef struct {
159     term_type t;
160     LPCWSTR name;
161     HWND w;
162     DWORD dwStyle;
163     DWORD dwExStyle;
164
165     TERM_LEN rows; /* int -> uint */
166     TERM_LEN cols;
167
168     uint pos_x; //!< タームの左上X座標
169     uint pos_y; //!< タームの左上Y座標
170     uint size_wid;
171     uint size_hgt;
172     uint size_ow1;
173     uint size_oh1;
174     uint size_ow2;
175     uint size_oh2;
176
177     bool size_hack;
178     bool visible;
179     concptr font_want;
180     HFONT font_id;
181     int font_wid; //!< フォント横幅
182     int font_hgt; //!< フォント縦幅
183     int tile_wid; //!< タイル横幅
184     int tile_hgt; //!< タイル縦幅
185
186     LOGFONTW lf;
187
188     bool posfix;
189 } term_data;
190
191 #define MAX_TERM_DATA 8 //!< Maximum number of windows
192
193 static term_data data[MAX_TERM_DATA]; //!< An array of term_data's
194 static term_data *my_td; //!< Hack -- global "window creation" pointer
195 POINT normsize; //!< Remember normal size of main window when maxmized
196
197 /*
198  * was main window maximized on previous playing
199  */
200 bool win_maximized = FALSE;
201
202 /*
203  * game in progress
204  */
205 bool game_in_progress = false;
206
207 /*
208  * movie in progress
209  */
210 bool movie_in_progress = false;
211
212 /*
213  * note when "open"/"new" become valid
214  */
215 bool initialized = FALSE;
216
217 /*
218  * Saved instance handle
219  */
220 static HINSTANCE hInstance;
221
222 /*
223  * Yellow brush for the cursor
224  */
225 static HBRUSH hbrYellow;
226
227 /*
228  * An icon
229  */
230 static HICON hIcon;
231
232 /* bg */
233 bg_mode current_bg_mode = bg_mode::BG_NONE;
234 #define DEFAULT_BG_FILENAME "bg.bmp"
235 char wallpaper_file[MAIN_WIN_MAX_PATH] = ""; //!< 壁紙ファイル名。
236
237 /*
238  * Show sub-windows even when Hengband is not in focus
239  */
240 static bool keep_subwindows = TRUE;
241
242 /*
243  * Full path to ANGBAND.INI
244  */
245 static concptr ini_file = NULL;
246
247 /*
248  * Name of application
249  */
250 static LPCWSTR AppName = L"ANGBAND";
251
252 /*
253  * Name of sub-window type
254  */
255 static LPCWSTR AngList = L"AngList";
256
257 /*
258  * The "complex" color values
259  */
260 static COLORREF win_clr[256];
261
262 /*
263  * Flag for macro trigger with dump ASCII
264  */
265 static bool term_no_press = FALSE;
266
267 /*
268  * Copy and paste
269  */
270 static bool mouse_down = FALSE;
271 static bool paint_rect = FALSE;
272 static TERM_LEN mousex = 0, mousey = 0;
273 static TERM_LEN oldx, oldy;
274
275 /*
276  * Hack -- define which keys are "special"
277  */
278 static bool special_key[256];
279 static bool ignore_key[256];
280
281 /*
282  * Hack -- initialization list for "special_key"
283  */
284 static byte special_key_list[] = {
285     VK_CLEAR, VK_PAUSE, VK_CAPITAL, VK_KANA, VK_JUNJA, VK_FINAL, VK_KANJI, VK_CONVERT, VK_NONCONVERT, VK_ACCEPT, VK_MODECHANGE, VK_PRIOR, VK_NEXT, VK_END,
286     VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_SELECT, VK_PRINT, VK_EXECUTE, VK_SNAPSHOT, VK_INSERT, VK_DELETE, VK_HELP, VK_APPS, VK_NUMPAD0, VK_NUMPAD1,
287     VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT, VK_DECIMAL,
288     VK_DIVIDE, VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19,
289     VK_F20, VK_F21, VK_F22, VK_F23, VK_F24, VK_NUMLOCK, VK_SCROLL, VK_ATTN, VK_CRSEL, VK_EXSEL, VK_EREOF, VK_PLAY, VK_ZOOM, VK_NONAME, VK_PA1,
290     0 /* End of List */
291 };
292
293 static byte ignore_key_list[] = {
294     VK_ESCAPE, VK_TAB, VK_SPACE, 'F', 'W', 'O', /*'H',*/ /* these are menu characters.*/
295     VK_SHIFT, VK_CONTROL, VK_MENU, VK_LWIN, VK_RWIN, VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, 0 /* End of List */
296 };
297
298 /*!
299  * @brief Validate a file
300  */
301 static void validate_file(concptr s)
302 {
303     if (check_file(s))
304         return;
305
306     quit_fmt(_("必要なファイル[%s]が見あたりません。", "Cannot find required file:\n%s"), s);
307 }
308
309 /*!
310  * @brief Validate a directory
311  */
312 static void validate_dir(concptr s, bool vital)
313 {
314     if (check_dir(s))
315         return;
316
317     if (vital) {
318         quit_fmt(_("必要なディレクトリ[%s]が見あたりません。", "Cannot find required directory:\n%s"), s);
319     } else if (mkdir(s)) {
320         quit_fmt("Unable to create directory:\n%s", s);
321     }
322 }
323
324 /*!
325  * @brief (Windows版固有実装)Get the "size" for a window
326  */
327 static void term_getsize(term_data *td)
328 {
329     if (td->cols < 1)
330         td->cols = 1;
331     if (td->rows < 1)
332         td->rows = 1;
333
334     TERM_LEN wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
335     TERM_LEN hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
336
337     RECT rw, rc;
338     if (td->w) {
339         GetWindowRect(td->w, &rw);
340         GetClientRect(td->w, &rc);
341
342         td->size_wid = (rw.right - rw.left) - (rc.right - rc.left) + wid;
343         td->size_hgt = (rw.bottom - rw.top) - (rc.bottom - rc.top) + hgt;
344
345         td->pos_x = rw.left;
346         td->pos_y = rw.top;
347     } else {
348         /* Tempolary calculation */
349         rc.left = 0;
350         rc.right = wid;
351         rc.top = 0;
352         rc.bottom = hgt;
353         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
354         td->size_wid = rc.right - rc.left;
355         td->size_hgt = rc.bottom - rc.top;
356     }
357 }
358
359 /*!
360  * @brief Write the "prefs" for a single term
361  */
362 static void save_prefs_aux(int i)
363 {
364     term_data *td = &data[i];
365     GAME_TEXT sec_name[128];
366     char buf[1024];
367
368     if (!td->w)
369         return;
370
371     wsprintfA(sec_name, "Term-%d", i);
372
373     if (i > 0) {
374         strcpy(buf, td->visible ? "1" : "0");
375         WritePrivateProfileStringA(sec_name, "Visible", buf, ini_file);
376     }
377
378     auto pwchar = td->lf.lfFaceName[0] != '\0' ? td->lf.lfFaceName : _(L"MS ゴシック", L"Courier");
379     WritePrivateProfileStringA(sec_name, "Font", to_multibyte(pwchar).c_str(), ini_file);
380
381     wsprintfA(buf, "%d", td->lf.lfWidth);
382     WritePrivateProfileStringA(sec_name, "FontWid", buf, ini_file);
383     wsprintfA(buf, "%d", td->lf.lfHeight);
384     WritePrivateProfileStringA(sec_name, "FontHgt", buf, ini_file);
385     wsprintfA(buf, "%d", td->lf.lfWeight);
386     WritePrivateProfileStringA(sec_name, "FontWgt", buf, ini_file);
387
388     wsprintfA(buf, "%d", td->tile_wid);
389     WritePrivateProfileStringA(sec_name, "TileWid", buf, ini_file);
390
391     wsprintfA(buf, "%d", td->tile_hgt);
392     WritePrivateProfileStringA(sec_name, "TileHgt", buf, ini_file);
393
394     WINDOWPLACEMENT lpwndpl;
395     lpwndpl.length = sizeof(WINDOWPLACEMENT);
396     GetWindowPlacement(td->w, &lpwndpl);
397
398     RECT rc = lpwndpl.rcNormalPosition;
399     if (i == 0)
400         wsprintfA(buf, "%d", normsize.x);
401     else
402         wsprintfA(buf, "%d", td->cols);
403
404     WritePrivateProfileStringA(sec_name, "NumCols", buf, ini_file);
405
406     if (i == 0)
407         wsprintfA(buf, "%d", normsize.y);
408     else
409         wsprintfA(buf, "%d", td->rows);
410
411     WritePrivateProfileStringA(sec_name, "NumRows", buf, ini_file);
412     if (i == 0) {
413         strcpy(buf, IsZoomed(td->w) ? "1" : "0");
414         WritePrivateProfileStringA(sec_name, "Maximized", buf, ini_file);
415     }
416
417     GetWindowRect(td->w, &rc);
418     wsprintfA(buf, "%d", rc.left);
419     WritePrivateProfileStringA(sec_name, "PositionX", buf, ini_file);
420
421     wsprintfA(buf, "%d", rc.top);
422     WritePrivateProfileStringA(sec_name, "PositionY", buf, ini_file);
423     if (i > 0) {
424         strcpy(buf, td->posfix ? "1" : "0");
425         WritePrivateProfileStringA(sec_name, "PositionFix", buf, ini_file);
426     }
427 }
428
429 /*!
430  * @brief Write the "prefs"
431  * We assume that the windows have all been initialized
432  */
433 static void save_prefs(void)
434 {
435     char buf[128];
436     wsprintfA(buf, "%d", arg_graphics);
437     WritePrivateProfileStringA("Angband", "Graphics", buf, ini_file);
438
439     strcpy(buf, arg_bigtile ? "1" : "0");
440     WritePrivateProfileStringA("Angband", "Bigtile", buf, ini_file);
441
442     strcpy(buf, arg_sound ? "1" : "0");
443     WritePrivateProfileStringA("Angband", "Sound", buf, ini_file);
444
445     strcpy(buf, arg_music ? "1" : "0");
446     WritePrivateProfileStringA("Angband", "Music", buf, ini_file);
447     strcpy(buf, use_pause_music_inactive ? "1" : "0");
448     WritePrivateProfileStringA("Angband", "MusicPauseInactive", buf, ini_file);
449
450     wsprintfA(buf, "%d", current_bg_mode);
451     WritePrivateProfileStringA("Angband", "BackGround", buf, ini_file);
452     WritePrivateProfileStringA("Angband", "BackGroundBitmap", wallpaper_file[0] != '\0' ? wallpaper_file : DEFAULT_BG_FILENAME, ini_file);
453
454     int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
455     char tmp[1024] = "";
456     strncat(tmp, ANGBAND_DIR, path_length);
457
458     int n = strncmp(tmp, savefile, path_length);
459     if (n == 0) {
460         char relative_path[1024] = "";
461         snprintf(relative_path, sizeof(relative_path), ".\\%s", (savefile + path_length));
462         WritePrivateProfileStringA("Angband", "SaveFile", relative_path, ini_file);
463     } else {
464         WritePrivateProfileStringA("Angband", "SaveFile", savefile, ini_file);
465     }
466
467     strcpy(buf, keep_subwindows ? "1" : "0");
468     WritePrivateProfileStringA("Angband", "KeepSubwindows", buf, ini_file);
469
470     for (int i = 0; i < MAX_TERM_DATA; ++i) {
471         save_prefs_aux(i);
472     }
473 }
474
475 /*!
476  * @brief callback for EnumDisplayMonitors API
477  */
478 BOOL CALLBACK monitorenumproc([[maybe_unused]] HMONITOR hMon, [[maybe_unused]] HDC hdcMon, [[maybe_unused]] LPRECT lpMon, LPARAM dwDate)
479 {
480     bool *result = (bool *)dwDate;
481     *result = true;
482     return FALSE;
483 }
484
485 /*!
486  * @brief Load the "prefs" for a single term
487  */
488 static void load_prefs_aux(int i)
489 {
490     term_data *td = &data[i];
491     GAME_TEXT sec_name[128];
492     char tmp[1024];
493
494     wsprintfA(sec_name, "Term-%d", i);
495     if (i > 0) {
496         td->visible = (GetPrivateProfileIntA(sec_name, "Visible", td->visible, ini_file) != 0);
497     }
498
499     GetPrivateProfileStringA(sec_name, "Font", _("MS ゴシック", "Courier"), tmp, 127, ini_file);
500
501     td->font_want = string_make(tmp);
502     int hgt = 15;
503     int wid = 0;
504     td->lf.lfWidth = GetPrivateProfileIntA(sec_name, "FontWid", wid, ini_file);
505     td->lf.lfHeight = GetPrivateProfileIntA(sec_name, "FontHgt", hgt, ini_file);
506     td->lf.lfWeight = GetPrivateProfileIntA(sec_name, "FontWgt", 0, ini_file);
507
508     td->tile_wid = GetPrivateProfileIntA(sec_name, "TileWid", td->lf.lfWidth, ini_file);
509     td->tile_hgt = GetPrivateProfileIntA(sec_name, "TileHgt", td->lf.lfHeight, ini_file);
510
511     td->cols = GetPrivateProfileIntA(sec_name, "NumCols", td->cols, ini_file);
512     td->rows = GetPrivateProfileIntA(sec_name, "NumRows", td->rows, ini_file);
513     normsize.x = td->cols;
514     normsize.y = td->rows;
515
516     if (i == 0) {
517         win_maximized = (GetPrivateProfileIntA(sec_name, "Maximized", win_maximized, ini_file) != 0);
518     }
519
520     int posx = GetPrivateProfileIntA(sec_name, "PositionX", 0, ini_file);
521     int posy = GetPrivateProfileIntA(sec_name, "PositionY", 0, ini_file);
522     // 保存座標がモニタ内の領域にあるかチェック
523     RECT rect = { posx, posy, posx + 128, posy + 128 };
524     bool in_any_monitor = false;
525     ::EnumDisplayMonitors(NULL, &rect, monitorenumproc, (LPARAM)&in_any_monitor);
526     if (in_any_monitor) {
527         // いずれかのモニタに表示可能、ウインドウ位置を復元
528         td->pos_x = posx;
529         td->pos_y = posy;
530     }
531
532     if (i > 0) {
533         td->posfix = (GetPrivateProfileIntA(sec_name, "PositionFix", td->posfix, ini_file) != 0);
534     }
535 }
536
537 /*!
538  * @brief Load the "prefs"
539  */
540 static void load_prefs(void)
541 {
542     arg_graphics = (byte)GetPrivateProfileIntA("Angband", "Graphics", static_cast<byte>(graphics_mode::GRAPHICS_NONE), ini_file);
543     arg_bigtile = (GetPrivateProfileIntA("Angband", "Bigtile", FALSE, ini_file) != 0);
544     use_bigtile = arg_bigtile;
545     arg_sound = (GetPrivateProfileIntA("Angband", "Sound", 0, ini_file) != 0);
546     arg_music = (GetPrivateProfileIntA("Angband", "Music", 0, ini_file) != 0);
547     use_pause_music_inactive = (GetPrivateProfileIntA("Angband", "MusicPauseInactive", 0, ini_file) != 0);
548     current_bg_mode = static_cast<bg_mode>(GetPrivateProfileIntA("Angband", "BackGround", 0, ini_file));
549     GetPrivateProfileStringA("Angband", "BackGroundBitmap", DEFAULT_BG_FILENAME, wallpaper_file, 1023, ini_file);
550     GetPrivateProfileStringA("Angband", "SaveFile", "", savefile, 1023, ini_file);
551
552     int n = strncmp(".\\", savefile, 2);
553     if (n == 0) {
554         int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
555         char tmp[1024] = "";
556         strncat(tmp, ANGBAND_DIR, path_length);
557         strncat(tmp, savefile + 2, strlen(savefile) - 2 + path_length);
558         strncpy(savefile, tmp, strlen(tmp));
559     }
560
561     keep_subwindows = (GetPrivateProfileIntA("Angband", "KeepSubwindows", 0, ini_file) != 0);
562     for (int i = 0; i < MAX_TERM_DATA; ++i) {
563         load_prefs_aux(i);
564     }
565 }
566
567 /*!
568  * @brief Initialize music
569  */
570 static void init_music(void)
571 {
572     // Flag set once "music" has been initialized
573     static bool can_use_music = FALSE;
574
575     if (!can_use_music) {
576         main_win_music::load_music_prefs();
577         can_use_music = TRUE;
578     }
579 }
580
581 /*!
582  * @brief Initialize sound
583  */
584 static void init_sound(void)
585 {
586     // Flag set once "sound" has been initialized
587     static bool can_use_sound = FALSE;
588
589     if (!can_use_sound) {
590         load_sound_prefs();
591         can_use_sound = TRUE;
592     }
593 }
594
595 /*!
596  * @brief Change sound mode
597  * @param new_mode bool
598  */
599 static void change_sound_mode(bool new_mode)
600 {
601     use_sound = new_mode;
602     if (use_sound) {
603         init_sound();
604     }
605 }
606
607 /*!
608  * @brief Initialize background
609  */
610 static void init_background(void)
611 {
612     // Flag set once "background" has been initialized
613     static bool can_use_background = FALSE;
614
615     if (!can_use_background) {
616         load_bg_prefs();
617         can_use_background = TRUE;
618     }
619 }
620
621 /*!
622  * @brief Change background mode
623  * @param new_mode bg_mode
624  * @param show_error trueに設定した場合のみ、エラーダイアログを表示する
625  * @param force_redraw trueの場合、モード変更に関わらずウインドウを再描画する
626  * @retval true success
627  * @retval false failed
628  */
629 static bool change_bg_mode(bg_mode new_mode, bool show_error = false, bool force_redraw = false)
630 {
631     bg_mode old_bg_mode = current_bg_mode;
632     current_bg_mode = new_mode;
633     if (current_bg_mode != bg_mode::BG_NONE) {
634         init_background();
635         if (!load_bg(wallpaper_file)) {
636             current_bg_mode = bg_mode::BG_NONE;
637             if (show_error)
638                 plog_fmt(_("壁紙用ファイル '%s' を読み込めません。", "Can't load the image file '%s'."), wallpaper_file);
639         }
640     } else {
641         delete_bg();
642     }
643
644     const bool mode_changed = (current_bg_mode != old_bg_mode);
645     if (mode_changed || force_redraw) {
646         // 全ウインドウ再描画
647         for (int i = 0; i < MAX_TERM_DATA; i++) {
648             term_data *td = &data[i];
649             if (td->visible)
650                 InvalidateRect(td->w, NULL, FALSE);
651         }
652     }
653
654     return (current_bg_mode == new_mode);
655 }
656
657 /*!
658  * @brief Resize a window
659  */
660 static void term_window_resize(term_data *td)
661 {
662     if (!td->w)
663         return;
664
665     SetWindowPos(td->w, 0, 0, 0, td->size_wid, td->size_hgt, SWP_NOMOVE | SWP_NOZORDER);
666     InvalidateRect(td->w, NULL, TRUE);
667 }
668
669 /*!
670  * @brief Force the use of a new font for a term_data.
671  * This function may be called before the "window" is ready.
672  * This function returns zero only if everything succeeds.
673  * @note that the "font name" must be capitalized!!!
674  */
675 static errr term_force_font(term_data *td)
676 {
677     if (td->font_id)
678         DeleteObject(td->font_id);
679
680     td->font_id = CreateFontIndirectW(&(td->lf));
681     int wid = td->lf.lfWidth;
682     int hgt = td->lf.lfHeight;
683     if (!td->font_id)
684         return 1;
685
686     if (!wid || !hgt) {
687         HDC hdcDesktop;
688         HFONT hfOld;
689         TEXTMETRIC tm;
690
691         hdcDesktop = GetDC(HWND_DESKTOP);
692         hfOld = static_cast<HFONT>(SelectObject(hdcDesktop, td->font_id));
693         GetTextMetrics(hdcDesktop, &tm);
694         SelectObject(hdcDesktop, hfOld);
695         ReleaseDC(HWND_DESKTOP, hdcDesktop);
696
697         wid = tm.tmAveCharWidth;
698         hgt = tm.tmHeight;
699     }
700
701     td->font_wid = wid;
702     td->font_hgt = hgt;
703
704     return 0;
705 }
706
707 /*!
708  * @brief Allow the user to change the font for this window.
709  */
710 static void term_change_font(term_data *td)
711 {
712     CHOOSEFONTW cf;
713     memset(&cf, 0, sizeof(cf));
714     cf.lStructSize = sizeof(cf);
715     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
716     cf.lpLogFont = &(td->lf);
717
718     if (!ChooseFontW(&cf))
719         return;
720
721     term_force_font(td);
722     td->tile_wid = td->font_wid;
723     td->tile_hgt = td->font_hgt;
724     term_getsize(td);
725     term_window_resize(td);
726 }
727
728 /*!
729  * @brief Allow the user to lock this window.
730  */
731 static void term_window_pos(term_data *td, HWND hWnd)
732 {
733     SetWindowPos(td->w, hWnd, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
734 }
735
736 /*!
737  * @brief Hack -- redraw a term_data
738  */
739 static void term_data_redraw(term_data *td)
740 {
741     term_activate(&td->t);
742     term_redraw();
743     term_activate(term_screen);
744 }
745
746 /*!
747  * @brief termの反転色表示
748  */
749 void term_inversed_area(HWND hWnd, int x, int y, int w, int h)
750 {
751     term_data *td = (term_data *)GetWindowLong(hWnd, 0);
752     int tx = td->size_ow1 + x * td->tile_wid;
753     int ty = td->size_oh1 + y * td->tile_hgt;
754     int tw = w * td->tile_wid - 1;
755     int th = h * td->tile_hgt - 1;
756
757     HDC hdc = GetDC(hWnd);
758     HBRUSH myBrush = CreateSolidBrush(RGB(255, 255, 255));
759     HBRUSH oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
760     HPEN oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
761
762     PatBlt(hdc, tx, ty, tw, th, PATINVERT);
763
764     SelectObject(hdc, oldBrush);
765     SelectObject(hdc, oldPen);
766 }
767
768 /*!
769  * @brief Windows版ユーザ設定項目実装部(実装必須) /Interact with the User
770  */
771 static errr term_user_win(int n)
772 {
773     (void)n;
774     return 0;
775 }
776
777 /*!
778  * @brief カラーパレットの変更?
779  */
780 static void refresh_color_table()
781 {
782     for (int i = 0; i < 256; i++) {
783         byte rv = angband_color_table[i][1];
784         byte gv = angband_color_table[i][2];
785         byte bv = angband_color_table[i][3];
786         win_clr[i] = PALETTERGB(rv, gv, bv);
787     }
788 }
789
790 /*!
791  * @brief React to global changes
792  */
793 static errr term_xtra_win_react(player_type *player_ptr)
794 {
795     refresh_color_table();
796
797     const byte current_mode = static_cast<byte>(graphic.get_mode());
798     if (current_mode != arg_graphics) {
799         const byte old_graphics = arg_graphics;
800         arg_graphics = static_cast<byte>(graphic.change_graphics(static_cast<graphics_mode>(arg_graphics)));
801         if (old_graphics != arg_graphics) {
802             plog(_("グラフィクスを初期化できません!", "Cannot initialize graphics!"));
803         }
804         use_graphics = (arg_graphics > 0);
805         reset_visuals(player_ptr);
806     }
807
808     for (int i = 0; i < MAX_TERM_DATA; i++) {
809         term_type *old = Term;
810         term_data *td = &data[i];
811         if ((td->cols != td->t.wid) || (td->rows != td->t.hgt)) {
812             term_activate(&td->t);
813             term_resize(td->cols, td->rows);
814             term_redraw();
815             term_activate(old);
816         }
817     }
818
819     return 0;
820 }
821
822 /*!
823  * @brief Process at least one event
824  */
825 static errr term_xtra_win_event(int v)
826 {
827     MSG msg;
828     if (v) {
829         if (GetMessage(&msg, NULL, 0, 0)) {
830             TranslateMessage(&msg);
831             DispatchMessage(&msg);
832         }
833     } else {
834         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
835             TranslateMessage(&msg);
836             DispatchMessage(&msg);
837         }
838     }
839
840     return 0;
841 }
842
843 /*!
844  * @brief Process all pending events
845  */
846 static errr term_xtra_win_flush(void)
847 {
848     MSG msg;
849     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
850         TranslateMessage(&msg);
851         DispatchMessage(&msg);
852     }
853
854     return 0;
855 }
856
857 /*!
858  * @brief Hack -- clear the screen
859  * @details
860  * Make this more efficient
861  */
862 static errr term_xtra_win_clear(void)
863 {
864     term_data *td = (term_data *)(Term->data);
865
866     RECT rc;
867     GetClientRect(td->w, &rc);
868
869     HDC hdc = GetDC(td->w);
870     SetBkColor(hdc, RGB(0, 0, 0));
871     SelectObject(hdc, td->font_id);
872     ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
873
874     if (current_bg_mode != bg_mode::BG_NONE) {
875         rc.left = 0;
876         rc.top = 0;
877         draw_bg(hdc, &rc);
878     }
879
880     ReleaseDC(td->w, hdc);
881     return 0;
882 }
883
884 /*!
885  * @brief Hack -- make a noise
886  */
887 static errr term_xtra_win_noise(void)
888 {
889     MessageBeep(MB_ICONASTERISK);
890     return 0;
891 }
892
893 /*!
894  * @brief Hack -- make a sound
895  */
896 static errr term_xtra_win_sound(int v)
897 {
898     if (!use_sound)
899         return 1;
900     return play_sound(v);
901 }
902
903 /*!
904  * @brief Hack -- play a music
905  */
906 static errr term_xtra_win_music(int n, int v)
907 {
908     if (!use_music) {
909         return 1;
910     }
911
912     return main_win_music::play_music(n, v);
913 }
914
915 /*!
916  * @brief Hack -- play a music matches a situation
917  */
918 static errr term_xtra_win_scene(int v)
919 {
920     // TODO 場面に合った壁紙変更対応
921     if (!use_music) {
922         return 1;
923     }
924
925     return main_win_music::play_music_scene(v);
926 }
927
928 /*!
929  * @brief Delay for "x" milliseconds
930  */
931 static int term_xtra_win_delay(int v)
932 {
933     Sleep(v);
934     return 0;
935 }
936
937 /*!
938  * @brief Do a "special thing"
939  * @todo z-termに影響があるのでplayer_typeの追加は保留
940  */
941 static errr term_xtra_win(int n, int v)
942 {
943     switch (n) {
944     case TERM_XTRA_NOISE: {
945         return (term_xtra_win_noise());
946     }
947     case TERM_XTRA_MUSIC_BASIC:
948     case TERM_XTRA_MUSIC_DUNGEON:
949     case TERM_XTRA_MUSIC_QUEST:
950     case TERM_XTRA_MUSIC_TOWN:
951     case TERM_XTRA_MUSIC_MONSTER: {
952         return term_xtra_win_music(n, v);
953     }
954     case TERM_XTRA_MUSIC_MUTE: {
955         return main_win_music::stop_music();
956     }
957     case TERM_XTRA_SCENE: {
958         return term_xtra_win_scene(v);
959     }
960     case TERM_XTRA_SOUND: {
961         return (term_xtra_win_sound(v));
962     }
963     case TERM_XTRA_BORED: {
964         return (term_xtra_win_event(0));
965     }
966     case TERM_XTRA_EVENT: {
967         return (term_xtra_win_event(v));
968     }
969     case TERM_XTRA_FLUSH: {
970         return (term_xtra_win_flush());
971     }
972     case TERM_XTRA_CLEAR: {
973         return (term_xtra_win_clear());
974     }
975     case TERM_XTRA_REACT: {
976         return (term_xtra_win_react(p_ptr));
977     }
978     case TERM_XTRA_DELAY: {
979         return (term_xtra_win_delay(v));
980     }
981     }
982
983     return 1;
984 }
985
986 /*!
987  * @brief Low level graphics (Assumes valid input).
988  * @details
989  * Draw a "cursor" at (x,y), using a "yellow box".
990  */
991 static errr term_curs_win(int x, int y)
992 {
993     term_data *td = (term_data *)(Term->data);
994     int tile_wid, tile_hgt;
995     tile_wid = td->tile_wid;
996     tile_hgt = td->tile_hgt;
997
998     RECT rc;
999     rc.left = x * tile_wid + td->size_ow1;
1000     rc.right = rc.left + tile_wid;
1001     rc.top = y * tile_hgt + td->size_oh1;
1002     rc.bottom = rc.top + tile_hgt;
1003
1004     HDC hdc = GetDC(td->w);
1005     FrameRect(hdc, &rc, hbrYellow);
1006     ReleaseDC(td->w, hdc);
1007     return 0;
1008 }
1009
1010 /*!
1011  * @brief Low level graphics (Assumes valid input).
1012  * @details
1013  * Draw a "big cursor" at (x,y), using a "yellow box".
1014  */
1015 static errr term_bigcurs_win(int x, int y)
1016 {
1017     term_data *td = (term_data *)(Term->data);
1018     int tile_wid, tile_hgt;
1019     tile_wid = td->tile_wid;
1020     tile_hgt = td->tile_hgt;
1021
1022     RECT rc;
1023     rc.left = x * tile_wid + td->size_ow1;
1024     rc.right = rc.left + 2 * tile_wid;
1025     rc.top = y * tile_hgt + td->size_oh1;
1026     rc.bottom = rc.top + tile_hgt;
1027
1028     HDC hdc = GetDC(td->w);
1029     FrameRect(hdc, &rc, hbrYellow);
1030     ReleaseDC(td->w, hdc);
1031     return 0;
1032 }
1033
1034 /*!
1035  * @brief Low level graphics (Assumes valid input).
1036  * @details
1037  * Erase a "block" of "n" characters starting at (x,y).
1038  */
1039 static errr term_wipe_win(int x, int y, int n)
1040 {
1041     term_data *td = (term_data *)(Term->data);
1042     RECT rc;
1043     rc.left = x * td->tile_wid + td->size_ow1;
1044     rc.right = rc.left + n * td->tile_wid;
1045     rc.top = y * td->tile_hgt + td->size_oh1;
1046     rc.bottom = rc.top + td->tile_hgt;
1047
1048     HDC hdc = GetDC(td->w);
1049     SetBkColor(hdc, RGB(0, 0, 0));
1050     SelectObject(hdc, td->font_id);
1051     if (current_bg_mode != bg_mode::BG_NONE)
1052         draw_bg(hdc, &rc);
1053     else
1054         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1055
1056     ReleaseDC(td->w, hdc);
1057     return 0;
1058 }
1059
1060 /*!
1061  * @brief Low level graphics.  Assumes valid input.
1062  * @details
1063  * Draw several ("n") chars, with an attr, at a given location.
1064  *
1065  * All "graphic" data is handled by "term_pict_win()", below.
1066  *
1067  * One would think there is a more efficient method for telling a window
1068  * what color it should be using to draw with, but perhaps simply changing
1069  * it every time is not too inefficient.
1070  */
1071 static errr term_text_win(int x, int y, int n, TERM_COLOR a, concptr s)
1072 {
1073     term_data *td = (term_data *)(Term->data);
1074     static HBITMAP WALL;
1075     static HBRUSH myBrush, oldBrush;
1076     static HPEN oldPen;
1077     static bool init_done = FALSE;
1078
1079     if (!init_done) {
1080         WALL = LoadBitmapW(hInstance, AppName);
1081         myBrush = CreatePatternBrush(WALL);
1082         init_done = TRUE;
1083     }
1084
1085     RECT rc{ static_cast<LONG>(x * td->tile_wid + td->size_ow1), static_cast<LONG>(y * td->tile_hgt + td->size_oh1),
1086         static_cast<LONG>(rc.left + n * td->tile_wid), static_cast<LONG>(rc.top + td->tile_hgt) };
1087
1088     HDC hdc = GetDC(td->w);
1089     SetBkColor(hdc, RGB(0, 0, 0));
1090     SetTextColor(hdc, win_clr[a]);
1091
1092     SelectObject(hdc, td->font_id);
1093     if (current_bg_mode != bg_mode::BG_NONE)
1094         SetBkMode(hdc, TRANSPARENT);
1095
1096     ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1097     if (current_bg_mode != bg_mode::BG_NONE)
1098         draw_bg(hdc, &rc);
1099
1100     rc.left += ((td->tile_wid - td->font_wid) / 2);
1101     rc.right = rc.left + td->font_wid;
1102     rc.top += ((td->tile_hgt - td->font_hgt) / 2);
1103     rc.bottom = rc.top + td->font_hgt;
1104
1105     for (int i = 0; i < n; i++) {
1106 #ifdef JP
1107         if (use_bigtile && *(s + i) == "■"[0] && *(s + i + 1) == "■"[1]) {
1108             rc.right += td->font_wid;
1109             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1110             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1111             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1112             SelectObject(hdc, oldBrush);
1113             SelectObject(hdc, oldPen);
1114             rc.right -= td->font_wid;
1115             i++;
1116             rc.left += 2 * td->tile_wid;
1117             rc.right += 2 * td->tile_wid;
1118         } else if (iskanji(*(s + i))) /* 2バイト文字 */
1119         {
1120             const auto *buf = to_wchar(s + i).wc_str();
1121             rc.right += td->font_wid;
1122             if (buf == NULL)
1123                 ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 2, NULL);
1124             else
1125                 ExtTextOutW(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, buf, wcslen(buf), NULL);
1126             rc.right -= td->font_wid;
1127             i++;
1128             rc.left += 2 * td->tile_wid;
1129             rc.right += 2 * td->tile_wid;
1130         } else if (*(s + i) == 127) {
1131             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1132             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1133             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1134             SelectObject(hdc, oldBrush);
1135             SelectObject(hdc, oldPen);
1136             rc.left += td->tile_wid;
1137             rc.right += td->tile_wid;
1138         } else {
1139             ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1140             rc.left += td->tile_wid;
1141             rc.right += td->tile_wid;
1142         }
1143 #else
1144         if (*(s + i) == 127) {
1145             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1146             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1147             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1148             SelectObject(hdc, oldBrush);
1149             SelectObject(hdc, oldPen);
1150             rc.left += td->tile_wid;
1151             rc.right += td->tile_wid;
1152         } else {
1153             ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1154             rc.left += td->tile_wid;
1155             rc.right += td->tile_wid;
1156         }
1157 #endif
1158     }
1159
1160     ReleaseDC(td->w, hdc);
1161     return 0;
1162 }
1163
1164 /*!
1165  * @brief Low level graphics.  Assumes valid input.
1166  * @details
1167  * Draw an array of "special" attr/char pairs at the given location.
1168  *
1169  * We use the "term_pict_win()" function for "graphic" data, which are
1170  * encoded by setting the "high-bits" of both the "attr" and the "char"
1171  * data.  We use the "attr" to represent the "row" of the main bitmap,
1172  * and the "char" to represent the "col" of the main bitmap.  The use
1173  * of this function is induced by the "higher_pict" flag.
1174  *
1175  * If "graphics" is not available, we simply "wipe" the given grids.
1176  */
1177 static errr term_pict_win(TERM_LEN x, TERM_LEN y, int n, const TERM_COLOR *ap, concptr cp, const TERM_COLOR *tap, concptr tcp)
1178 {
1179     term_data *td = (term_data *)(Term->data);
1180     int i;
1181     HDC hdcMask = NULL;
1182     if (!use_graphics) {
1183         return (term_wipe_win(x, y, n));
1184     }
1185
1186     const tile_info &infGraph = graphic.get_tile_info();
1187     const bool has_mask = (infGraph.hBitmapMask != NULL);
1188     TERM_LEN w1 = infGraph.CellWidth;
1189     TERM_LEN h1 = infGraph.CellHeight;
1190     TERM_LEN tw1 = infGraph.TileWidth;
1191     TERM_LEN th1 = infGraph.TileHeight;
1192     TERM_LEN w2, h2, tw2 = 0;
1193     w2 = td->tile_wid;
1194     h2 = td->tile_hgt;
1195     tw2 = w2;
1196     if (use_bigtile)
1197         tw2 *= 2;
1198
1199     TERM_LEN x2 = x * w2 + td->size_ow1 + infGraph.OffsetX;
1200     TERM_LEN y2 = y * h2 + td->size_oh1 + infGraph.OffsetY;
1201     HDC hdc = GetDC(td->w);
1202     HDC hdcSrc = CreateCompatibleDC(hdc);
1203     HBITMAP hbmSrcOld = static_cast<HBITMAP>(SelectObject(hdcSrc, infGraph.hBitmap));
1204
1205     if (has_mask) {
1206         hdcMask = CreateCompatibleDC(hdc);
1207         SelectObject(hdcMask, infGraph.hBitmapMask);
1208     }
1209
1210     for (i = 0; i < n; i++, x2 += w2) {
1211         TERM_COLOR a = ap[i];
1212         char c = cp[i];
1213         int row = (a & 0x7F);
1214         int col = (c & 0x7F);
1215         TERM_LEN x1 = col * w1;
1216         TERM_LEN y1 = row * h1;
1217
1218         if (has_mask) {
1219             TERM_LEN x3 = (tcp[i] & 0x7F) * w1;
1220             TERM_LEN y3 = (tap[i] & 0x7F) * h1;
1221             tw2 = tw2 * w1 / tw1;
1222             h2 = h2 * h1 / th1;
1223             if ((tw1 == tw2) && (th1 == h2)) {
1224                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
1225                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
1226                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
1227                 continue;
1228             }
1229
1230             SetStretchBltMode(hdc, COLORONCOLOR);
1231             StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x3, y3, w1, h1, SRCAND);
1232             StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCPAINT);
1233             if ((x1 != x3) || (y1 != y3)) {
1234                 StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
1235                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
1236             }
1237
1238             continue;
1239         }
1240
1241         if ((w1 == tw2) && (h1 == h2)) {
1242             BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
1243             continue;
1244         }
1245
1246         SetStretchBltMode(hdc, COLORONCOLOR);
1247         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
1248     }
1249
1250     SelectObject(hdcSrc, hbmSrcOld);
1251     DeleteDC(hdcSrc);
1252     if (has_mask) {
1253         SelectObject(hdcMask, hbmSrcOld);
1254         DeleteDC(hdcMask);
1255     }
1256
1257     ReleaseDC(td->w, hdc);
1258     return 0;
1259 }
1260
1261 /*!
1262  * @brief Create and initialize a "term_data" given a title
1263  */
1264 static void term_data_link(term_data *td)
1265 {
1266     term_type *t = &td->t;
1267     term_init(t, td->cols, td->rows, FILE_READ_BUFF_SIZE);
1268     t->soft_cursor = TRUE;
1269     t->higher_pict = TRUE;
1270     t->attr_blank = TERM_WHITE;
1271     t->char_blank = ' ';
1272     t->user_hook = term_user_win;
1273     t->xtra_hook = term_xtra_win;
1274     t->curs_hook = term_curs_win;
1275     t->bigcurs_hook = term_bigcurs_win;
1276     t->wipe_hook = term_wipe_win;
1277     t->text_hook = term_text_win;
1278     t->pict_hook = term_pict_win;
1279     t->data = (vptr)(td);
1280 }
1281
1282 /*!
1283  * @brief Create the windows
1284  * @details
1285  * First, instantiate the "default" values, then read the "ini_file"
1286  * to over-ride selected values, then create the windows, and fonts.
1287  *
1288  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
1289  * must make active to realize the palette.
1290  */
1291 static void init_windows(void)
1292 {
1293     term_data *td;
1294     td = &data[0];
1295     WIPE(td, term_data);
1296     td->name = win_term_name[0];
1297
1298     td->rows = 24;
1299     td->cols = 80;
1300     td->visible = TRUE;
1301     td->size_ow1 = 2;
1302     td->size_ow2 = 2;
1303     td->size_oh1 = 2;
1304     td->size_oh2 = 2;
1305     td->pos_x = 7 * 30;
1306     td->pos_y = 7 * 20;
1307     td->posfix = FALSE;
1308
1309     for (int i = 1; i < MAX_TERM_DATA; i++) {
1310         td = &data[i];
1311         WIPE(td, term_data);
1312         td->name = win_term_name[i];
1313         td->rows = 24;
1314         td->cols = 80;
1315         td->visible = FALSE;
1316         td->size_ow1 = 1;
1317         td->size_ow2 = 1;
1318         td->size_oh1 = 1;
1319         td->size_oh2 = 1;
1320         td->pos_x = (7 - i) * 30;
1321         td->pos_y = (7 - i) * 20;
1322         td->posfix = FALSE;
1323     }
1324
1325     load_prefs();
1326
1327     /* Atrributes of main window */
1328     td = &data[0];
1329     td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_VISIBLE);
1330     td->dwExStyle = 0;
1331     td->visible = TRUE;
1332
1333     /* Attributes of sub windows */
1334     for (int i = 1; i < MAX_TERM_DATA; i++) {
1335         td = &data[i];
1336         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
1337         td->dwExStyle = (WS_EX_TOOLWINDOW);
1338     }
1339
1340     /* Font of each window */
1341     for (int i = 0; i < MAX_TERM_DATA; i++) {
1342         td = &data[i];
1343         wcsncpy(td->lf.lfFaceName, to_wchar(td->font_want).wc_str(), LF_FACESIZE);
1344         td->lf.lfCharSet = _(SHIFTJIS_CHARSET, DEFAULT_CHARSET);
1345         td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1346         term_force_font(td);
1347         if (!td->tile_wid)
1348             td->tile_wid = td->font_wid;
1349         if (!td->tile_hgt)
1350             td->tile_hgt = td->font_hgt;
1351         term_getsize(td);
1352         term_window_resize(td);
1353     }
1354
1355     /* Create sub windows */
1356     for (int i = MAX_TERM_DATA - 1; i >= 1; --i) {
1357         td = &data[i];
1358
1359         my_td = td;
1360         td->w = CreateWindowExW(
1361             td->dwExStyle, AngList, td->name, td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
1362         my_td = NULL;
1363
1364         if (!td->w)
1365             quit(_("サブウィンドウに作成に失敗しました", "Failed to create sub-window"));
1366
1367         td->size_hack = TRUE;
1368         term_getsize(td);
1369         term_window_resize(td);
1370
1371         if (td->visible) {
1372             ShowWindow(td->w, SW_SHOW);
1373         }
1374         td->size_hack = FALSE;
1375
1376         term_data_link(td);
1377         angband_term[i] = &td->t;
1378
1379         if (td->visible) {
1380             /* Activate the window */
1381             SetActiveWindow(td->w);
1382         }
1383
1384         if (td->posfix) {
1385             term_window_pos(td, HWND_TOPMOST);
1386         } else {
1387             term_window_pos(td, td->w);
1388         }
1389     }
1390
1391     /* Create main window */
1392     td = &data[0];
1393     my_td = td;
1394     td->w = CreateWindowExW(
1395         td->dwExStyle, AppName, _(L"変愚蛮怒", td->name), td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
1396     my_td = NULL;
1397
1398     if (!td->w)
1399         quit(_("メインウィンドウの作成に失敗しました", "Failed to create Angband window"));
1400
1401     /* Resize */
1402     td->size_hack = TRUE;
1403     term_getsize(td);
1404     term_window_resize(td);
1405     td->size_hack = FALSE;
1406
1407     term_data_link(td);
1408     angband_term[0] = &td->t;
1409     normsize.x = td->cols;
1410     normsize.y = td->rows;
1411
1412     if (win_maximized)
1413         ShowWindow(td->w, SW_SHOWMAXIMIZED);
1414     else
1415         ShowWindow(td->w, SW_SHOW);
1416
1417     SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1418     hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
1419     (void)term_xtra_win_flush();
1420 }
1421
1422 /*!
1423  * @brief Prepare the menus
1424  */
1425 static void setup_menus(void)
1426 {
1427     HMENU hm = GetMenu(data[0].w);
1428
1429     if (current_world_ptr->character_generated) {
1430         EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1431         EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1432         EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_ENABLED);
1433     } else {
1434         EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
1435         EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
1436         EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1437     }
1438
1439     for (int i = 0; i < MAX_TERM_DATA; i++) {
1440         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1441         CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i, (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
1442         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_ENABLED);
1443     }
1444
1445     for (int i = 0; i < MAX_TERM_DATA; i++) {
1446         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1447
1448         if (data[i].visible) {
1449             EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1450         }
1451     }
1452
1453     for (int i = 0; i < MAX_TERM_DATA; i++) {
1454         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1455         CheckMenuItem(hm, IDM_WINDOW_POS_0 + i, (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
1456         if (data[i].visible) {
1457             EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_ENABLED);
1458         }
1459     }
1460
1461     for (int i = 0; i < MAX_TERM_DATA; i++) {
1462         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1463         if (data[i].visible) {
1464             EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
1465         }
1466     }
1467
1468     for (int i = 0; i < MAX_TERM_DATA; i++) {
1469         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1470         if (data[i].visible) {
1471             EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
1472         }
1473     }
1474
1475     for (int i = 0; i < MAX_TERM_DATA; i++) {
1476         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1477         if (data[i].visible) {
1478             EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1479         }
1480     }
1481
1482     for (int i = 0; i < MAX_TERM_DATA; i++) {
1483         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1484
1485         if (data[i].visible) {
1486             EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1487         }
1488     }
1489     CheckMenuItem(hm, IDM_WINDOW_KEEP_SUBWINDOWS, (keep_subwindows ? MF_CHECKED : MF_UNCHECKED));
1490
1491     CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_NONE) ? MF_CHECKED : MF_UNCHECKED));
1492     CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL) ? MF_CHECKED : MF_UNCHECKED));
1493     CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT) ? MF_CHECKED : MF_UNCHECKED));
1494     CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND) ? MF_CHECKED : MF_UNCHECKED));
1495     CheckMenuItem(hm, IDM_OPTIONS_BIGTILE, (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
1496     CheckMenuItem(hm, IDM_OPTIONS_MUSIC, (arg_music ? MF_CHECKED : MF_UNCHECKED));
1497     CheckMenuItem(hm, IDM_OPTIONS_MUSIC_PAUSE_INACTIVE, (use_pause_music_inactive ? MF_CHECKED : MF_UNCHECKED));
1498     CheckMenuItem(hm, IDM_OPTIONS_SOUND, (arg_sound ? MF_CHECKED : MF_UNCHECKED));
1499     CheckMenuItem(hm, IDM_OPTIONS_NO_BG, ((current_bg_mode == bg_mode::BG_NONE) ? MF_CHECKED : MF_UNCHECKED));
1500     CheckMenuItem(hm, IDM_OPTIONS_BG, ((current_bg_mode == bg_mode::BG_ONE) ? MF_CHECKED : MF_UNCHECKED));
1501     CheckMenuItem(hm, IDM_OPTIONS_PRESET_BG, ((current_bg_mode == bg_mode::BG_PRESET) ? MF_CHECKED : MF_UNCHECKED));
1502     // TODO IDM_OPTIONS_PRESET_BG を有効にする
1503     EnableMenuItem(hm, IDM_OPTIONS_PRESET_BG, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1504 }
1505
1506 /*!
1507  * @brief Check for double clicked (or dragged) savefile
1508  * @details
1509  * Apparently, Windows copies the entire filename into the first
1510  * piece of the "command line string".  Perhaps we should extract
1511  * the "basename" of that filename and append it to the "save" dir.
1512  * @param savefile_option savefile path
1513  */
1514 static void check_for_save_file(const std::string &savefile_option)
1515 {
1516     if (savefile_option.empty())
1517         return;
1518
1519     strcpy(savefile, savefile_option.c_str());
1520     validate_file(savefile);
1521     game_in_progress = true;
1522 }
1523
1524 /*!
1525  * @brief Process a menu command
1526  */
1527 static void process_menus(player_type *player_ptr, WORD wCmd)
1528 {
1529     if (!initialized) {
1530         plog(_("まだ初期化中です...", "You cannot do that yet..."));
1531         return;
1532     }
1533
1534     term_data *td;
1535     OPENFILENAMEW ofn;
1536     switch (wCmd) {
1537     case IDM_FILE_NEW: {
1538         if (game_in_progress || movie_in_progress) {
1539             plog(_("プレイ中は新しいゲームを始めることができません!", "You can't start a new game while you're still playing!"));
1540         } else {
1541             game_in_progress = true;
1542             savefile[0] = '\0';
1543         }
1544
1545         break;
1546     }
1547     case IDM_FILE_OPEN: {
1548         if (game_in_progress || movie_in_progress) {
1549             plog(_("プレイ中はゲームをロードすることができません!", "You can't open a new game while you're still playing!"));
1550         } else {
1551             memset(&ofn, 0, sizeof(ofn));
1552             ofn.lStructSize = sizeof(ofn);
1553             ofn.hwndOwner = data[0].w;
1554             ofn.lpstrFilter = L"Save Files (*.)\0*\0";
1555             ofn.nFilterIndex = 1;
1556             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
1557
1558             if (get_open_filename(&ofn, ANGBAND_DIR_SAVE, savefile, MAIN_WIN_MAX_PATH)) {
1559                 validate_file(savefile);
1560                 game_in_progress = true;
1561             }
1562         }
1563
1564         break;
1565     }
1566     case IDM_FILE_SAVE: {
1567         if (game_in_progress && current_world_ptr->character_generated) {
1568             if (!can_save) {
1569                 plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
1570                 break;
1571             }
1572
1573             msg_flag = FALSE;
1574             do_cmd_save_game(player_ptr, FALSE);
1575         } else {
1576             plog(_("今、セーブすることは出来ません。", "You may not do that right now."));
1577         }
1578
1579         break;
1580     }
1581     case IDM_FILE_EXIT: {
1582         if (game_in_progress && current_world_ptr->character_generated) {
1583             if (!can_save) {
1584                 plog(_("今は終了できません。", "You may not do that right now."));
1585                 break;
1586             }
1587
1588             msg_flag = FALSE;
1589             forget_lite(player_ptr->current_floor_ptr);
1590             forget_view(player_ptr->current_floor_ptr);
1591             clear_mon_lite(player_ptr->current_floor_ptr);
1592
1593             term_key_push(SPECIAL_KEY_QUIT);
1594             break;
1595         }
1596
1597         quit(NULL);
1598         break;
1599     }
1600     case IDM_FILE_SCORE: {
1601         char buf[1024];
1602         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
1603         highscore_fd = fd_open(buf, O_RDONLY);
1604         if (highscore_fd < 0) {
1605             msg_print("Score file unavailable.");
1606         } else {
1607             screen_save();
1608             term_clear();
1609             display_scores(0, MAX_HISCORES, -1, NULL);
1610             (void)fd_close(highscore_fd);
1611             highscore_fd = -1;
1612             screen_load();
1613             term_fresh();
1614         }
1615
1616         break;
1617     }
1618     case IDM_FILE_MOVIE: {
1619         if (game_in_progress || movie_in_progress) {
1620             plog(_("プレイ中はムービーをロードすることができません!", "You can't open a movie while you're playing!"));
1621         } else {
1622             memset(&ofn, 0, sizeof(ofn));
1623             ofn.lStructSize = sizeof(ofn);
1624             ofn.hwndOwner = data[0].w;
1625             ofn.lpstrFilter = L"Angband Movie Files (*.amv)\0*.amv\0";
1626             ofn.nFilterIndex = 1;
1627             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
1628
1629             if (get_open_filename(&ofn, ANGBAND_DIR_USER, savefile, MAIN_WIN_MAX_PATH)) {
1630                 prepare_browse_movie_without_path_build(savefile);
1631                 movie_in_progress = true;
1632             }
1633         }
1634
1635         break;
1636     }
1637     case IDM_WINDOW_VIS_0: {
1638         plog(_("メインウィンドウは非表示にできません!", "You are not allowed to do that!"));
1639         break;
1640     }
1641     case IDM_WINDOW_VIS_1:
1642     case IDM_WINDOW_VIS_2:
1643     case IDM_WINDOW_VIS_3:
1644     case IDM_WINDOW_VIS_4:
1645     case IDM_WINDOW_VIS_5:
1646     case IDM_WINDOW_VIS_6:
1647     case IDM_WINDOW_VIS_7: {
1648         int i = wCmd - IDM_WINDOW_VIS_0;
1649         if ((i < 0) || (i >= MAX_TERM_DATA))
1650             break;
1651
1652         td = &data[i];
1653         if (!td->visible) {
1654             td->visible = TRUE;
1655             ShowWindow(td->w, SW_SHOW);
1656             term_data_redraw(td);
1657         } else {
1658             td->visible = FALSE;
1659             td->posfix = FALSE;
1660             ShowWindow(td->w, SW_HIDE);
1661         }
1662
1663         break;
1664     }
1665     case IDM_WINDOW_FONT_0:
1666     case IDM_WINDOW_FONT_1:
1667     case IDM_WINDOW_FONT_2:
1668     case IDM_WINDOW_FONT_3:
1669     case IDM_WINDOW_FONT_4:
1670     case IDM_WINDOW_FONT_5:
1671     case IDM_WINDOW_FONT_6:
1672     case IDM_WINDOW_FONT_7: {
1673         int i = wCmd - IDM_WINDOW_FONT_0;
1674         if ((i < 0) || (i >= MAX_TERM_DATA))
1675             break;
1676
1677         td = &data[i];
1678         term_change_font(td);
1679         break;
1680     }
1681     case IDM_WINDOW_POS_1:
1682     case IDM_WINDOW_POS_2:
1683     case IDM_WINDOW_POS_3:
1684     case IDM_WINDOW_POS_4:
1685     case IDM_WINDOW_POS_5:
1686     case IDM_WINDOW_POS_6:
1687     case IDM_WINDOW_POS_7: {
1688         int i = wCmd - IDM_WINDOW_POS_0;
1689         if ((i < 0) || (i >= MAX_TERM_DATA))
1690             break;
1691
1692         td = &data[i];
1693         if (!td->posfix && td->visible) {
1694             td->posfix = TRUE;
1695             term_window_pos(td, HWND_TOPMOST);
1696         } else {
1697             td->posfix = FALSE;
1698             term_window_pos(td, data[0].w);
1699         }
1700
1701         break;
1702     }
1703     case IDM_WINDOW_I_WID_0:
1704     case IDM_WINDOW_I_WID_1:
1705     case IDM_WINDOW_I_WID_2:
1706     case IDM_WINDOW_I_WID_3:
1707     case IDM_WINDOW_I_WID_4:
1708     case IDM_WINDOW_I_WID_5:
1709     case IDM_WINDOW_I_WID_6:
1710     case IDM_WINDOW_I_WID_7: {
1711         int i = wCmd - IDM_WINDOW_I_WID_0;
1712         if ((i < 0) || (i >= MAX_TERM_DATA))
1713             break;
1714
1715         td = &data[i];
1716         td->tile_wid += 1;
1717         term_getsize(td);
1718         term_window_resize(td);
1719         break;
1720     }
1721     case IDM_WINDOW_D_WID_0:
1722     case IDM_WINDOW_D_WID_1:
1723     case IDM_WINDOW_D_WID_2:
1724     case IDM_WINDOW_D_WID_3:
1725     case IDM_WINDOW_D_WID_4:
1726     case IDM_WINDOW_D_WID_5:
1727     case IDM_WINDOW_D_WID_6:
1728     case IDM_WINDOW_D_WID_7: {
1729         int i = wCmd - IDM_WINDOW_D_WID_0;
1730         if ((i < 0) || (i >= MAX_TERM_DATA))
1731             break;
1732
1733         td = &data[i];
1734         td->tile_wid -= 1;
1735         term_getsize(td);
1736         term_window_resize(td);
1737         break;
1738     }
1739     case IDM_WINDOW_I_HGT_0:
1740     case IDM_WINDOW_I_HGT_1:
1741     case IDM_WINDOW_I_HGT_2:
1742     case IDM_WINDOW_I_HGT_3:
1743     case IDM_WINDOW_I_HGT_4:
1744     case IDM_WINDOW_I_HGT_5:
1745     case IDM_WINDOW_I_HGT_6:
1746     case IDM_WINDOW_I_HGT_7: {
1747         int i = wCmd - IDM_WINDOW_I_HGT_0;
1748         if ((i < 0) || (i >= MAX_TERM_DATA))
1749             break;
1750
1751         td = &data[i];
1752         td->tile_hgt += 1;
1753         term_getsize(td);
1754         term_window_resize(td);
1755         break;
1756     }
1757     case IDM_WINDOW_D_HGT_0:
1758     case IDM_WINDOW_D_HGT_1:
1759     case IDM_WINDOW_D_HGT_2:
1760     case IDM_WINDOW_D_HGT_3:
1761     case IDM_WINDOW_D_HGT_4:
1762     case IDM_WINDOW_D_HGT_5:
1763     case IDM_WINDOW_D_HGT_6:
1764     case IDM_WINDOW_D_HGT_7: {
1765         int i = wCmd - IDM_WINDOW_D_HGT_0;
1766         if ((i < 0) || (i >= MAX_TERM_DATA))
1767             break;
1768
1769         td = &data[i];
1770         td->tile_hgt -= 1;
1771         term_getsize(td);
1772         term_window_resize(td);
1773         break;
1774     }
1775     case IDM_WINDOW_KEEP_SUBWINDOWS: {
1776         keep_subwindows = !keep_subwindows;
1777         break;
1778     }
1779     case IDM_OPTIONS_NO_GRAPHICS: {
1780         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_NONE)) {
1781             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_NONE);
1782             if (game_in_progress)
1783                 do_cmd_redraw(player_ptr);
1784         }
1785         break;
1786     }
1787     case IDM_OPTIONS_OLD_GRAPHICS: {
1788         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL)) {
1789             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL);
1790             if (game_in_progress)
1791                 do_cmd_redraw(player_ptr);
1792         }
1793
1794         break;
1795     }
1796     case IDM_OPTIONS_NEW_GRAPHICS: {
1797         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT)) {
1798             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT);
1799             if (game_in_progress)
1800                 do_cmd_redraw(player_ptr);
1801         }
1802
1803         break;
1804     }
1805     case IDM_OPTIONS_NEW2_GRAPHICS: {
1806         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND)) {
1807             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND);
1808             if (game_in_progress)
1809                 do_cmd_redraw(player_ptr);
1810         }
1811
1812         break;
1813     }
1814     case IDM_OPTIONS_BIGTILE: {
1815         td = &data[0];
1816         arg_bigtile = !arg_bigtile;
1817         term_activate(&td->t);
1818         term_resize(td->cols, td->rows);
1819         InvalidateRect(td->w, NULL, TRUE);
1820         break;
1821     }
1822     case IDM_OPTIONS_MUSIC: {
1823         arg_music = !arg_music;
1824         use_music = arg_music;
1825         if (use_music) {
1826             init_music();
1827             if (game_in_progress)
1828                 select_floor_music(player_ptr);
1829         } else {
1830             main_win_music::stop_music();
1831         }
1832         break;
1833     }
1834     case IDM_OPTIONS_MUSIC_PAUSE_INACTIVE: {
1835         use_pause_music_inactive = !use_pause_music_inactive;
1836         break;
1837     }
1838     case IDM_OPTIONS_OPEN_MUSIC_DIR: {
1839         std::vector<char> buf(MAIN_WIN_MAX_PATH);
1840         path_build(&buf[0], MAIN_WIN_MAX_PATH, ANGBAND_DIR_XTRA_MUSIC, "music.cfg");
1841         open_dir_in_explorer(&buf[0]);
1842         break;
1843     }
1844     case IDM_OPTIONS_SOUND: {
1845         arg_sound = !arg_sound;
1846         change_sound_mode(arg_sound);
1847         break;
1848     }
1849     case IDM_OPTIONS_OPEN_SOUND_DIR: {
1850         std::vector<char> buf(MAIN_WIN_MAX_PATH);
1851         path_build(&buf[0], MAIN_WIN_MAX_PATH, ANGBAND_DIR_XTRA_SOUND, "sound.cfg");
1852         open_dir_in_explorer(&buf[0]);
1853         break;
1854     }
1855     case IDM_OPTIONS_NO_BG: {
1856         change_bg_mode(bg_mode::BG_NONE);
1857         break;
1858     }
1859     case IDM_OPTIONS_PRESET_BG: {
1860         change_bg_mode(bg_mode::BG_PRESET);
1861         break;
1862     }
1863     case IDM_OPTIONS_BG: {
1864         if (change_bg_mode(bg_mode::BG_ONE))
1865             break;
1866         // 壁紙の設定に失敗した(ファイルが存在しない等)場合、壁紙に使うファイルを選択させる
1867     }
1868         [[fallthrough]]; /* Fall through */
1869     case IDM_OPTIONS_OPEN_BG: {
1870         memset(&ofn, 0, sizeof(ofn));
1871         ofn.lStructSize = sizeof(ofn);
1872         ofn.hwndOwner = data[0].w;
1873         ofn.lpstrFilter = L"Image Files (*.bmp;*.png;*.jpg;*.jpeg;)\0*.bmp;*.png;*.jpg;*.jpeg;\0";
1874         ofn.nFilterIndex = 1;
1875         ofn.lpstrTitle = _(L"壁紙を選んでね。", L"Choose wall paper.");
1876         ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1877
1878         if (get_open_filename(&ofn, NULL, wallpaper_file, MAIN_WIN_MAX_PATH)) {
1879             change_bg_mode(bg_mode::BG_ONE, true, true);
1880         }
1881         break;
1882     }
1883     case IDM_DUMP_SCREEN_HTML: {
1884         save_screen_as_html(data[0].w);
1885         break;
1886     }
1887     }
1888 }
1889
1890 /*!
1891  * @brief Add a keypress to the "queue"
1892  */
1893 static errr term_keypress(int k)
1894 {
1895     /* Refuse to enqueue non-keys */
1896     if (!k)
1897         return -1;
1898
1899     /* Store the char, advance the queue */
1900     Term->key_queue[Term->key_head++] = (char)k;
1901
1902     /* Circular queue, handle wrap */
1903     if (Term->key_head == Term->key_size)
1904         Term->key_head = 0;
1905
1906     if (Term->key_head != Term->key_tail)
1907         return 0;
1908
1909     return 1;
1910 }
1911
1912 /*!
1913  * @brief Add a keypress to the "queue"
1914  * @details マルチバイト文字をkey_queueに追加する。
1915  * @param str マルチバイト文字列
1916  */
1917 static void term_keypress(char *str)
1918 {
1919     if (str) {
1920         char *psrc = str;
1921         while (*psrc) {
1922             term_keypress(*psrc);
1923             ++psrc;
1924         }
1925     }
1926 }
1927
1928 /*!
1929  * @brief キーダウンのハンドラ
1930  */
1931 static bool process_keydown(WPARAM wParam, LPARAM lParam)
1932 {
1933     bool mc = FALSE;
1934     bool ms = FALSE;
1935     bool ma = FALSE;
1936
1937     if (GetKeyState(VK_CONTROL) & 0x8000)
1938         mc = TRUE;
1939     if (GetKeyState(VK_SHIFT) & 0x8000)
1940         ms = TRUE;
1941     if (GetKeyState(VK_MENU) & 0x8000)
1942         ma = TRUE;
1943
1944     term_no_press = (ma) ? TRUE : FALSE;
1945     if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)])) {
1946         bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
1947         bool numpad = FALSE;
1948
1949         term_keypress(31);
1950         if (mc)
1951             term_keypress('C');
1952         if (ms)
1953             term_keypress('S');
1954         if (ma)
1955             term_keypress('A');
1956
1957         int i = LOBYTE(HIWORD(lParam));
1958         term_keypress('x');
1959         switch (wParam) {
1960         case VK_DIVIDE:
1961             term_no_press = TRUE;
1962             [[fallthrough]]; /* Fall through */
1963         case VK_RETURN:
1964             numpad = ext_key;
1965             break;
1966         case VK_NUMPAD0:
1967         case VK_NUMPAD1:
1968         case VK_NUMPAD2:
1969         case VK_NUMPAD3:
1970         case VK_NUMPAD4:
1971         case VK_NUMPAD5:
1972         case VK_NUMPAD6:
1973         case VK_NUMPAD7:
1974         case VK_NUMPAD8:
1975         case VK_NUMPAD9:
1976         case VK_ADD:
1977         case VK_MULTIPLY:
1978         case VK_SUBTRACT:
1979         case VK_SEPARATOR:
1980         case VK_DECIMAL:
1981             term_no_press = TRUE;
1982             [[fallthrough]]; /* Fall through */
1983         case VK_CLEAR:
1984         case VK_HOME:
1985         case VK_END:
1986         case VK_PRIOR:
1987         case VK_NEXT:
1988         case VK_INSERT:
1989         case VK_DELETE:
1990         case VK_UP:
1991         case VK_DOWN:
1992         case VK_LEFT:
1993         case VK_RIGHT:
1994             numpad = !ext_key;
1995         }
1996
1997         if (numpad)
1998             term_keypress('K');
1999
2000         term_keypress(hexsym[i / 16]);
2001         term_keypress(hexsym[i % 16]);
2002         term_keypress(13);
2003
2004         return 1;
2005     }
2006
2007     return 0;
2008 }
2009
2010 /*!
2011  * @brief ウィンドウのアクティブ/非アクティブのハンドラ
2012  */
2013 static void handle_app_active(HWND hWnd, UINT uMsg, WPARAM wParam, [[maybe_unused]] LPARAM lParam)
2014 {
2015     switch (uMsg) {
2016     case WM_ACTIVATEAPP: {
2017         if (wParam) {
2018             if (use_pause_music_inactive)
2019                 main_win_music::resume_music();
2020         } else {
2021             if (use_pause_music_inactive)
2022                 main_win_music::pause_music();
2023         }
2024         break;
2025     }
2026     case WM_WINDOWPOSCHANGING: {
2027         if (!IsIconic(hWnd))
2028             if (use_pause_music_inactive)
2029                 main_win_music::resume_music();
2030         break;
2031     }
2032     }
2033 }
2034
2035 /*!
2036  * @brief メインウインドウ用ウインドウプロシージャ
2037  */
2038 LRESULT PASCAL AngbandWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2039 {
2040     PAINTSTRUCT ps;
2041     term_data *td;
2042     td = (term_data *)GetWindowLong(hWnd, 0);
2043
2044     handle_app_active(hWnd, uMsg, wParam, lParam);
2045
2046     switch (uMsg) {
2047     case WM_NCCREATE: {
2048         SetWindowLong(hWnd, 0, (LONG)(my_td));
2049         break;
2050     }
2051     case WM_CREATE: {
2052         setup_mci(hWnd);
2053         return 0;
2054     }
2055     case WM_GETMINMAXINFO: {
2056         MINMAXINFO *lpmmi;
2057         RECT rc;
2058
2059         lpmmi = (MINMAXINFO *)lParam;
2060         if (!td)
2061             return 1;
2062
2063         rc.left = rc.top = 0;
2064         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
2065         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
2066
2067         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
2068
2069         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
2070         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
2071
2072         return 0;
2073     }
2074     case WM_ERASEBKGND: {
2075         return 1;
2076     }
2077     case WM_PAINT: {
2078         BeginPaint(hWnd, &ps);
2079         if (td)
2080             term_data_redraw(td);
2081         EndPaint(hWnd, &ps);
2082         ValidateRect(hWnd, NULL);
2083         return 0;
2084     }
2085     case MM_MCINOTIFY: {
2086         main_win_music::on_mci_notify(wParam, lParam);
2087
2088         return 0;
2089     }
2090     case WM_SYSKEYDOWN:
2091     case WM_KEYDOWN: {
2092         if (process_keydown(wParam, lParam))
2093             return 0;
2094         break;
2095     }
2096     case WM_CHAR: {
2097         // wParam is WCHAR because using RegisterClassW
2098         if (term_no_press)
2099             term_no_press = FALSE;
2100         else {
2101             WCHAR wc[2] = { (WCHAR)wParam, '\0' };
2102             term_keypress(to_multibyte(wc).c_str());
2103         }
2104         return 0;
2105     }
2106     case WM_LBUTTONDOWN: {
2107         if (macro_running())
2108             return 0;
2109         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
2110         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
2111         mouse_down = TRUE;
2112         oldx = mousex;
2113         oldy = mousey;
2114         return 0;
2115     }
2116     case WM_LBUTTONUP: {
2117         if (!mouse_down)
2118             return 0;
2119         HGLOBAL hGlobal;
2120         LPSTR lpStr;
2121         TERM_LEN dx = abs(oldx - mousex) + 1;
2122         TERM_LEN dy = abs(oldy - mousey) + 1;
2123         TERM_LEN ox = (oldx > mousex) ? mousex : oldx;
2124         TERM_LEN oy = (oldy > mousey) ? mousey : oldy;
2125
2126         mouse_down = FALSE;
2127         paint_rect = FALSE;
2128
2129 #ifdef JP
2130         int sz = (dx + 3) * dy;
2131 #else
2132         int sz = (dx + 2) * dy;
2133 #endif
2134         hGlobal = GlobalAlloc(GHND, sz + 1);
2135         if (hGlobal == NULL)
2136             return 0;
2137         lpStr = (LPSTR)GlobalLock(hGlobal);
2138
2139         for (int i = 0; i < dy; i++) {
2140 #ifdef JP
2141             char *s;
2142             const auto &scr = data[0].t.scr->c;
2143
2144             C_MAKE(s, (dx + 1), char);
2145             strncpy(s, &scr[oy + i][ox], dx);
2146
2147             if (ox > 0) {
2148                 if (iskanji(scr[oy + i][ox - 1]))
2149                     s[0] = ' ';
2150             }
2151
2152             if (ox + dx < data[0].cols) {
2153                 if (iskanji(scr[oy + i][ox + dx - 1]))
2154                     s[dx - 1] = ' ';
2155             }
2156
2157             for (int j = 0; j < dx; j++) {
2158                 if (s[j] == 127)
2159                     s[j] = '#';
2160                 *lpStr++ = s[j];
2161             }
2162 #else
2163             for (int j = 0; j < dx; j++) {
2164                 *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
2165             }
2166 #endif
2167             if (dy > 1) {
2168                 *lpStr++ = '\r';
2169                 *lpStr++ = '\n';
2170             }
2171         }
2172
2173         GlobalUnlock(hGlobal);
2174         if (OpenClipboard(hWnd) == 0) {
2175             GlobalFree(hGlobal);
2176             return 0;
2177         }
2178
2179         EmptyClipboard();
2180         SetClipboardData(CF_TEXT, hGlobal);
2181         CloseClipboard();
2182         term_redraw();
2183         return 0;
2184     }
2185     case WM_MOUSEMOVE: {
2186         if (!mouse_down)
2187             return 0;
2188
2189         int dx, dy;
2190         int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
2191         int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
2192         int ox, oy;
2193
2194         if (paint_rect) {
2195             dx = abs(oldx - mousex) + 1;
2196             dy = abs(oldy - mousey) + 1;
2197             ox = (oldx > mousex) ? mousex : oldx;
2198             oy = (oldy > mousey) ? mousey : oldy;
2199             term_inversed_area(hWnd, ox, oy, dx, dy);
2200         } else {
2201             paint_rect = TRUE;
2202         }
2203
2204         dx = abs(cx - mousex) + 1;
2205         dy = abs(cy - mousey) + 1;
2206         ox = (cx > mousex) ? mousex : cx;
2207         oy = (cy > mousey) ? mousey : cy;
2208         term_inversed_area(hWnd, ox, oy, dx, dy);
2209
2210         oldx = cx;
2211         oldy = cy;
2212         return 0;
2213     }
2214     case WM_INITMENU: {
2215         setup_menus();
2216         return 0;
2217     }
2218     case WM_CLOSE: {
2219         if (!game_in_progress || !current_world_ptr->character_generated) {
2220             quit(NULL);
2221             return 0;
2222         }
2223
2224         if (!can_save) {
2225             plog(_("今は終了できません。", "You may not do that right now."));
2226             return 0;
2227         }
2228
2229         msg_flag = FALSE;
2230         forget_lite(p_ptr->current_floor_ptr);
2231         forget_view(p_ptr->current_floor_ptr);
2232         clear_mon_lite(p_ptr->current_floor_ptr);
2233         term_key_push(SPECIAL_KEY_QUIT);
2234         return 0;
2235     }
2236     case WM_QUERYENDSESSION: {
2237         if (!game_in_progress || !current_world_ptr->character_generated) {
2238             quit(NULL);
2239             return 0;
2240         }
2241
2242         msg_flag = FALSE;
2243         if (p_ptr->chp < 0)
2244             p_ptr->is_dead = FALSE;
2245         exe_write_diary(p_ptr, DIARY_GAMESTART, 0, _("----ゲーム中断----", "---- Save and Exit Game ----"));
2246
2247         p_ptr->panic_save = 1;
2248         signals_ignore_tstp();
2249         (void)strcpy(p_ptr->died_from, _("(緊急セーブ)", "(panic save)"));
2250         (void)save_player(p_ptr, SAVE_TYPE_CLOSE_GAME);
2251         quit(NULL);
2252         return 0;
2253     }
2254     case WM_QUIT: {
2255         quit(NULL);
2256         return 0;
2257     }
2258     case WM_COMMAND: {
2259         process_menus(p_ptr, LOWORD(wParam));
2260         return 0;
2261     }
2262     case WM_SIZE: {
2263         if (!td)
2264             return 1;
2265         if (!td->w)
2266             return 1;
2267         if (td->size_hack)
2268             return 1;
2269
2270         //!< @todo 二重のswitch文。後で分割する.
2271         switch (wParam) {
2272         case SIZE_MINIMIZED: {
2273             for (int i = 1; i < MAX_TERM_DATA; i++) {
2274                 if (data[i].visible)
2275                     ShowWindow(data[i].w, SW_HIDE);
2276             }
2277
2278             return 0;
2279         }
2280         case SIZE_MAXIMIZED:
2281         case SIZE_RESTORED: {
2282             TERM_LEN cols = (LOWORD(lParam) - td->size_ow1 - td->size_ow2) / td->tile_wid;
2283             TERM_LEN rows = (HIWORD(lParam) - td->size_oh1 - td->size_oh2) / td->tile_hgt;
2284             if ((td->cols != cols) || (td->rows != rows)) {
2285                 td->cols = cols;
2286                 td->rows = rows;
2287                 if (!IsZoomed(td->w) && !IsIconic(td->w)) {
2288                     normsize.x = td->cols;
2289                     normsize.y = td->rows;
2290                 }
2291
2292                 term_activate(&td->t);
2293                 term_resize(td->cols, td->rows);
2294                 InvalidateRect(td->w, NULL, TRUE);
2295             }
2296
2297             td->size_hack = TRUE;
2298             for (int i = 1; i < MAX_TERM_DATA; i++) {
2299                 if (data[i].visible)
2300                     ShowWindow(data[i].w, SW_SHOWNA);
2301             }
2302
2303             td->size_hack = FALSE;
2304
2305             return 0;
2306         }
2307         }
2308
2309         break;
2310     }
2311     case WM_ACTIVATE: {
2312         if (!wParam || HIWORD(lParam))
2313             break;
2314
2315         for (int i = 1; i < MAX_TERM_DATA; i++) {
2316             if (!data[i].posfix)
2317                 term_window_pos(&data[i], hWnd);
2318         }
2319
2320         SetFocus(hWnd);
2321         return 0;
2322     }
2323     case WM_ACTIVATEAPP: {
2324         if (IsIconic(td->w))
2325             break;
2326
2327         for (int i = 1; i < MAX_TERM_DATA; i++) {
2328             if (data[i].visible) {
2329                 if (wParam == TRUE) {
2330                     ShowWindow(data[i].w, SW_SHOWNA);
2331                 } else {
2332                     ShowWindow(data[i].w, SW_HIDE);
2333                 }
2334             }
2335         }
2336     }
2337         [[fallthrough]]; /* Fall through */
2338     case WM_ENABLE: {
2339         if (wParam == FALSE && keep_subwindows) {
2340             for (int i = 1; i < MAX_TERM_DATA; i++) {
2341                 if (data[i].visible) {
2342                     ShowWindow(data[i].w, SW_SHOWNA);
2343                 }
2344             }
2345         }
2346     }
2347     }
2348
2349     return DefWindowProc(hWnd, uMsg, wParam, lParam);
2350 }
2351
2352 /*!
2353  * @brief サブウインドウ用ウインドウプロシージャ
2354  */
2355 LRESULT PASCAL AngbandListProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2356 {
2357     term_data *td;
2358     PAINTSTRUCT ps;
2359     td = (term_data *)GetWindowLong(hWnd, 0);
2360
2361     switch (uMsg) {
2362     case WM_NCCREATE: {
2363         SetWindowLong(hWnd, 0, (LONG)(my_td));
2364         break;
2365     }
2366     case WM_CREATE: {
2367         return 0;
2368     }
2369     case WM_GETMINMAXINFO: {
2370         MINMAXINFO *lpmmi;
2371         RECT rc;
2372
2373         lpmmi = (MINMAXINFO *)lParam;
2374         if (!td)
2375             return 1;
2376
2377         rc.left = rc.top = 0;
2378         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
2379         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
2380
2381         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
2382         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
2383         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
2384         return 0;
2385     }
2386     case WM_SIZE: {
2387         if (!td)
2388             return 1;
2389         if (!td->w)
2390             return 1;
2391         if (td->size_hack)
2392             return 1;
2393
2394         td->size_hack = TRUE;
2395
2396         TERM_LEN cols = (LOWORD(lParam) - td->size_ow1 - td->size_ow2) / td->tile_wid;
2397         TERM_LEN rows = (HIWORD(lParam) - td->size_oh1 - td->size_oh2) / td->tile_hgt;
2398         if ((td->cols != cols) || (td->rows != rows)) {
2399             term_type *old_term = Term;
2400             td->cols = cols;
2401             td->rows = rows;
2402             term_activate(&td->t);
2403             term_resize(td->cols, td->rows);
2404             term_activate(old_term);
2405             InvalidateRect(td->w, NULL, TRUE);
2406             p_ptr->window_flags = 0xFFFFFFFF;
2407             handle_stuff(p_ptr);
2408         }
2409
2410         td->size_hack = FALSE;
2411         return 0;
2412     }
2413     case WM_ERASEBKGND: {
2414         return 1;
2415     }
2416     case WM_PAINT: {
2417         BeginPaint(hWnd, &ps);
2418         if (td)
2419             term_data_redraw(td);
2420         EndPaint(hWnd, &ps);
2421         return 0;
2422     }
2423     case WM_SYSKEYDOWN:
2424     case WM_KEYDOWN: {
2425         if (process_keydown(wParam, lParam))
2426             return 0;
2427
2428         break;
2429     }
2430     case WM_CHAR: {
2431         // wParam is WCHAR because using RegisterClassW
2432         if (term_no_press)
2433             term_no_press = FALSE;
2434         else {
2435             WCHAR wc[2] = { (WCHAR)wParam, '\0' };
2436             term_keypress(to_multibyte(wc).c_str());
2437         }
2438         return 0;
2439     }
2440     case WM_NCLBUTTONDOWN: {
2441         if (wParam == HTCLOSE)
2442             wParam = HTSYSMENU;
2443
2444         if (wParam == HTSYSMENU) {
2445             if (td->visible) {
2446                 td->visible = FALSE;
2447                 ShowWindow(td->w, SW_HIDE);
2448             }
2449
2450             return 0;
2451         }
2452
2453         break;
2454     }
2455     }
2456
2457     return DefWindowProc(hWnd, uMsg, wParam, lParam);
2458 }
2459
2460 /*!
2461  * @brief Display warning message (see "z-util.c")
2462  */
2463 static void hook_plog(concptr str)
2464 {
2465     if (str) {
2466         MessageBoxW(data[0].w, to_wchar(str).wc_str(), _(L"警告!", L"Warning"), MB_ICONEXCLAMATION | MB_OK);
2467     }
2468 }
2469
2470 /*!
2471  * @brief Display error message and quit (see "z-util.c")
2472  */
2473 static void hook_quit(concptr str)
2474 {
2475     if (str) {
2476         MessageBoxW(data[0].w, to_wchar(str).wc_str(), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2477     }
2478
2479     save_prefs();
2480     for (int i = MAX_TERM_DATA - 1; i >= 0; --i) {
2481         term_force_font(&data[i]);
2482         if (data[i].font_want)
2483             string_free(data[i].font_want);
2484         if (data[i].w)
2485             DestroyWindow(data[i].w);
2486         data[i].w = 0;
2487     }
2488
2489     DeleteObject(hbrYellow);
2490     finalize_bg();
2491     graphic.finalize();
2492
2493     UnregisterClassW(AppName, hInstance);
2494     if (hIcon)
2495         DestroyIcon(hIcon);
2496
2497     exit(0);
2498 }
2499
2500 /*!
2501  * @brief Init some stuff
2502  */
2503 static void init_stuff(void)
2504 {
2505     char path[MAIN_WIN_MAX_PATH];
2506     DWORD path_len = GetModuleFileNameA(hInstance, path, MAIN_WIN_MAX_PATH);
2507     strcpy(path + path_len - 4, ".INI");
2508     ini_file = string_make(path);
2509
2510     int i = path_len;
2511     for (; i > 0; i--) {
2512         if (path[i] == '\\') {
2513             break;
2514         }
2515     }
2516
2517     strcpy(path + i + 1, "lib\\");
2518     validate_dir(path, TRUE);
2519     init_file_paths(path, path);
2520     validate_dir(ANGBAND_DIR_APEX, FALSE);
2521     validate_dir(ANGBAND_DIR_BONE, FALSE);
2522     if (!check_dir(ANGBAND_DIR_EDIT)) {
2523         validate_dir(ANGBAND_DIR_DATA, TRUE);
2524     } else {
2525         validate_dir(ANGBAND_DIR_DATA, FALSE);
2526     }
2527
2528     validate_dir(ANGBAND_DIR_FILE, TRUE);
2529     validate_dir(ANGBAND_DIR_HELP, FALSE);
2530     validate_dir(ANGBAND_DIR_INFO, FALSE);
2531     validate_dir(ANGBAND_DIR_PREF, TRUE);
2532     validate_dir(ANGBAND_DIR_SAVE, FALSE);
2533     validate_dir(ANGBAND_DIR_DEBUG_SAVE, FALSE);
2534     validate_dir(ANGBAND_DIR_USER, TRUE);
2535     validate_dir(ANGBAND_DIR_XTRA, TRUE);
2536     path_build(path, sizeof(path), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
2537
2538     validate_file(path);
2539     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
2540     ANGBAND_DIR_XTRA_GRAF = string_make(path);
2541     validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
2542
2543     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
2544     ANGBAND_DIR_XTRA_SOUND = string_make(path);
2545     validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
2546
2547     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
2548     ANGBAND_DIR_XTRA_MUSIC = string_make(path);
2549     validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
2550
2551     for (i = 0; special_key_list[i]; ++i) {
2552         special_key[special_key_list[i]] = TRUE;
2553     }
2554
2555     for (i = 0; ignore_key_list[i]; ++i) {
2556         ignore_key[ignore_key_list[i]] = TRUE;
2557     }
2558
2559     ANGBAND_SYS = "win";
2560     if (7 != GetKeyboardType(0))
2561         ANGBAND_KEYBOARD = "0";
2562     else {
2563         switch (GetKeyboardType(1)) {
2564         case 0x0D01:
2565         case 0x0D02:
2566         case 0x0D03:
2567         case 0x0D04:
2568         case 0x0D05:
2569         case 0x0D06:
2570             /* NEC PC-98x1 */
2571             ANGBAND_KEYBOARD = "NEC98";
2572             break;
2573         default:
2574             /* PC/AT */
2575             ANGBAND_KEYBOARD = "JAPAN";
2576         }
2577     }
2578 }
2579
2580 /*!
2581  * @brief 全スポイラー出力を行う
2582  * Create Spoiler files
2583  * @details スポイラー出力処理の成功、失敗に関わらずプロセスを終了する。
2584  */
2585 void create_debug_spoiler(void)
2586 {
2587     init_stuff();
2588     init_angband(p_ptr, TRUE);
2589
2590     switch (output_all_spoilers()) {
2591     case spoiler_output_status::SPOILER_OUTPUT_SUCCESS:
2592         fprintf(stdout, "Successfully created a spoiler file.");
2593     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FOPEN:
2594         fprintf(stderr, "Cannot create spoiler file.");
2595     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FCLOSE:
2596         fprintf(stderr, "Cannot close spoiler file.");
2597     default:
2598         break;
2599     }
2600
2601     quit(NULL);
2602 }
2603
2604 /*!
2605  * @brief メインウインドウ、サブウインドウのウインドウクラス登録
2606  */
2607 static void register_wndclass(void)
2608 {
2609     WNDCLASSW wc{};
2610     wc.style = CS_CLASSDC;
2611     wc.lpfnWndProc = AngbandWndProc;
2612     wc.cbClsExtra = 0;
2613     wc.cbWndExtra = 4;
2614     wc.hInstance = hInstance;
2615     wc.hIcon = hIcon = LoadIconW(hInstance, AppName);
2616     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2617     wc.hbrBackground = NULL;
2618     wc.lpszMenuName = AppName;
2619     wc.lpszClassName = AppName;
2620
2621     if (!RegisterClassW(&wc))
2622         exit(1);
2623
2624     wc.lpfnWndProc = AngbandListProc;
2625     wc.lpszMenuName = NULL;
2626     wc.lpszClassName = AngList;
2627
2628     if (!RegisterClassW(&wc))
2629         exit(2);
2630 }
2631
2632 /*!
2633  * @brief (Windows固有)Windowsアプリケーションとしてのエントリポイント
2634  */
2635 int WINAPI WinMain(
2636     _In_ HINSTANCE hInst, [[maybe_unused]] _In_opt_ HINSTANCE hPrevInst, [[maybe_unused]] _In_ LPSTR lpCmdLine, [[maybe_unused]] _In_ int nCmdShow)
2637 {
2638     setlocale(LC_ALL, "ja_JP");
2639     hInstance = hInst;
2640     if (is_already_running()) {
2641         MessageBoxW(
2642             NULL, _(L"変愚蛮怒はすでに起動しています。", L"Hengband is already running."), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2643         return FALSE;
2644     }
2645
2646     command_line.handle();
2647     register_wndclass();
2648
2649     // before term_data initialize
2650     plog_aux = [](concptr str) {
2651         if (str)
2652             MessageBoxW(NULL, to_wchar(str).wc_str(), _(L"警告!", L"Warning"), MB_ICONEXCLAMATION | MB_OK);
2653     };
2654     quit_aux = [](concptr str) {
2655         if (str) {
2656             MessageBoxW(NULL, to_wchar(str).wc_str(), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2657         }
2658
2659         UnregisterClassW(AppName, hInstance);
2660         if (hIcon)
2661             DestroyIcon(hIcon);
2662
2663         exit(0);
2664     };
2665     core_aux = quit_aux;
2666
2667     init_stuff();
2668
2669     HDC hdc = GetDC(NULL);
2670     if (GetDeviceCaps(hdc, BITSPIXEL) <= 8) {
2671         quit(_("画面を16ビット以上のカラーモードにして下さい。", "Please switch to High Color (16-bit) or higher color mode."));
2672     }
2673     ReleaseDC(NULL, hdc);
2674
2675     refresh_color_table();
2676     init_windows();
2677     change_bg_mode(current_bg_mode, true);
2678
2679     // after term_data initialize
2680     plog_aux = hook_plog;
2681     quit_aux = hook_quit;
2682     core_aux = hook_quit;
2683
2684     signals_init();
2685     term_activate(term_screen);
2686     init_angband(p_ptr, FALSE);
2687     initialized = TRUE;
2688
2689     check_for_save_file(command_line.get_savefile_option());
2690     prt(_("[ファイル] メニューの [新規] または [開く] を選択してください。", "[Choose 'New' or 'Open' from the 'File' menu]"), 23, _(8, 17));
2691     term_fresh();
2692
2693     change_sound_mode(arg_sound);
2694     use_music = arg_music;
2695     if (use_music) {
2696         init_music();
2697     }
2698
2699     // ユーザーがゲーム開始を選択するまで待つループ
2700     MSG msg;
2701     while (GetMessage(&msg, NULL, 0, 0)) {
2702         TranslateMessage(&msg);
2703         DispatchMessage(&msg);
2704         if (game_in_progress || movie_in_progress)
2705             break;
2706     }
2707
2708     term_flush();
2709     if (movie_in_progress) {
2710         // selected movie
2711         play_game(p_ptr, FALSE, TRUE);
2712     } else if (savefile[0] == '\0') {
2713         // new game
2714         play_game(p_ptr, TRUE, FALSE);
2715     }
2716     else {
2717         // selected savefile
2718         play_game(p_ptr, FALSE, FALSE);
2719     }
2720
2721     quit(NULL);
2722     return 0;
2723 }
2724 #endif /* WINDOWS */