Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version

WinBatch Scripting - Clipboard Managing




Seitenanfang/TopOfPage Seitenende/EndOfPage Seitenende/EndOfPage Seitenanfang/TopOfPage

udfGetClipFormatName (iFormatID)

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetclipformatname",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetclipformatname

#DefineFunction udfGetClipFormatName (iFormatID)
IntControl(73,2,0,0,0) ; GoSub WBERRORHANDLER.
sCFName = ""
GoSub CFID_%iFormatID%
; If there is no predefined format found in our list, then we ask the Clipboard for the registered name.
If (sCFName == "")
   iBBSize = 256
   hBB = BinaryAlloc(iBBSize)
   BinaryEodSet(hBB,iBBSize)
   iResult = DllCall("USER32.DLL",long:"GetClipboardFormatNameA",long:iFormatID,lpbinary:hBB,long:iBBSize)
   If (iResult > 0) Then sCFName = BinaryPeekStr(hBB,0,iResult)
   BinaryFree(hBB)
EndIf
Return (sCFName)
;..........................................................................................................................................
;PREDEFINED CLIPBOARD FORMATS
:CFID_1
sCFName="CF_TEXT"
Return ; from GoSub
:CFID_2
sCFName="CF_BITMAP"
Return ; from GoSub
:CFID_3
sCFName="CF_METAFILEPICT"
Return ; from GoSub
:CFID_4
sCFName="CF_SYLK"
Return ; from GoSub
:CFID_5
sCFName="CF_DIF"
Return ; from GoSub
:CFID_6
sCFName="CF_TIFF"
Return ; from GoSub
:CFID_7
sCFName="CF_OEMTEXT"
Return ; from GoSub
:CFID_8
sCFName="CF_DIB"
Return ; from GoSub
:CFID_9
sCFName="CF_PALETTE"
Return ; from GoSub
:CFID_10
sCFName="CF_PENDATA"
Return ; from GoSub
:CFID_11
sCFName="CF_RIFF"
Return ; from GoSub
:CFID_12
sCFName="CF_WAVE"
Return ; from GoSub
:CFID_13
sCFName="CF_UNICODETEXT"
Return ; from GoSub
:CFID_14
sCFName="CF_ENHMETAFILE"
Return ; from GoSub
:CFID_15
sCFName="CF_HDROP"
Return ; from GoSub
:CFID_16
sCFName="CF_LOCALE"
Return ; from GoSub
:CFID_128
sCFName="CF_OWNERDISPLAY"
Return ; from GoSub
:CFID_129
sCFName="CF_DSPTEXT"
Return ; from GoSub
:CFID_130
sCFName="CF_DSPBITMAP"
Return ; from GoSub
:CFID_131
sCFName="CF_DSPMETAFILEPICT"
Return ; from GoSub
:CFID_142
sCFName="CF_DSPENHMETAFILE"
Return ; from GoSub
:WBERRORHANDLER
Return ; from GoSub
;..........................................................................................................................................
;   Detlev Dalitz.20030119
;..........................................................................................................................................
#EndFunction

:skip_udfgetclipformatname
;------------------------------------------------------------------------------------------------------------------------------------------



; --- test ---

sFormatList = ""
iCount = 0
iResult = 0
If DllCall("USER32.DLL",long:"OpenClipboard",long:DllHwnd(""))
   While @TRUE
      iResult = DllCall("USER32.DLL",long:"EnumClipboardFormats",long:iResult)
      If (iResult == 0) Then Break
      iCount = iCount + 1
      sItem = StrCat(iCount,":",@TAB,StrFixLeft(iResult," ",5)," = ",udfGetClipFormatName(iresult))
      sFormatList = ItemInsert(sItem,-1,sFormatList,@LF)
   EndWhile
EndIf
iResult = DllCall("USER32.DLL",long:"CloseClipboard")

IntControl(28,1,0,0,0) ; Select fixed pitch font in listboxes.
AskItemlist("Current Clipboard Formats",sFormatList,@LF,@UNSORTED,@SINGLE)

