Desktop image preview on hover

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Stinkfoot9
Posts: 51
Joined: 29 May 2022, 12:17

Desktop image preview on hover

Post by Stinkfoot9 » 02 Aug 2022, 15:27

I've made a script for viewing previews of images on the desktop using a gui on hover of image.

First biggish script so excuse any redundancy or mishaps, works good though.

Code: Select all

		
		; Desktop image Preview - Press Alt over hovered image
		; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=106980&p=475571#p475571
		
			#NoEnv
			#NoTrayIcon
			CoordMode, Mouse , Screen
			SW := A_ScreenWidth
			SH := A_ScreenHeight
			GUIHW = 700	; <------ Adjust Preview Image size		
			TGUIHW := GUIHW*1.275 
			
			
			
			
			~alt:: 
			settimer, pic, 200
			Keywait,alt, u
			if errorlevel = 0      
			{
			Settimer, Pic, off
			oldgium = ""
			Gui, Destroy
			}
			return                                                                                                                      



			GetNameOfIconUnderMouse() { 			; https://www.autohotkey.com/boards/viewtopic.php?t=51788
			MouseGetPos, , , hwnd, CtrlClass
			WinGetClass, WinClass, ahk_id %hwnd%
			try if (WinClass = "CabinetWClass" && CtrlClass = "DirectUIHWND3") {
				oAcc := Acc_ObjectFromPoint()
				Name := Acc_Parent(oAcc).accValue(0)
				Name := Name ? Name : oAcc.accValue(0)
			} else if (WinClass = "Progman" || WinClass = "WorkerW") {
				oAcc := Acc_ObjectFromPoint(ChildID)
				Name := ChildID ? oAcc.accName(ChildID) : ""
			}
			Return Name
			}
			

			Acc_Init() {  ; https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
				Static h
				If Not h
					h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
			}
			Acc_ObjectFromPoint(ByRef _idChild_ = "", x = "", y = "") {
				Acc_Init()
				If	DllCall("oleacc\AccessibleObjectFromPoint", "Int64", x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
				Return ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
			}
			Acc_Parent(Acc) { 
				try parent:=Acc.accParent
				return parent?Acc_Query(parent):
			}
			Acc_Query(Acc) { ; thanks Lexikos - www.autohotkey.com/forum/viewtopic.php?t=81731&p=509530#509530
				try return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
			}
		
		
			Pic:
			
			GIUM := GetNameOfIconUnderMouse()
			DeskGium = C:\Users\istit\OneDrive\Desktop\%GIUM%			
			
			imgSize(img, ByRef width , ByRef height) ; Get image's dimensions
				{
				If FileExist(img) 
					{
					GUI, Mygui: Add, Picture, hwndpic, %img%
					ControlGetPos,,, width, height,, ahk_id %pic%
					Gui, Mygui: Destroy
					} 
				}
			imgSize(DeskGium, iW , iH)
			
			
			if InStr(GIUM, ".ico") || InStr(GIUM, ".png") ||  InStr(GIUM, ".jpg") || InStr(GIUM, ".jpeg")
				{
				If (GIUM != OLDGIUM) ; If new file DOESN'T match old. 
				;\
				{
					MouseGetPos, x1, y1
					X2 := (x1 + 50)
					Gui, destroy
					Gui, -caption -AlwaysOnTop +Disabled -SysMenu +Owner ; +Owner avoids a taskbar tab.					
					Gui, Color, EEAA99
					Gui +LastFound  ; Make the GUI window the last found window for use by the line below.
					WinSet, TransColor, EEAA99
					
					If (Iw > IH) ; "If image Wider than tall"
						{                                                      
						cNih := (y1 + (Ih/(iw/tguihw)))                                                           						
						If (x1 > 1000)  ; "If cursor right of middle screen reverse manifest left"
							{
							x2 := ((x1 - tguihw) - 150)	
							If (CNih > Sh) ; Cursor + NIH > SH                                    
								{                                                                           
								oh1 := (CNih - sh)                                                        
								y1 := (y1 - oh1)                                      
								}   	
							}
						If ((x1 - tguihw) < 0)  ; "Stop Left side clipping 1020 is a actual image size of 800"
							{
							OhL := (tguihw + x1)         
							x2 := !ohl
							If (y1 > 540)
								{
								Oh1b := (sh - (tguihw + y1) )
								y1 := !oh1b
								}
							Else if (y1 < 600)
								{
								y1 := (Y1 + 10)
								}
							}	
						Gui, Add, Picture, x10 w%guihw% h-1 ,%DESKGIUM%    ;"800 is too large for my screen so some clipping is inevitable, I've contained the clipping to the left side as most of my images are on the right."
						gui, Show, x%x2% y%y1%, NoActivate								
						}
					if (ih > iw) ; "if image taller than wider"
						{
						If (x1 > 900)
							{
							x2 := (x1 - (Iw/(ih/tguihw)) - 150) ; "manifest on left side also add buffer"                    
							}                     											                                                          
						If ((y1 + tguihw) > SH) 	  ; "Stop image from manifesting outside of screen bottom."    
							{
							oh2 := ((y1 + tguihw) - sh)					
							y1 := !oh
							}	 
						Gui, Add, Picture, x10 w-1 h%guihw% ,%DESKGIUM%
						gui, Show, x%x2% y%y1%, NoActivate
						} 
						OLDGIUM = %GIUM%		  
					}
				}
			return

Return to “Scripts and Functions (v1)”