_GuiDropFiles.ahk - 响应拖拽开始、结束

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

Post Reply
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

_GuiDropFiles.ahk - 响应拖拽开始、结束

Post by tmplinshi » 10 Sep 2014, 17:49

代码是从这个帖子修改而来的(感谢 amnesiac 修复了 Unicode 中的问题)
_GuiDropFiles.ahk
用法:

GuiDropFiles.config(界面句柄, 拖拽开始时运行的标签, 拖拽结束时运行的标签)

拖拽结束时,变量 GuiDropFiles_FileName 包含了拖拽的文件路径(只能保存一个)

示例

Code: Select all

#NoEnv
#Include, <_GuiDropFiles>

; =================================
;        界面
; =================================
Gui, +HwndHGUI +AlwaysOnTop
Gui, Add, Edit, w300 HwndHEdit1, 
Gui, Add, Edit, w300 HwndHEdit2, 这里不接受拖拽
Gui, Add, Edit, w300 HwndHEdit3, 
Gui, Show,, 拖拽高亮

GuiDropFiles.config(HGUI, "GuiDropFiles_Begin", "GuiDropFiles_End")
Return

; =================================
;        开始拖拽
; =================================
GuiDropFiles_Begin:
    CoverControl(HEdit1 "," HEdit3)
Return

; =================================
;        拖拽结束
; =================================
GuiDropFiles_End:
    CoverControl()

    If GuiDropFiles_FileName {
        MouseGetPos,,,, hwnd, 2
        If hwnd in %HEdit1%,%HEdit3%
            GuiControl,, %hwnd%, % GuiDropFiles_FileName
    }
Return

; =================================
;        退出
; =================================
GuiClose:
ExitApp

; ===============================================================================================
CoverControl(hwnd_CsvList = "") {
    static handler := {__New: "test"}
    static _ := new handler
    static HGUI2
    If !HGUI2 {
        Gui, New, +LastFound +hwndHGUI2 -Caption +E0x20 +ToolWindow +AlwaysOnTop
        Gui, Color, 00FF00
        Gui, 1:Default
        WinSet, Transparent, 50
    }

    If (hwnd_CsvList = "") {
        Gui, %HGUI2%:Cancel
        Return
    }

    static lastHwnd

    MouseGetPos,,,, hwnd, 2

    If hwnd not in %hwnd_CsvList%
    {
        Gui, %HGUI2%:Cancel
        lastHwnd := ""
        Return
    }
    If (hwnd = lastHwnd)
        Return

    WinGetPos, x, y, w, h, ahk_id %hwnd%
    Gui, %HGUI2%:Show, X%x% Y%y% w%w% h%h% NA

    lastHwnd := hwnd
}
Image
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by RobertL » 10 Sep 2014, 21:53

图文教程真是赏心悦目..感谢!
  • 函数GuiDropFiles.config设置拖放始末 事件的回调/响应
  • 函数CoverControl用于切换控件高亮。
  • 通过鼠标获取释放时所处的控件
  • 释放 时为控件更新GuiDropFiles_FileName
  • 构建了一个与目标控件 匹配的窗口,使用半透明进行高亮
  • LastHwnd是干嘛的?仅用于hwnd = lastHwnd条件中,为真时返回。
  • 函数CoverControl中的static handler := {__New: "test"}static _ := new handler是不是多余的?
我为人人,人人为己?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by tmplinshi » 11 Sep 2014, 03:24

函数CoverControl中的static handler := {__New: "test"}static _ := new handler是不是多余的?
确实可以去掉,只是我担心在拖拽时才创建那个半透明窗口会造成一点延时,所以让脚本启动时就建立。
Last edited by tmplinshi on 16 Dec 2014, 03:49, edited 1 time in total.
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by RobertL » 11 Sep 2014, 05:45

嗯,高亮是个好功能,回头把他单独提取成通用小模块
我为人人,人人为己?
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by arcticir » 12 Sep 2014, 09:32

脚本不错,但示例不好。
获取已选数据,普通的方法当然是剪贴板的复制粘贴,
但剪贴板不单效率低,而且易受干扰不稳定。
而脚本的优点就是绕过剪贴板(当然还有拖选特性,并不等同普通选择)
所以它适合的场景,应该是类似万能存储箱之类的东西,
把所有拖拽到数据都记录下来。(然后上传到GMAIL,嘎嘎)
个人观点啦。
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by arcticir » 12 Sep 2014, 09:35

对了,能变通下,直接获取已选数据吗?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by tmplinshi » 12 Sep 2014, 09:39

@arcticir
不太清楚你的意思。这个脚本我主要是用来响应拖拽的开始,因为 ahk 自带的 GuiDropFiles: 标签只能在拖拽结束时响应。
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by arcticir » 12 Sep 2014, 09:52

你点错技能树的样子啊
从浏览器拖拽图片得到的数据:
Image
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by arcticir » 12 Sep 2014, 09:58

那能在拖拽时响应,而不是在释放拖拽时响应吗?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by tmplinshi » 12 Sep 2014, 10:01

不知道
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: _GuiDropFiles.ahk - 响应拖拽开始、结束

Post by arcticir » 12 Sep 2014, 10:08

Image
Post Reply

Return to “脚本函数”