开始

在使用文件管理器中,我们经常要用到的一个需求就是获取一个文件的全路径,那么需要右键,安全,滑动复制

太麻烦了!

如果可以用ahk便利的获取到路径就好了!

所以有了下面的脚本:中键显示菜单。

代码

#Include G:\AHK\gitee_ahk2\common\Tip.ahk
#Include G:\AHK\gitee_ahk2\common\Theme.ahk
#Include G:\AHK\gitee_ahk2\common\Extend.ahk
#Include G:\AHK\gitee_ahk2\common\util\Explorer.ahk
#Include G:\AHK\gitee_ahk2\common\util\Animation.ahk

CoordMode 'Mouse', 'Screen'

MButton:: ShowContextMenu()

imgList := IL_Create(10)
  , IL_Add(imgList, './resource/icon/vscode.ico')
  , IL_Add(imgList, './resource/icon/text.png')

ExplorerContextActions := [
  ['Icon' 0, '复制路径', _copyPath],
  ['Icon' 1, '以vscode打开', _openWithVsCode],
  ['Icon' 2, '以记事本打开', _openWithNotepad],
]

selected := []

ShowContextMenu() {
  try id := WinGetID('A')
  catch {
    Tip.ShowTip('Error on get id')
    return
  }
  if !Explorer.IsValidHwnd(id)
    return
  window := Explorer(WinGetID('A'))
  global selected := window.GetListViewSelected()
  if !selected.Length
    return
  ExplorerContext.Show(imgList, ExplorerContextActions)
}

_openWithVsCode() {
  if selected.Length
    Run('code ' selected[1])
}
_openWithNotepad() {
  if selected.Length
    Run('notepad ' selected.join(A_Space))
}
_copyPath() {
  if selected.Length
    A_Clipboard := '', A_Clipboard := selected[1], Tip.ShowTip('Copied!')
}


class ExplorerContext extends Gui {

  static Width := 150
  cbs := Map()

  __New(imageList := '', actions := []) {
    super.__New('+AlwaysOnTop +ToolWindow -Caption')
    this.SetFont('s14', 'consolas')
    lv := this.AddListView('w' ExplorerContext.Width ' Grid -Multi -Hdr Lv0x2000 LV0x8000', ['items']) ;
    lv.OnEvent('Click', (p*) => this.OnLvClick(p*))
    this.lv := lv, lv.SetImageList(imageList)
    for v in actions
      this.LvAdd(v[1], v[2], v[3])
    Theme.Light(this)
  }

  LvAdd(opts, col, callback) {
    this.lv.Add(opts, col), this.cbs.Set(col, callback)
  }

  Modify() {
    lv := this.lv, this.GetPos(&x, &y, &w, &h)
    lv.Move(3, 3, w - 6, h - 6), lv.ModifyCol(1, w - 10)
  }

  _Hide() {
    Animation.FadeOut(this, true)
      , HotKey('~LButton Up', 'Off')
  }

  static Show(imgList, actions) {
    static ins := ExplorerContext(imgList, actions)
    Animation.FadeIn(ins, (*) => ins.Modify(), 8)
    Hotkey '~LButton Up', (*) => ins._Hide(), 'On'
  }

  OnLvClick(lv, row, *) => this.cbs.Get(lv.GetText(row, 1), Noop)()

}

说明

脚本使用的几个依赖:

  • Animation.ahk 用于在ui的显示和隐藏时添加动画效果
  • Explorer.ahk 用于获取选中的文件路径

这些或许可以在其他文章中找到,或许在仓库中……我也不记得

另外,脚本使用到了imageList,需要在路径中有对应的资源,在仓库中已经上传了;但没有资源脚本也不会停止,只是无法显示图标.

  • 实际使用时,可能需要微调listView的大小,通过修改Modify()方法,这已经是我所能想到让listview完美覆盖gui的最好的方法了。

效果

image

我添加了我常用的几个操作,你可以添加自己的,需要修改:

  • ExplorerContextActions里的回调列表

如果需要图标,修改imgList

posted on 2024-03-25 13:13  落寞的雪  阅读(138)  评论(0编辑  收藏  举报