Adjust Aspect Ratio to window size ??

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
yfjuu6
Posts: 134
Joined: 28 Apr 2023, 15:28

Adjust Aspect Ratio to window size ??

Post by yfjuu6 » 02 Dec 2023, 15:06

I use ImageGlass as the main app to browse and view my photos. I want to create a functionality that when someone try to resize a window displays a photo, it respects a pre-defined Aspect Ratio.
here is 2 examples :
e.g 1- this is a V1 script does the functionality on a GUI window :

Code: Select all

#NoEnv
#SingleInstance, Ignore
ClientWidth  := 735 ; you only can set the size of the client area
ClientHeight := 245 ; you only can set the size of the client area
Gui, +AlwaysOnTop +Resize -MaximizeBox +ToolWindow +LastFound  ; added +LastFound for WinGetPos
Gui, Show, w%ClientWidth% h%ClientHeight% NoActivate, Test GUI ; removed unnecessary xCenter and yCenter
WinGetPos, , , GuiWidth, GuiHeight                             ; get the real window size
OnMessage(WM_SIZING := 0x0214, "GuiSizing")                    ; handle WM_SIZING messages
Return
GuiClose:
ExitApp
GuiSizing(WP, LP) {
   ; http://msdn.microsoft.com/en-us/library/ms632647(v=vs.85).aspx
   ; WP : The edge of the window that is being sized.
   ; LP : A pointer to a RECT structure with the screen coordinates of the drag rectangle.
   Global GuiWidth, GuiHeight
   ; The following values passed in WP will keep the ratio
   Static KeepsRatio := {4: "TopLeft", 5: "TopRight", 7: "BottomLeft", 8: "BottomRight"}
   ; The following values passed in WP will resize horizontally
   Static Horizontal := {1: "Left", 2: "Right"}
   ; The following values passed in WP will resize vertically
   Static Vertical   := {3: "Top",  6: "Bottom"}
   Critical
   ; If the ratio will be kept, do nothing
   If !KeepsRatio.HasKey(WP) {
      ; Get the new window rectangle
      X := NumGet(LP + 0,  0, "Int")
      Y := NumGet(LP + 0,  4, "Int")
      W := NumGet(LP + 0,  8, "Int") - X
      H := NumGet(LP + 0, 12, "Int") - Y
      ; If the window will be resized horizontally, adjust the height
      If Horizontal.HasKey(WP)
         NumPut(Y + (W / GuiWidth * GuiHeight), LP + 0, 12, "Int")
      ; Otherwise it will be resized vertically, so adjust the width
      Else
         NumPut(X + (H / GuiHeight * GuiWidth), LP + 0,  8, "Int")
   }
}
e.g 2- Exactly the same feature as in the multimedia software Player "PotPlayer" as shown in this image :
Adjust Aspect Ratio to window size ??
Note :
- I found the GUI script mentioned as an example from here :
https://www.autohotkey.com/board/topic/92581-how-to-maintain-guis-aspect-ratio-when-resizing/
Any help to do so please!
Attachments
Adjust AR to window size.png
Adjust AR to window size.png (61.21 KiB) Viewed 164 times

Return to “Ask for Help (v2)”