using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace GameLauncher.Services { /// /// ツクールVX / VX Ace のゲームウィンドウを、ディスプレイに収まる最大の整数倍へ拡大する外部連携サービス。 /// ゲーム側は無改造のまま、ランチャーがゲームウィンドウを検出して Win32 API で拡大します。 /// 外部ツールパスが設定されている場合はそちらを優先起動します。 /// public class WindowResizeService { #region Win32 API [DllImport("user32.dll", SetLastError = true)] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); [DllImport("user32.dll")] private static extern int GetSystemMetrics(int nIndex); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")] private static extern int GetWindowTextLength(IntPtr hWnd); private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); private const int SM_CXSCREEN = 0; private const int SM_CYSCREEN = 1; private const uint SWP_NOZORDER = 0x0004; private const uint SWP_NOACTIVATE = 0x0010; #endregion /// /// ゲーム起動後にウィンドウ拡大を適用します。 /// 外部ツールパスが設定されていればそれを起動し、無ければ内蔵の Win32 API 方式で拡大します。 /// /// 起動済みのゲームプロセス /// 外部ツール(Sizer等)の実行ファイルパス。空なら内蔵方式。 /// (成功可否, ログ用メッセージ) public async Task<(bool Success, string Message)> ApplyResizeAsync(Process process, string? externalToolPath) { if (!string.IsNullOrEmpty(externalToolPath) && File.Exists(externalToolPath)) { try { Process.Start(new ProcessStartInfo { FileName = externalToolPath, UseShellExecute = true }); return (true, $"外部ツールを起動しました: {Path.GetFileName(externalToolPath)}"); } catch (Exception ex) { // 外部ツール起動失敗時は内蔵方式にフォールバック var (ok, msg) = await this.ApplyBuiltInResizeAsync(process); return (ok, $"外部ツール起動に失敗したため内蔵方式を使用: {ex.Message} / {msg}"); } } return await this.ApplyBuiltInResizeAsync(process); } /// /// 内蔵の Win32 API 方式でゲームウィンドウを整数倍に拡大します。 /// public async Task<(bool Success, string Message)> ApplyBuiltInResizeAsync(Process process) { try { IntPtr hWnd = IntPtr.Zero; // ウィンドウが生成されるまで最大約5秒待機 for (int i = 0; i < 50; i++) { process.Refresh(); if (process.HasExited) { return (false, "ゲームプロセスが終了しました。"); } if (process.MainWindowHandle != IntPtr.Zero) { hWnd = process.MainWindowHandle; break; } // メインウィンドウが未確定な場合、可視ウィンドウをプロセスIDで探索 hWnd = FindWindowByProcessId((uint)process.Id); if (hWnd != IntPtr.Zero) break; await Task.Delay(100); } if (hWnd == IntPtr.Zero) { return (false, "ゲームウィンドウを検出できませんでした。"); } int screenW = GetSystemMetrics(SM_CXSCREEN); int screenH = GetSystemMetrics(SM_CYSCREEN); // ゲームの既定ウィンドウサイズ(VX/VX Ace 標準) int baseW = 544; int baseH = 416; int scale = Math.Min(screenW / baseW, screenH / baseH); if (scale < 1) scale = 1; int newW = baseW * scale; int newH = baseH * scale; bool moved = SetWindowPos(hWnd, IntPtr.Zero, 0, 0, newW, newH, SWP_NOZORDER | SWP_NOACTIVATE); return moved ? (true, $"ウィンドウを {newW}x{newH} ({scale}x) に拡大しました。") : (false, "ウィンドウサイズの変更に失敗しました。"); } catch (Exception ex) { return (false, $"ウィンドウ拡大中にエラーが発生しました: {ex.Message}"); } } /// /// 指定プロセスIDに属する可視トップレベルウィンドウを検索します。 /// private static IntPtr FindWindowByProcessId(uint processId) { IntPtr result = IntPtr.Zero; EnumWindows((hWnd, lParam) => { GetWindowThreadProcessId(hWnd, out uint pid); if (pid == processId && IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0) { result = hWnd; return false; // 見つかったので列挙終了 } return true; }, IntPtr.Zero); return result; } } }