udfClipGetApp
str udfClipGetApp ()
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfClipGetApp () ; The DllCall - Binary Buffer version.
hdlUser32 = DllLoad (StrCat (DirWindows(1), "USER32.DLL"))

; Get handle to the window that currently has the clipboard open.
hdlWnd = DllCall (hdlUser32, long:"GetOpenClipboardWindow")
If !hdlWnd Then Return "" ; Clipboard is not assigned.

; Get the application pathname.
intBBSize = 260 ; MAX_PATH=260
hdlBB = BinaryAlloc (intBBSize)
BinaryEodSet (hdlBB, intBBSize)
intBBSize = DllCall (hdlUser32, long:"GetWindowModuleFileNameA", long:hdlWnd, lpbinary:hdlBB, long:intBBSize)
strGetWindowModuleFileName = FileNameLong (BinaryPeekStr (hdlBB, 0, intBBSize))
hdlBB = BinaryFree (hdlBB)

; Get text from the window title bar.
intBBSize = 1 + DllCall (hdlUser32, long:"GetWindowTextLengthA", long:hdlWnd) ; Sized one more byte for sure.
hdlBB = BinaryAlloc (intBBSize)
BinaryEodSet (hdlBB, intBBSize)
intBBSize = DllCall (hdlUser32, long:"GetWindowTextA", long:hdlWnd, lpbinary:hdlBB, long:intBBSize)
strGetWindowText = BinaryPeekStr (hdlBB, 0, intBBSize)
hdlBB = BinaryFree (hdlBB)

; Convert window handle to WinBatch WinId pseudo-handle.
hdlBB = BinaryAlloc (4)
BinaryPoke4 (hdlBB, 0, hdlWnd)
strWinId = StrCat ("#WIN$ID#", BinaryPeekHex (hdlBB, 3, 1), BinaryPeekHex (hdlBB, 2, 1), BinaryPeekHex (hdlBB, 1, 1), BinaryPeekHex (hdlBB, 0, 1))
hdlBB = BinaryFree (hdlBB)

strList = hdlWnd
strList = ItemInsert (strWinId, -1, strList, @TAB)
strList = ItemInsert (strGetWindowModuleFileName, -1, strList, @TAB)
strList = ItemInsert (strGetWindowText, -1, strList, @TAB)

hdlUser32 = DllFree (hdlUser32)
Return strList
;..........................................................................................................................................
; This UDF "udfClipGetApp" returns a tab delimited list of four items.
; Item1 ... The handle to the window that currently has the clipboard open.
; Item2 ... The WinId pseudo handle to the window that currently has the clipboard open.
; Item3 ... The (full path and file-) name of the module associated with the specified window handle.
; Item4 ... The text of the specified window's title bar.
;           If the window does not have a caption, Then the return value is a null string.
;
; If there is no application to detect currently using the clipboard,
; Then "udfClipGetApp" returns an empty string.
;..........................................................................................................................................
; Detlev Dalitz.20020701.20090429.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfClipGetApp2 () ; The Control Manager Extender version.

; Get handle to the window that currently has the clipboard open.
hdlWnd = DllCall ("USER32.DLL", long:"GetOpenClipboardWindow")
If !hdlWnd Then Return "" ; Clipboard is not assigned.

AddExtender ("WWCTL44I.DLL") ; Control Manager Extender
strGetWindowText = cWndinfo (hdlWnd, 0)
strWinId = cWinIDConvert (hdlWnd)
strGetWindowModuleFileName = WinExename (strWinId)

strList = hdlWnd
strList = ItemInsert (strWinId, -1, strList, @TAB)
strList = ItemInsert (strGetWindowModuleFileName, -1, strList, @TAB)
strList = ItemInsert (strGetWindowText, -1, strList, @TAB)

Return strList
;..........................................................................................................................................
; This UDF "udfClipGetApp" returns a tab delimited list of four items.
; Item1 ... The handle to the window that currently has the clipboard open.
; Item2 ... The WinId pseudo handle to the window that currently has the clipboard open.
; Item3 ... The (full path and file-) name of the module associated with the specified window handle.
; Item4 ... The text of the specified window's title bar.
;           If the window does not have a caption, Then the return value is a null string.
;
; If there is no application to detect currently using the clipboard,
; Then "udfClipGetApp" returns an empty string.
;..........................................................................................................................................
; Detlev Dalitz.20030711
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strMsgTitle = "Demo: udfClipGetApp ()"

; Part 1
; We need an application which makes repeatedly use of the clipboard.
strClpBrdTestApp = FileCreateTemp ("TMP")
FileDelete (strClpBrdTestApp)
strClpBrdTestApp = StrCat (FilePath (strClpBrdTestApp), "looptest.wbt")
hdlFW = FileOpen (strClpBrdTestApp, "WRITE")
FileWrite (hdlFW, ';Testloop for udfGetClipbApp ()')
FileWrite (hdlFW, 'TimeDelay (5)')
FileWrite (hdlFW, '')
FileWrite (hdlFW, 'DllUser32 = StrCat (DirWindows (1), "USER32.DLL")')
FileWrite (hdlFW, '')
FileWrite (hdlFW, 'TitleOld = "Clipboard Test Loop is running ..."')
FileWrite (hdlFW, 'BoxOpen (TitleOld, "Stop it with (@CTRL & @SHIFT)")')
FileWrite (hdlFW, 'WinPlace (500, 0, 900, 200, TitleOld)')
FileWrite (hdlFW, '')
FileWrite (hdlFW, ';Enable Close button')
FileWrite (hdlFW, 'IntControl (1008, 1, 0, 0, 0)')
FileWrite (hdlFW, ';Tell WIL not to complain when box is closed')
FileWrite (hdlFW, 'IntControl (12, 1+4, 0, 0, 0)')
FileWrite (hdlFW, '')
FileWrite (hdlFW, 'i=0')
FileWrite (hdlFW, 'While 1')
FileWrite (hdlFW, '   i = i + 1')
FileWrite (hdlFW, '   TitleNew = StrCat (i, " - The Clipboard is mine. Stop it with (@CTRL & @SHIFT).")')
FileWrite (hdlFW, '   WinTitle (TitleOld, TitleNew)')
FileWrite (hdlFW, '   TitleOld = TitleNew')
FileWrite (hdlFW, '   hdlWndNewOwner = DllHwnd (TitleNew)')
FileWrite (hdlFW, '   bool = DLLCall (DllUser32, long:"OpenClipboard", long:hdlWndNewOwner)')
FileWrite (hdlFW, '   bool = DLLCall (DllUser32, long:"EmptyClipboard")')
FileWrite (hdlFW, '   TimeDelay (5)')
FileWrite (hdlFW, '   bool = DLLCall (DllUser32, long:"CloseClipboard")')
FileWrite (hdlFW, '')
FileWrite (hdlFW, '   If IsKeyDown (@CTRL & @SHIFT) Then Break')
FileWrite (hdlFW, '   If !FileExist ("%strClpBrdTestApp%") Then Break')
FileWrite (hdlFW, 'EndWhile')
FileWrite (hdlFW, '')
FileWrite (hdlFW, 'BoxShut ()')
FileWrite (hdlFW, 'Message ("Clipboard Test Loop", "Done.")')
FileWrite (hdlFW, 'Exit')
hdlFW = FileClose (hdlFW)
Run (StrCat (DirHome (), "winbatch.exe"), strClpBrdTestApp)


; Part2
While @TRUE
   strList = udfClipGetApp ()  ; udfClipGetApp with DllCalls and Binary Buffers.
   ;strList = udfClipGetApp2 () ; udfClipGetApp with Control Manager Extender.
   If strList != ""
      strMsgText = ""
      strMsgText = StrCat (strMsgText, "Handle to the window that currently has the clipboard open.", @LF, ItemExtract (1, strList, @TAB), @LF, @LF)
      strMsgText = StrCat (strMsgText, "The window handle converted to WinID.", @LF, ItemExtract (2, strList, @TAB), @LF, @LF)
      strMsgText = StrCat (strMsgText, "The application module associated with the specified window handle.", @LF, ItemExtract (3, strList, @TAB), @LF, @LF)
      strMsgText = StrCat (strMsgText, "The text of the specified window's title bar.", @LF, ItemExtract (4, strList, @TAB), @LF, @LF)
   Else
      strMsgText = "Clipboard is currently not used by any application."
   EndIf
   Pause (strMsgTitle, strMsgText)
EndWhile

:CANCEL
FileDelete (strClpBrdTestApp)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
; Test output
;
; strList = "5833894@TAB#WIN$ID#005904A6@TABD:\ProgPROG\WinBatch\System\WinBatch Studio.exe@TAB1 - The Clipboard is mine. Stop it with (@CTRL & @SHIFT)."
;
; Item1 = "5833894"                                                   ; Window handle.
; Item2 = "#WIN$ID#005904A6"                                          ; WinId pseudo handle.
; Item3 = "D:\Programs\WinBatch\System\WinBatch Studio.exe"           ; Associated module.
; Item4 = "1 - The Clipboard is mine. Stop it with (@CTRL & @SHIFT)." ; Window title.
;------------------------------------------------------------------------------------------------------------------------------------------