GUI Post-IT Notes Need Auto-Size

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Evo32
Posts: 1
Joined: 26 Jun 2022, 13:08

GUI Post-IT Notes Need Auto-Size

Post by Evo32 » 26 Jun 2022, 13:18

Below is some code that I use to act as "POST-IT NOTES". I have a folder with some .jpegs of my Aussie puppy that I pin to my screen while working sometimes. To load a picture of him, I just have to enter the name of the file in the InputBox in line 6. If I enter a v, it will paste my clipboard into the GUI that is acting as the post-it note. Everything works amazing and I use another bit of code to change the opacity, make it click-through, and remove the titlebar. My problem is that it doesn't auto-size. I set the height and width to a certain picture and that one looks perfect, but others are not so great. This was tolerable until I started using the 'v' clipboard part. Some things I make a snippet of on my screen are 100x100 and some things are 800x200.

Thank you for your time!

Code: Select all

i := 1
; PHOTO STICKER
this:
!^p::
i++
InputBox, pic, Photo Sticker, Enter .JPEG`n(v for clipboard)`n`nbedboy | bfastbutt | happyboy | hotboy | playboy | rgbboy`nrgbboy2 | sunmoon | tiredboy
if !pic
	Return
else if pic = v
{
	if DllCall("OpenClipboard", "ptr", 0) {
		if DllCall("IsClipboardFormatAvailable", "uint", 2) {
			hBitmap := DllCall("GetClipboardData", "uint", 2, "ptr")
		} else return
		DllCall("CloseClipboard")
	}

	Gui %i%: Add, Picture,hwndmypic, % "HBITMAP:*" hBitmap
	controlgetpos,,,wh,ht,,ahk_id %mypic%
	gui %i%: Destroy
	if (wh > ht) {
		Gui %i%: Add, Picture, x0 y0 w420 h-1, % "HBITMAP:*" hBitmap
	} else {
		Gui %i%: Add, Picture, x0 y0 w-1 h420, % "HBITMAP:*" hBitmap
	}
	Gui %i%: Show, w320 h320
	Gui %i%: +Resize
	;Gui %i%: +LastFound
	;WinSet, Transparent, 127
	Return
}
	
if !FileExist("C:\Users\Evo\Pictures\"pic+".jpeg")
	Return
 
gui %i%: Add, Picture,hwndmypic, C:\Users\Evo\Pictures\%pic%.jpeg
controlgetpos,,,wh,ht,,ahk_id %mypic%
gui %i%: Destroy
if (wh > ht) {
	Gui %i%: Add, Picture,x0 y0 w320 h-1, C:\Users\Evo\Pictures\%pic%.jpeg
} else {
	Gui %i%: Add, Picture,x0 y0 w-1 h320, C:\Users\Evo\Pictures\%pic%.jpeg
}
Gui %i%: Show, w320 h320
Gui %i%: +Resize
;Gui %i%: +LastFound
;WinSet, Transparent, 127
Return

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: GUI Post-IT Notes Need Auto-Size

Post by mikeyww » 26 Jun 2022, 13:45

I did not test your script, but it looks like it would scale the image according to the edge length that you have set.

Code: Select all

image = d:\images\microHDMI.png
If !FileExist(image) {
  MsgBox, 48, Error, File not found. Aborting.`n`n%image%
 Return
} Else img := imgSize(image)
If (img.w > img.h)
     Gui, Add, Picture, x0 y0 w320 h-1 , %image%
Else Gui, Add, Picture, x0 y0 w-1  h320, %image%
Gui, Show, w320 h320

imgSize(img) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
 ; Returns an array indicating the image's width (w) and height (h), obtained from the file's properties
 SplitPath, img, fn, dir
 objShell := ComObjCreate("Shell.Application")
 objFolder := objShell.NameSpace(dir), objFolderItem := objFolder.ParseName(fn)
 scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
 Return {w: scale.1, h: scale.2}
}
Picture:
To retain the image's actual width and/or height, omit the W and/or H options. Otherwise, the image is scaled to the specified width and/or height (this width and height also determines which icon to load from a multi-icon .ICO file). To shrink or enlarge the image while preserving its aspect ratio, specify -1 for one of the dimensions and a positive number for the other. For example, specifying w200 h-1 would make the image 200 pixels wide and cause its height to be set automatically. If the picture cannot be loaded or displayed (e.g. file not found), the control is left empty and its width and height are set to zero.
Output:

image220626-1455-001.png
Output
image220626-1455-001.png (320.13 KiB) Viewed 402 times
Attachments
microHDMI.png
Test image
microHDMI.png (1.1 MiB) Viewed 405 times

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: GUI Post-IT Notes Need Auto-Size

Post by boiler » 26 Jun 2022, 14:02

I’m thinking OP’s issue is that the GUI window itself is not automatically sizing to the picture size. I don’t see why the window is being forced to be 320x320 in the GUI show command. Don’t specify any size and specify the margins to be zero and the GUI size should conform to the picture size no matter what it is. Adding the +Resize attribute doesn’t resize the window. It allows the user to manually resize it.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: GUI Post-IT Notes Need Auto-Size

Post by mikeyww » 26 Jun 2022, 14:05

Confirmed!

Show:
Omit the X, Y, W, and H options below to have the window retain its previous size and position. If there is no previous position, the window will be auto-centered in one or both dimensions if the X and/or Y options mentioned below are absent. If there is no previous size, the window will be auto-sized according to the size and positions of the controls it contains.

Code: Select all

image = d:\images\microHDMI.png
If !FileExist(image) {
 MsgBox, 48, Error, File not found. Aborting.`n`n%image%
 Return
} Else Gui, Margin, 0, 0
img := imgSize(image)
If (img.w > img.h)
     Gui, Add, Picture, x0 y0 w320 h-1 , %image%
Else Gui, Add, Picture, x0 y0 w-1  h320, %image%
Gui, Show

imgSize(img) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
 ; Returns an array indicating the image's width (w) and height (h), obtained from the file's properties
 SplitPath, img, fn, dir
 objShell := ComObjCreate("Shell.Application")
 objFolder := objShell.NameSpace(dir), objFolderItem := objFolder.ParseName(fn)
 scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
 Return {w: scale.1, h: scale.2}
}
image220626-1505-001.png
Output
image220626-1505-001.png (316.86 KiB) Viewed 389 times

Post Reply

Return to “Ask for Help (v1)”