Skip to content

Instantly share code, notes, and snippets.

@unarist
Created May 4, 2021 17:05
Show Gist options
  • Save unarist/7d1033e11ca8e841778425e7e7ac144e to your computer and use it in GitHub Desktop.
Save unarist/7d1033e11ca8e841778425e7e7ac144e to your computer and use it in GitHub Desktop.
autoresize
open System
open System.Windows.Automation
module Screen =
open System.Windows.Forms
open System.Drawing
let workingAreaFor (rect: System.Windows.Rect) =
let rect = new Rectangle(int rect.X, int rect.Y, int rect.Width, int rect.Height)
let workingArea = (Screen.FromRectangle rect).WorkingArea
new System.Windows.Rect(float workingArea.X, float workingArea.Y, float workingArea.Width, float workingArea.Height)
module Rect =
open System.Windows
let fitTo (container: Rect) (contents: Rect) =
let newRect = new Rect(contents.Location, contents.Size)
let scale = Math.Min(container.Width / contents.Width, container.Height / contents.Height)
newRect.Scale(scale, scale)
if container.Bottom < newRect.Bottom then newRect.Offset(0.0, container.Top - newRect.Top)
if container.Right < newRect.Right then newRect.Offset(container.Left - newRect.Left, 0.0)
newRect
[<EntryPoint>]
let main argv =
let mainWindowHandle = (System.Diagnostics.Process.GetProcessesByName "hoge").[0].MainWindowHandle
let gameWindow = AutomationElement.FromHandle mainWindowHandle
let transformPattern = gameWindow.GetCurrentPattern TransformPattern.Pattern :?> TransformPattern
let rec loop (lastRect: System.Windows.Rect): unit =
let currentRect = gameWindow.Current.BoundingRectangle
if not (lastRect.Equals currentRect) then
let workingArea = Screen.workingAreaFor gameWindow.Current.BoundingRectangle
let transformedRect = Rect.fitTo workingArea gameWindow.Current.BoundingRectangle
transformPattern.Move(transformedRect.Left, transformedRect.Top)
transformPattern.Resize(transformedRect.Width, transformedRect.Height)
System.Threading.Thread.Sleep 1000
loop currentRect
loop (new System.Windows.Rect())
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment