GUI COMMANDS: COMPLETE RETHINK

Get help with using AutoHotkey and its commands and hotkeys
User avatar
hasantr
Posts: 747
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: GUI COMMANDS: COMPLETE RETHINK (latest: get/set system fonts, ComboBox choose string notify)

23 Aug 2019, 17:11

jeeswg wrote:
22 Apr 2017, 21:07
Some clipboard/hDrop functions:

JEE_ClipboardEnumFormats
JEE_ClipboardGetDropEffect
JEE_ClipboardGetPaths
JEE_ClipboardGetText
JEE_ClipboardSetPaths
JEE_ClipboardSetText
JEE_DropCreate
JEE_DropGetPaths

Code: Select all

;==================================================

JEE_ClipboardEnumFormats(vSep=",")
{
	DllCall("OpenClipboard", Ptr,0)
	vOutput := ""
	while vFormat := DllCall("EnumClipboardFormats", UInt,vFormat?vFormat:0)
		vOutput .= vFormat vSep
	DllCall("CloseClipboard")
	vOutput := SubStr(vOutput, 1, -StrLen(vSep))
	return vOutput
}

;==================================================

;e.g. vDropEffect := JEE_ClipboardGetDropEffect()
;e.g. vIsCopied := (vDropEffect & 1)
;e.g. vIsCut := (vDropEffect & 2) >> 1
;DROPEFFECT_LINK := 4, DROPEFFECT_MOVE := 2, DROPEFFECT_COPY := 1
;copied = 5, cut = 2, none = 0 (no files on clipboard = 0)

JEE_ClipboardGetDropEffect()
{
	static vPreferredDropEffect := DllCall("RegisterClipboardFormat", Str,"Preferred DropEffect")
	if !DllCall("IsClipboardFormatAvailable", UInt,vPreferredDropEffect)
		return 0
	if DllCall("OpenClipboard", Ptr,0)
	{
		hDropEffect := DllCall("GetClipboardData", UInt,vPreferredDropEffect, UPtr)
		pDropEffect := DllCall("GlobalLock", Ptr,hDropEffect, UPtr)
		vDropEffect := NumGet(pDropEffect+0, 0, "UChar")
		DllCall("GlobalUnlock", Ptr,hDropEffect)
		DllCall("CloseClipboard")
	}
	return vDropEffect
}

;==================================================

JEE_ClipboardGetPaths(vSep="`n")
{
	;CF_HDROP := 0xF
	if !DllCall("IsClipboardFormatAvailable", UInt,0xF)
		return ""
	if !DllCall("OpenClipboard", Ptr,0)
		return ""
	if !hDrop := DllCall("GetClipboardData", UInt,0xF)
	{
		DllCall("CloseClipboard")
		return ""
	}

	;==============================
	;same as JEE_DropGetPaths:
	vOutput := ""
	vCount := DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,-1, Ptr,0, UInt,0, UInt)
	Loop, % vCount
	{
		vSize := DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,A_Index-1, Ptr,0, UInt,0, UInt) + 1
		VarSetCapacity(vPath, vSize*(A_IsUnicode?2:1), 0)
		DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,A_Index-1, Str,vPath, UInt,vSize, UInt)
		vOutput .= vPath vSep
	}
	DllCall("shell32\DragFinish", "Ptr", hDrop)
	;==============================

	DllCall("CloseClipboard")
	return SubStr(vOutput, 1, -StrLen(vSep))
}

;==================================================

JEE_ClipboardGetText()
{
	;CF_LOCALE := 0x10 ;CF_UNICODETEXT := 0xD
	;CF_OEMTEXT := 0x7 ;CF_TEXT := 0x1
	if !DllCall("IsClipboardFormatAvailable", UInt,0xD)
		return ""
	if !DllCall("OpenClipboard", Ptr,0)
		return ""
	if !hBuf := DllCall("GetClipboardData", UInt,0xD)
	{
		DllCall("CloseClipboard")
		return ""
	}

	pBuf := DllCall("GlobalLock", Ptr,hBuf, Ptr)
	vSize := DllCall("GlobalSize", Ptr,hBuf)
	VarSetCapacity(vOutput, vSize, 0)
	DllCall("msvcrt\memcpy", Ptr,&vOutput, Ptr,pBuf, UPtr,vSize)
	VarSetCapacity(vOutput, -1)

	DllCall("GlobalUnlock", Ptr,hBuf)
	DllCall("CloseClipboard")
	return vOutput
}

;==================================================

