This is not a particularly perfect code, and there is a lot of room for improvement in terms of performance and compatibility.
The main purpose is to share it so that more people can get inspired.
I hope someone can help to improve the way to get the full file path under mouse. (I've done some research in this area, but haven't made any major breakthroughs.)
Code (Recommend download the packaged zip file.)
Code: Select all
; 鼠标移动到文件上面,不用点击即可预览内容。支持 ahk,txt,ini,jpg,jpeg,png,bmp,tif 格式。
; Mouse over to the file to preview the content without clicking. Support ahk,txt,ini,jpg,jpeg,png,bmp,tif format.
; You must enable "show file ext name" in explorer.
SetBatchLines, -1
CoordMode, Mouse, Screen
maxStr:=200, maxW:=500, maxH:=500
gosub, init
SetTimer, preview, 100
return
preview:
Current := GetFileUnderMouse()
if (Displayed != Current.Path)
{
Displayed := Current.Path
Ext := Current.Ext
if Ext in ahk,txt,ini
{
UpdateLayeredWindow(hPreview, hdc, , , , , 0)
File := FileOpen(Current.Path, "r")
btt(File.Read(maxStr),,,,"Style5")
File.Close()
}
else if Ext in jpg,jpeg,png,bmp,tif
{
btt()
gosub, displaypic
}
else
{
btt()
UpdateLayeredWindow(hPreview, hdc, , , , , 0)
}
}
return
init:
; close system file info tip.
RegRead, ShowInfoTip, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, ShowInfoTip
RegWrite, REG_DWORD, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, ShowInfoTip, 0
Gui, -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs +HwndhPreview
Gui, Show, NA
pToken := Gdip_Startup()
OnExit, GdipExit
hbm := CreateDIBSection(maxW, maxH)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetInterpolationMode(G, 0)
return
displaypic:
pBitmap := Gdip_CreateBitmapFromFile(Current.Path)
Width := Gdip_GetImageWidth(pBitmap)
Height := Gdip_GetImageHeight(pBitmap)
ratio := Width/Height
if (ratio>=1)
{
dw:=maxW
dh:=dw/ratio
}
else
{
dh:=maxH
dw:=ratio*dh
}
Gdip_DrawImage(G, pBitmap, 0, 0, dw, dh)
Gdip_DisposeImage(pBitmap)
MouseGetPos, OutputVarX, OutputVarY
UpdateLayeredWindow(hPreview, hdc, OutputVarX+16, OutputVarY+16, dw, dh)
return
GdipExit:
RegWrite, REG_DWORD, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, ShowInfoTip, %ShowInfoTip%
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
SelectObject(hdc, obm)
DeleteDC(hdc)
DeleteObject(hbm)
Gdip_Shutdown(pToken)
ExitApp
return
; https://www.autohotkey.com/boards/viewtopic.php?t=51788
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
GetFileUnderMouse()
{
static Windows:=ComObjCreate("Shell.Application").Windows
MouseGetPos, , , hwnd, CtrlClass
WinGetClass, WinClass, ahk_id %hwnd%
try if (WinClass = "CabinetWClass" && InStr(CtrlClass, "DirectUIHWND"))
{
oAcc := Acc_ObjectFromPoint()
Name := Acc_Parent(oAcc).accValue(0)
NonNull(Name, oAcc.accValue(0))
if (Name="")
return
for window in Windows
if (window.hwnd = hwnd)
{
FolderPath := RegExReplace(window.Document.Folder.Self.Path, "(\w+?\:)\\$", "$1") ; “d:\” 转换为 “d:”
SplitPath, Name, , , OutExtension, OutNameNoExt
ret := {}
ret.Path := FolderPath "\" Name
ret.Ext := OutExtension
ret.Name := OutNameNoExt
return, ret
}
}
else if (WinClass = "Progman" || WinClass = "WorkerW")
{
oAcc := Acc_ObjectFromPoint(ChildID)
Name := ChildID ? oAcc.accName(ChildID) : ""
if (Name="")
return
SplitPath, Name, , , OutExtension, OutNameNoExt
ret := {}
ret.Path := A_Desktop "\" Name
ret.Ext := OutExtension
ret.Name := OutNameNoExt
return, ret
}
}
Acc_Init() {
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)
}
#Include <Gdip_All>
Attention:You must enable "File name extensions" in explorer.
Download