:CANCEL
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;   If someone wants to know what the current data format type is on the clipboard,
;   e.g. Bitmap or Rich Text or whatever else, then this script can help.
;
;   This script shows how to determine all the formats currently available on the clipboard,
;   including the customised formats.
;
;   To see custom formats in the list, try copying something from
;   Notepad, Word, Write or Internet Explorer before running this script.
;
;   Example (from the Windows Explorer)
;      1:   52900 = DataObject
;      2:   51586 = Shell IDList Array
;      3:   54218 = Preferred DropEffect
;      4:   54154 = Shell Object Offsets
;      5:      15 = CF_HDROP
;      6:   50782 = FileName
;      7:   52918 = Ole Private Data
;
;   Example (from the Internet Explorer)
;      1:   52813 = DataObject
;      2:       1 = CF_TEXT
;      3:      13 = CF_UNICODETEXT
;      4:   54320 = HTML Format
;      5:   52372 = Rich Text Format
;      6:   52851 = Ole Private Data
;      7:      16 = CF_LOCALE
;      8:       7 = CF_OEMTEXT
;
;..........................................................................................................................................
;   Detlev Dalitz.20030119
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*




Seitenanfang/TopOfPage Seitenende/EndOfPage Seitenende/EndOfPage Seitenanfang/TopOfPage

udfGetClipIDForCustomFormat (sFormatName)

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetclipidforcustomformat",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetclipidforcustomformat

#DefineFunction udfGetClipIDForCustomFormat (sFormatName)
iFormatID = DllCall("USER32.DLL",long:"RegisterClipboardFormatA",lpstr:sFormatName)
If (iFormatID < 49152) Then iFormatID = 0
Return (iFormatID)
;..........................................................................................................................................
; The RegisterClipboardFormat function registers a new clipboard format.
; Registered clipboard formats are identified by values in the range 0xC000 (49152) through 0xFFFF (65535).
; This format can then be used as a valid clipboard format.
;
; If the function succeeds, the return value identifies the registered clipboard format.
; If the function fails, the return value is zero.
;
; If a registered format with the specified name already exists,
; a new format is not registered and the return value identifies the existing format.
; This enables more than one application to copy and paste data using the same registered clipboard format.
; Note that the format name comparison is case-insensitive.
;..........................................................................................................................................
; Detlev Dalitz.20030119
;..........................................................................................................................................
#EndFunction

:skip_udfgetclipidforcustomformat
;------------------------------------------------------------------------------------------------------------------------------------------



; --- test ---

iFormatID1 = udfGetClipIDForCustomFormat ("CF_TEXT")          ; ==> 57379
iFormatID2 = udfGetClipIDForCustomFormat ("CF_LOCALE")        ; ==> 57432
iFormatID3 = udfGetClipIDForCustomFormat ("Rich Text Format") ; ==> 52096
iFormatID4 = udfGetClipIDForCustomFormat ("xxxxxxxx")         ; ==> 57489
iFormatID5 = udfGetClipIDForCustomFormat ("XXXXXXXX")         ; ==> 57489
iFormatID6 = udfGetClipIDForCustomFormat ("xxx")              ; ==> 57485

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*




Seitenanfang/TopOfPage Seitenende/EndOfPage Seitenende/EndOfPage Seitenanfang/TopOfPage

udfGetClipDataAsString (hWndOwner, iFormatID, sNullChar)

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetclipidforcustomformat",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetclipidforcustomformat

#DefineFunction udfGetClipIDForCustomFormat (sFormatName)
iFormatID = DllCall("USER32.DLL",long:"RegisterClipboardFormatA",lpstr:sFormatName)
If (iFormatID < 49152) Then iFormatID = 0
Return (iFormatID)
;..........................................................................................................................................
; The RegisterClipboardFormat function registers a new clipboard format.
; Registered clipboard formats are identified by values in the range 0xC000 (49152) through 0xFFFF (65535).
; This format can then be used as a valid clipboard format.
;
; If the function succeeds, the return value identifies the registered clipboard format.
; If the function fails, the return value is zero.
;
; If a registered format with the specified name already exists,
; a new format is not registered and the return value identifies the existing format.
; This enables more than one application to copy and paste data using the same registered clipboard format.
; Note that the format name comparison is case-insensitive.
;..........................................................................................................................................
; Detlev Dalitz.20030119
;..........................................................................................................................................
#EndFunction

:skip_udfgetclipidforcustomformat
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetClipDataAsString (hWndOwner, iFormatID, sNullChar)
sString = ""
; Open the clipboard for access.
If !!DllCall("USER32.DLL",long:"OpenClipboard",long:hWndOwner)
   ; Check if this data format is available.
   If !!DllCall("USER32.DLL",long:"IsClipboardFormatAvailable",long:iFormatID)
      ; Get the memory handle to the data.
      hMem = DllCall("USER32.DLL",long:"GetClipboardData",long:iFormatID)
      If !!hMem
         ; Get the size of this memory block.
         iSize = DllCall("KERNEL32.DLL",long:"GlobalSize",long:hMem)
         If !!iSize
            ; Get a pointer to the memory.
            iPtr = DllCall("KERNEL32.DLL",long:"GlobalLock",long:hMem)
            If !!iPtr
               ; Allocate a binary buffer to hold the data.
               hBB = BinaryAlloc(iSize)
               BinaryEodSet(hBB,iSize)
               iptrBB = IntControl(42,hBB,0,0,0)

               ; Copy from the pointer into the binary buffer.
               iResult = DllCall("KERNEL32.DLL",long:"RtlMoveMemory",long:iptrBB,long:iPtr,long:iSize)

               ; Unlock the memory block.
               iResult = DllCall("KERNEL32.DLL",long:"GlobalUnlock",long:hMem)

               ; Now get the data from binary buffer as a string.
               If (sNullChar>"") Then BinaryReplace(hBB,"",StrSub(sNullChar,1,1),@TRUE)
               sString = BinaryPeekStr(hBB,0,iSize)
               BinaryFree(hBB)
            EndIf
         EndIf
      EndIf
   EndIf
   DllCall("USER32.DLL",long:"CloseClipboard")
EndIf
Return (sString)
;..........................................................................................................................................
; This function udfGetClipDataAsString returns the current clipboard data as a string.
;
; Parameter:
; hWndOwner ... Handle to the window to be associated with the open clipboard.
;               If this parameter is NULL, the open clipboard is associated with the current task.
;               In general you can apply the WinBatch function DllHwnd("") as current hWndOwner.
;
; iFormatID ... Specifies the type of format to be retrieved.
;               This parameter must not specify any of the predefined clipboard formats.
;
; sNullChar ... Depending on the current data format the clipboard data may contain binary zeroes.
;               The parameter sNullChar controls how to handle those binary zeroes.
;               sNullChar specifies a 1-byte character that replaces a NULL byte in the returning string.
;               If sNullChar is an empty string, then NULL bytes will not be removed.
;               For text/html data the sNullChar can be an empty string as default.
;..........................................................................................................................................
; Detlev Dalitz.20030119
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; --- test ---

; The following code will get any information pasted onto the clipboard in HTML Format.
; HTML Format is pasted by Internet Explorer, and gives you an entire HTML document
; based on the selection you have copied to the clipboard.


iFormatID = udfGetClipIDForCustomFormat("HTML Format")
If !!iFormatID
   sText = udfGetClipDataAsString(DllHwnd(""),iFormatID,"")
   ; Take a look with WinBatch browser.
   sFilename = FileCreateTemp("TMP")
   FilePut(sFilename,sText)
   If Run(StrCat(DirHome(),"browser.exe"),sFilename)
      sBrowser = WinGetactive()
      SendKeysTo(sBrowser,"^t")
      WinWaitClose(sBrowser)
   EndIf
   ; Note: If the file is empty, then you might have forgotten
   ; to copy some html related data to the clipboard.
   If FileExist(sFilename) Then FileDelete(sFilename)
EndIf

:CANCEL
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*




Seitenanfang/TopOfPage Seitenende/EndOfPage Seitenende/EndOfPage Seitenanfang/TopOfPage

udfGetClipApp ()

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetclipapp",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetclipapp

#DefineFunction udfGetClipApp ()

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

; Get the application pathname.
iBBSize = 260 ; MAX_PATH=260
hBB = BinaryAlloc(iBBSize)
BinaryEodSet(hBB,iBBSize)
iBBSize = DllCall("USER32.DLL",long:"GetWindowModuleFileNameA",long:hWnd,lpbinary:hBB,long:iBBSize)
sGetWindowModuleFileNameA = FileNameLong(BinaryPeekStr(hBB,0,iBBSize))
BinaryFree(hBB)

; Get text from the window title bar.
iBBSize = 1 + DllCall("USER32.DLL",long:"GetWindowTextLengthA",long:hWnd) ; Sized one more byte for sure.
hBB = BinaryAlloc(iBBSize)
BinaryEodSet(hBB,iBBSize)
iBBSize = DllCall("USER32.DLL",long:"GetWindowTextA",long:hWnd,lpbinary:hBB,long:iBBSize)
sGetWindowTextA = BinaryPeekStr(hBB,0,iBBSize)
BinaryFree(hBB)

sList = hWnd
sList = ItemInsert(sGetWindowModuleFileNameA,-1,sList,@TAB)
sList = ItemInsert(sGetWindowTextA,-1,sList,@TAB)

Return (sList)
;..........................................................................................................................................
; This function "udfGetClipApp" returns a tab delimited list of three items.
; item1 ... Handle to the window that currently has the clipboard open.
; Item2 ... The (full path- and file-) name of the module associated with the specified window handle.
; Item3 ... The text of the specified window's title bar.
;           If the window does not have a caption, the return value is a null string.
;
; If there is no application to detect currently using the clipboard,
; then "udfGetClipApp" returns an empty string.
;..........................................................................................................................................
; Detlev Dalitz.20020701
;..........................................................................................................................................
#EndFunction

:skip_udfgetclipapp
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfgetclipapp_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfgetclipapp_2

#DefineFunction udfGetClipApp_2 ()

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

AddExtender("WWCTL34I.DLL") ; Control Manager Extender
sGetWindowText = cWndinfo(hWnd,0)
sWinId = cWinIDConvert(hWnd)
sGetWindowModuleFileName = WinExename(sWinId)

sList = hWnd
sList = ItemInsert(sWinId,-1,sList,@TAB)
sList = ItemInsert(sGetWindowModuleFileName,-1,sList,@TAB)
sList = ItemInsert(sGetWindowText,-1,sList,@TAB)

Return (sList)
;..........................................................................................................................................
; This function "udfGetClipApp" 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, the return value is a null string.
;
; If there is no application to detect currently using the clipboard,
; then "udfGetClipApp" returns an empty string.
;..........................................................................................................................................
; Detlev Dalitz.20030711
;..........................................................................................................................................
#EndFunction

:skip_udfgetclipapp_2
;------------------------------------------------------------------------------------------------------------------------------------------


; --- test ---

sMsgTitle = "Demo  udfGetClipApp()"

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


; Part2
While @TRUE
   ;sList = udfGetClipApp()    ; udfGetClipApp with DllCalls.
   sList = udfGetClipApp_2()  ; udfGetClipApp with Control Manager Extender.
   If sList>""
      sMsgText = ""
      sMsgText = StrCat(sMsgText,"Handle to the window that currently has the clipboard open.",@LF,ItemExtract(1,sList,@TAB),@LF,@LF)
      sMsgText = StrCat(sMsgText,"The window handle converted to WinID.",@LF,ItemExtract(2,sList,@TAB),@LF,@LF)
      sMsgText = StrCat(sMsgText,"The application module associated with the specified window handle.",@LF,ItemExtract(3,sList,@TAB),@LF,@LF)
      sMsgText = StrCat(sMsgText,"The text of the specified window's title bar.",@LF,ItemExtract(4,sList,@TAB),@LF,@LF)
   Else
      sMsgText = "Clipboard is currently not used by any application."
   EndIf
   Pause(sMsgTitle,sMsgText)
EndWhile

:CANCEL
FileDelete(sClpBrdTestApp)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*




Seitenanfang/TopOfPage Seitenende/EndOfPage Seitenende/EndOfPage Seitenanfang/TopOfPage

udfSetClipEmpty ()

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfsetclipempty",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsetclipempty

#DefineFunction udfSetClipEmpty ()
If !!DllCall("USER32.DLL",long:"OpenClipboard",long:DllHwnd("")) Then Return !!DllCall("USER32.DLL",long:"EmptyClipboard") && !!DllCall("USER32.DLL",long:"CloseClipboard")
Return 0
;..........................................................................................................................................
; This function udfSetClipEmpty() returns a boolean value to indicate if the emptying was successful or not.
; The effect of emptying the clipboard by this function udfSetClipEmpty()
; can also easily be realized in WinBatch by applying the one statement ClipPut("").
;
; Remarks from MS SDK:
; The EmptyClipboard function empties the clipboard and frees handles to data in the clipboard.
; The function then assigns ownership of the clipboard to the window that currently has the clipboard open.
;
; Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function.
; If the application specifies a NULL window handle when opening the clipboard,
; EmptyClipboard succeeds but sets the clipboard owner to NULL.
;..........................................................................................................................................
; Detlev Dalitz.20030119
;..........................................................................................................................................
#EndFunction

:skip_udfsetclipempty
;------------------------------------------------------------------------------------------------------------------------------------------



; --- test ---
ClipPut("If you later see this message again, then emptying of the clipboard did not work.")
Message("Demo  udfSetClipEmpty() - PRE-EMPTYING",ClipGet())
iBool = udfSetClipEmpty()
Message("Demo  udfSetClipEmpty() - POST-EMPTYING",ClipGet())
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*





Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version