/// <summary> /// 指定ウィンドウの移動とサイズ変更を行います。 /// </summary> /// <param name="windowHandle">ウィンドウハンドル</param> /// <param name="rect">RECTオブジェクト</param> private void MoveUmamusumeWindow(IntPtr windowHandle, RECT rect) { if (rect == new RECT()) { return; } Win32api.MoveWindow(windowHandle, rect.X, rect.Y, rect.Width, rect.Height, false); }
/// <summary> /// タイマーティックイベント /// </summary> private void Timer_Tick(object sender, EventArgs e) { //ウマ娘のウィンドウハンドル取得 Process[] process = Process.GetProcessesByName(umamusumeProcessName); //アプリの終了を検知した場合 if (process.Length == 0) { if (IsUmamusumeAppRunning) { double aspectRatioBefore = (double)beforeRECT.Height / beforeRECT.Width; if (aspectRatioBefore > 1) { //縦 setting.BeforeVerticalRECT = beforeRECT; } else { //横 setting.BeforeHorizontalRECT = beforeRECT; } } IsUmamusumeAppRunning = false; return; } Process umamusumeProcess = process.First(); bool error = Win32api.GetWindowRect(umamusumeProcess.MainWindowHandle, out RECT rect); //起動を検知したら、フラグセット。 //Windowの大きさを設定して終了。 if (!IsUmamusumeAppRunning) { WaitUmaWindowAvailable(umamusumeProcess, timeOut); IsUmamusumeAppRunning = true; MoveUmamusumeWindow(umamusumeProcess.MainWindowHandle, setting.BeforeVerticalRECT); return; } //サイズ変更が行われた if (rect != beforeRECT) { //ウィンドウの縦横比が変わったら、大きさ変更 double aspectRatioCurrent = (double)rect.Height / rect.Width; double aspectRatioBefore = (double)beforeRECT.Height / beforeRECT.Width; //浮動小数点の比較は、誤差があるので差の絶対値を求めて、誤差以上の場合等しくない。 if (Math.Abs(aspectRatioCurrent - aspectRatioBefore) > tolerance) { //横画面になった時 if (aspectRatioCurrent < 1 && aspectRatioBefore > 1) { setting.BeforeVerticalRECT = beforeRECT; WaitUmaWindowAvailable(umamusumeProcess, timeOut); MoveUmamusumeWindow(umamusumeProcess.MainWindowHandle, setting.BeforeHorizontalRECT); } //縦長になった時 else if (aspectRatioBefore < 1 && aspectRatioCurrent > 1) { setting.BeforeHorizontalRECT = beforeRECT; WaitUmaWindowAvailable(umamusumeProcess, timeOut); MoveUmamusumeWindow(umamusumeProcess.MainWindowHandle, setting.BeforeVerticalRECT); } } } beforeRECT = rect; }