Created
September 10, 2014 23:05
-
-
Save tmplinshi/981f61a8ea5368761c71 to your computer and use it in GitHub Desktop.
响应拖拽开始、结束
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; http://ahkscript.org/boards/viewtopic.php?f=28&p=25564#p25564 | |
Class GuiDropFiles | |
{ | |
config(GuiHwnd, BeginLable = "", EndLable = "") { | |
global IDropSource, IDropTarget | |
VarSetCapacity(IDropSource,24,0), NumPut(&IDropSource+4,IDropSource), nParams=31132 | |
Loop, Parse, nParams | |
NumPut(RegisterCallback("IDropSource","",A_LoopField,A_Index-1),IDropSource,4*A_Index) | |
VarSetCapacity(IDropTarget,32,0), NumPut(&IDropTarget+4,IDropTarget), nParams=3116516 | |
Loop, Parse, nParams | |
NumPut(RegisterCallback("IDropTarget","",A_LoopField,A_Index-1),IDropTarget,4*A_Index) | |
DllCall("ole32\OleInitialize","Uint",0) | |
DllCall("ole32\RegisterDragDrop","Uint",GuiHwnd,"Uint",&IDropTarget) | |
this.GuiHwnd := GuiHwnd | |
this.BeginLable := BeginLable | |
this.EndLable := EndLable | |
} | |
__Delete() { | |
static _ := new GuiDropFiles | |
DllCall("ole32\RevokeDragDrop","Uint",this.GuiHwnd) | |
DllCall("ole32\OleUninitialize") | |
} | |
} | |
IDropSource(this, escape=0, key=0) | |
{ | |
If A_EventInfo = 3 | |
hResult := escape ? 0x00040101 : key&3 ? 0 : 0x00040100 | |
Else If A_EventInfo = 4 | |
hResult := 0x00040102 | |
Else If A_EventInfo = 0 | |
hResult := 0, NumPut(this,key+0) | |
Else hResult := 0 | |
Return hResult | |
} | |
IDropTarget(this, pdata=0, key=0, x=0, y=0, peffect=0) | |
{ | |
global GuiDropFiles_FileName | |
If (A_EventInfo = 4) { | |
GuiDropFiles_FileName := "" | |
NumPut(NumGet(y+0)&5,y+0) | |
If (GuiDropFiles.BeginLable != "") | |
SetTimer, % GuiDropFiles.BeginLable, -1 | |
} | |
Else If A_EventInfo = 3 | |
NumPut(NumGet(peffect+0)&5,peffect+0) | |
Else If A_EventInfo = 6 | |
NumPut(NumGet(peffect+0)&5,peffect+0), GuiDropFiles_FileName := IEnumFormatEtc(pdata) | |
Else If A_EventInfo = 0 | |
NumPut(this,key+0) | |
If A_EventInfo in 5,6 | |
{ | |
If (GuiDropFiles.EndLable != "") | |
SetTimer, % GuiDropFiles.EndLable, -1 | |
} | |
Return 0 | |
} | |
IEnumFormatEtc(this) | |
{ | |
DllCall(NumGet(NumGet(1*this)+32),"Uint",this,"Uint",1,"UintP",penum) ; DATADIR_GET=1, DATADIR_SET=2 | |
Loop | |
{ | |
VarSetCapacity(FormatEtc,20,0) | |
If DllCall(NumGet(NumGet(1*penum)+12), "Uint", penum, "Uint",1, "Uint", &FormatEtc, "Uint",0) | |
Break | |
0+(nFormat:=NumGet(FormatEtc,0,"Ushort"))<18 ? RegExMatch(__cfList, "(?:\w+\s+){" . nFormat-1 . "}(?<FORMAT>\w+\b)", CF_) : nFormat>=0x80&&nFormat<=0x83 ? RegExMatch("CF_OWNERDISPLAY CF_DSPTEXT CF_DSPBITMAP CF_DSPMETAFILEPICT", "(?:\w+\s+){" . nFormat-0x80 . "}(?<FORMAT>\w+\b)", CF_) : nFormat=0x8E ? CF_FORMAT:="CF_DSPENHMETAFILE" : CF_FORMAT:=GetClipboardFormatName(nFormat) | |
VarSetCapacity(StgMedium,12,0) | |
If DllCall(NumGet(NumGet(1*this)+12), "Uint", this, "Uint", &FormatEtc, "Uint", &StgMedium) | |
Continue | |
If NumGet(StgMedium,0)=1 ; TYMED_HGLOBAL=1 | |
{ | |
hData:=NumGet(StgMedium,4) | |
pData:=DllCall("GlobalLock", "Uint", hData) | |
nSize:=DllCall("GlobalSize", "Uint", hData) | |
VarSetCapacity(sData,1023), DllCall("wsprintf", "str", sData, "str", (DllCall("advapi32\IsTextUnicode", "Uint", pData, "Uint", nSize, "Uint", 0) & A_IsUnicode) ? "%s" : "%S", "Uint", pData, "Cdecl") | |
DllCall("GlobalUnlock", "Uint", hData) | |
If (CF_FORMAT = "FileNameW") | |
FileNameW := sData | |
} | |
Else { | |
RegExMatch("TYMED_NULL TYMED_FILE TYMED_ISTREAM TYMED_ISTORAGE TYMED_GDI TYMED_MFPICT TYMED_ENHMF", "(?:\w+\s+){" . Floor(ln(NumGet(StgMedium)+1)/ln(2)) . "}(?<STGMEDIUM>\w+\b)", TYMED_) | |
} | |
DllCall("ole32\ReleaseStgMedium","Uint",&StgMedium) | |
} | |
DllCall(NumGet(NumGet(1*penum)+8), "Uint", penum) | |
Return FileNameW | |
} | |
GetClipboardFormatName(nFormat) | |
{ | |
VarSetCapacity(sFormat, 255) | |
DllCall("GetClipboardFormatName", "Uint", nFormat, "str", sFormat, "Uint", 256) | |
Return sFormat | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 示例 - 拖拽高亮 | |
#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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment