这段脚本实现了一个增强的桌面交互功能,允许用户通过双击桌面空白处或图标区域来切换桌面图标的显示和隐藏状态。以下是脚本的功能详解:

 

; #Include <GetDesktopIconUnderMouse>

#If IsDesktopUnderMouse()
~LButton::
  LButton_presses++			;记录现在是第几次按下鼠标
  if (LButton_presses=2)
    ;GetDesktopIconUnderMouse() 能在桌面图标隐藏后也取到图标,此时双击看起来是空白的原图标位置就将因为识别到图标而不切换显隐状态
    ;所以鼠标下没有取到图标,或者取到图标但当前全部桌面图标是已隐藏的状态,则切换一遍显隐状态
    if (!IsObject(GetDesktopIconUnderMouse()) or DesktopIconsIsShow=0)			
      DesktopIconsIsShow:=HideOrShowDesktopIcons()
  SetTimer, KeyLButton, -300			;多少毫秒以内的连续点击算作双击
return

KeyLButton:
  LButton_presses := 0
return

IsDesktopUnderMouse()			;鼠标下的是桌面吗
{
  MouseGetPos, , , OutputVarWin
  WinGetClass, OutputVarClass, % "ahk_id" OutputVarWin
  if (OutputVarClass="WorkerW" or OutputVarClass="Progman")
    return, 1
  else
    return, 0
}

HideOrShowDesktopIcons()			;返回值为0代表当前桌面图标已隐藏,1代表已显示
{
  ControlGet, OutputVarHwnd, Hwnd,, SysListView321, ahk_class WorkerW
  if (OutputVarHwnd="")
    ControlGet, OutputVarHwnd, Hwnd,, SysListView321, ahk_class Progman
  if (DllCall("IsWindowVisible", UInt, OutputVarHwnd))
  {
    WinHide, ahk_id %OutputVarHwnd%
    return, 0
  }
  else
  {
    WinShow, ahk_id %OutputVarHwnd%
    return, 1
  }
}
#If

; ===============================================================================================================================
; GetDesktopIconUnderMouse()
; https://www.autohotkey.com/boards/viewtopic.php?p=260761
; Function:       Gets the desktop icon under the mouse. See the "Return values" section below for more information about the
;                 icon and associated file data retrieved.
; Parameters:     None
; Return values:  If there is an icon under the mouse, an associative array with the following keys:
;                 - left: the left position of the icon in screen coordinates
;                 - top: the top position of the icon in screen coordinates
;                 - right: the right position of the icon in screen coordinates
;                 - bottom: the bottom position of the icon in screen coordinates
;                 - name: the name of the file represented by the icon, e.g. New Text Document.txt
;                 - size: the size of the file represented by the icon, e.g. 1.72 KB. Note: this value is blank for folders
;                 - type: the type of the file represented by the icon, e.g. TXT File, JPEG image, File folder
;                 - date: the modified date of the file represented by the icon, e.g. 9/9/2016 10:39 AM
;                 Otherwise, a blank value
; Global vars:    None
; Dependencies:   None
; Tested with:    AHK 1.1.30.01 (A32/U32/U64)
; Tested on:      Win 7 (x64)
; Written by:     iPhilip
; ===============================================================================================================================

GetDesktopIconUnderMouse() {
  static MEM_COMMIT := 0x1000, MEM_RELEASE := 0x8000, PAGE_ReadWRITE := 0x04
    , PROCESS_VM_OPERATION := 0x0008, PROCESS_VM_READ := 0x0010
    , LVM_GETITEMCOUNT := 0x1004, LVM_GETITEMRECT := 0x100E

  Icon := ""
  MouseGetPos, x, y, hwnd
  if not (hwnd = WinExist("ahk_class Progman") || hwnd = WinExist("ahk_class WorkerW"))
    return
  ControlGet, hwnd, HWND, , SysListView321
  if not WinExist("ahk_id" hwnd)
    return
  WinGet, pid, PID
  if (hProcess := DllCall("OpenProcess" , "UInt", Process_VM_OPERATION|Process_VM_Read, "Int",  false, "UInt", pid)) {
    VarSetCapacity(iCoord, 16)
    SendMessage, %LVM_GETITEMCOUNT%, 0, 0
    loop, %ErrorLevel% {
      pItemCoord := DllCall("VirtualAllocEx", "Ptr", hProcess, "Ptr", 0, "UInt", 16, "UInt", MEM_COMMIT, "UInt", PAGE_ReadWRITE)
      SendMessage, %LVM_GETITEMRECT%, % A_Index-1, %pItemCoord%
      DllCall("ReadProcessMemory", "Ptr", hProcess, "Ptr", pItemCoord, "Ptr", &iCoord, "UInt", 16, "UInt", 0)
      DllCall("VirtualFreeEx", "Ptr", hProcess, "Ptr", pItemCoord, "UInt", 0, "UInt", MEM_RELEASE)
      left   := NumGet(iCoord,  0, "Int")
      top    := NumGet(iCoord,  4, "Int")
      Right  := NumGet(iCoord,  8, "Int")
      bottom := NumGet(iCoord, 12, "Int")
      if (left < x and x < Right and top < y and y < bottom) {
        ControlGet, list, List
        RegExMatch(StrSplit(list, "`n")[A_Index], "O)(.*)\t(.*)\t(.*)\t(.*)", Match)
        Icon := {left:left, top:top, Right:Right, bottom:bottom
          , name:Match[1], size:Match[2], type:Match[3]
        ; Delete extraneous date characters (https://goo.gl/pMw6AM):
        ; - Unicode LTR (Left-to-Right) mark (0x200E = 8206)
        ; - Unicode RTL (Right-to-Left) mark (0x200F = 8207)
          , date:RegExReplace(Match[4], A_IsUnicode ? "[\x{200E}-\x{200F}]" : "\?")}
        break
      }
    }
    DllCall("CloseHandle", "Ptr", hProcess)
  }
  return Icon
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。