JEE_ClipboardSetPaths(vPaths, vDropEffect=0x5)
{
	;DROPEFFECT_LINK := 4, DROPEFFECT_MOVE := 2, DROPEFFECT_COPY := 1
	(SubStr(vDropEffect, 1, 2) = "cu") ? (vDropEffect := 0x2) : "" ;cut
	(SubStr(vDropEffect, 1, 2) = "co") ? (vDropEffect := 0x5) : "" ;copy

	;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
	hDrop := DllCall("GlobalAlloc", UInt,0x42, UPtr,20+(StrLen(vPaths)+2)*2, Ptr)
	pDrop := DllCall("GlobalLock", Ptr,hDrop, Ptr)

	;DROPFILES struct
	NumPut(20, pDrop+0, 0, "UInt")
	NumPut(1, pDrop+16, 0, "UInt")

	;e.g. CF_HDROP with 3 paths: 'path1 null path2 null path3 null null'
	vOffset := 20
	Loop, Parse, vPaths, `n, `r
		if !(A_LoopField = "")
			vOffset += StrPut(A_LoopField, pDrop+vOffset, StrLen(A_LoopField)+1, "UTF-16")*2

	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("GetCurrentProcessId", UInt))

	;CF_HDROP := 0xF
	DllCall("GlobalUnlock", Ptr,hDrop)
	DllCall("OpenClipboard", Ptr,hWnd)
	DllCall("EmptyClipboard")
	DllCall("SetClipboardData", UInt,0xF, Ptr,hDrop)

	if (vDropEffect = 0)
	{
		DllCall("CloseClipboard")
		return
	}

	hDropEffect := DllCall("GlobalAlloc", UInt,0x42, UPtr,4, Ptr)
	pDropEffect := DllCall("GlobalLock", Ptr,hDropEffect)
	DllCall("RtlFillMemory", Ptr,pDropEffect, UPtr,1, UChar,vDropEffect)
	DllCall("GlobalUnlock", Ptr,hDropEffect)

	vFormat := DllCall("RegisterClipboardFormat", Str,"Preferred DropEffect")
	DllCall("SetClipboardData", UInt,vFormat, Ptr,hDropEffect)
	DllCall("CloseClipboard")
	return
}

;==================================================

JEE_ClipboardSetText(vText)
{
	;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
	hBuf := DllCall("GlobalAlloc", UInt,0x42, UPtr,(StrLen(vText)+2)*2, Ptr)
	pBuf := DllCall("GlobalLock", Ptr,hBuf, Ptr)
	StrPut(vText, pBuf, StrLen(vText)+1, "UTF-16")

	;CF_LOCALE := 0x10 ;CF_UNICODETEXT := 0xD
	;CF_OEMTEXT := 0x7 ;CF_TEXT := 0x1
	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("GetCurrentProcessId", UInt))
	DllCall("GlobalUnlock", Ptr,hBuf)
	DllCall("OpenClipboard", Ptr,hWnd)
	DllCall("EmptyClipboard")
	DllCall("SetClipboardData", UInt,0xD, Ptr,hBuf)
	DllCall("CloseClipboard")
	return
}

;==================================================

;e.g. hDrop := JEE_DropCreate(vPaths)

;where vPaths is an LF/CRLF-separated list
;JEE_HDropSetPaths
JEE_DropCreate(vPaths, vPosX=0, vPosY=0)
{
	;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
	vWidth := A_IsUnicode?2:1
	hDrop := DllCall("GlobalAlloc", UInt,0x42, UPtr,20+(StrLen(vPaths)+2)*vWidth, Ptr)
	pDrop := DllCall("GlobalLock", Ptr,hDrop)

	;DROPFILES struct
	NumPut(20, pDrop+0, 0, "UInt")
	NumPut(vPosX, pDrop+4, 0, "UInt")
	NumPut(vPosY, pDrop+8, 0, "UInt")
	NumPut(A_IsUnicode?1:0, pDrop+16, 0, "UInt")

	;e.g. CF_HDROP with 3 paths: 'path1 null path2 null path3 null null'
	vOffset := 20
	Loop, Parse, vPaths, `n, `r
		if !(A_LoopField = "")
		{
			DllCall("RtlMoveMemory", UInt,pDrop+vOffset, Str,A_LoopField, UInt,StrLen(A_LoopField)*vWidth)
			vOffset += (StrLen(A_LoopField)+1)*vWidth
		}

	DllCall("GlobalUnlock", Ptr,hDrop)
	return hDrop
}

;==================================================

JEE_DropGetPaths(hDrop, vSep="`n")
{
	vOutput := ""
	vCount := DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,-1, Ptr,0, UInt,0, UInt)
	Loop, % vCount
	{
		vSize := DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,A_Index-1, Ptr,0, UInt,0, UInt) + 1
		VarSetCapacity(vPath, vSize*(A_IsUnicode?2:1), 0)
		DllCall("shell32\DragQueryFile" (A_IsUnicode?"W":"A"), Ptr,hDrop, UInt,A_Index-1, Str,vPath, UInt,vSize, UInt)
		vOutput .= vPath vSep
	}
	DllCall("shell32\DragFinish", "Ptr", hDrop)
	return SubStr(vOutput, 1, -StrLen(vSep))
}

;==================================================
That's great. Thanks.

Return to “Ask For Help”

Who is online

Users browsing this forum: AmDeG 11, Bing [Bot], JeMykle, JoeWinograd, mikeyww, Rohwedder and 50 guests