Clipboard Accsess

BlitzMax Forums/BlitzMax Programming/Clipboard Accsess

d-bug(Posted 2005) [#1]
Does anybody know, why this doesn't work ?

Const CF_TEXT = 1

Extern "Win32"

Function OpenClipboard:Int (hwnd:Int)
Function CloseClipboard:Int ()
Function IsClipboardFormatAvailable:Int (format:Int)
Function EmptyClipboard:Int ()
Function GetClipboardData:String (format:Int)
Function SetClipboardData:Int (format:Int,str:String)

End Extern

Function ClipboardRead:String ()
Local clip:String

OpenClipboard 0
If IsClipboardFormatAvailable (CF_TEXT)
clip = GetClipBoardData (CF_TEXT)
EndIf
CloseClipboard
Return clip

End Function


Graphics 1024,768

txt:String = ""

Repeat
Cls

If KeyDown(Key_LControl) And KeyHit(Key_V) txt = ClipboardRead()
DrawText "< "+txt+" >",100,100

Flip
FlushMem
Until KeyHit(Key_Escape)

This is an one to one copy of the Blitz3D-Userlib-version.
It always returns an "Unhandled Memory Exception Error"...

Need help for this.


EOF(Posted 2005) [#2]
Extern "Win32" 

Function OpenClipboard:Int (hwnd:Int) 
Function CloseClipboard:Int () 
Function IsClipboardFormatAvailable:Int (format:Int) 
Function EmptyClipboard:Int () 
Function GetClipboardData(format:Int) 
Function SetClipboardData:Int (format:Int,str:String) 

End Extern 

Function ClipboardRead:String() 
	Const CF_TEXT = 1 
	Local clip:String
	Local cbd:Int
	If OpenClipboard(0) 
		If IsClipboardFormatAvailable(CF_TEXT) 
			cbd=GetClipBoardData(CF_TEXT)
			clip=clip.FromCString(Byte Ptr cbd)
		EndIf 
		CloseClipboard 
		Return clip
	EndIf
End Function 


Graphics 1024,768,0

Global txt:String = "" 

Repeat 
Cls 

If (KeyDown(Key_LControl) And KeyHit(Key_V)) txt = ClipboardRead() 
DrawText "< "+txt+" >",100,100 

Flip 
FlushMem 
Until KeyHit(Key_Escape) 



d-bug(Posted 2005) [#3]
Thank you JB,

this is great work. Where did you got the "FromCString" from ? Can't find it...


EOF(Posted 2005) [#4]
Welcome.
Have a look in Language Reference > Strings


Filax(Posted 2005) [#5]
Very usefull ! many thanks.


Filax(Posted 2005) [#6]
I have try to make a function to put text in clipboard but
seem don'"t work ?




Filax(Posted 2005) [#7]
I have find (thanks Birdie)




amonite(Posted 2005) [#8]
thanks for sharing d:bug and Filax :)


Filax(Posted 2005) [#9]
Nothing :)


taxlerendiosk(Posted 2005) [#10]
Bump - how do I get a pixmap from an image copied to the clipboard? I know that you have you use CF_BITMAP (2) instead of CF_TEXT, but I get a memory exception error if I try to do anything with the pointer I get back, do I have to do something with GlobalAlloc?

Also, some applications can accept pasted "files" (like MSN messenger opens the "send file" dialog if you copy a file icon in Windows Explorer and paste it into a conversation window.) How easy is that to do? I would've thought it's just a string containing the file path, though it isn't stored as CF_TEXT.


xMicky(Posted 2006) [#11]
Mysterious errors, something in filax code seems to be wrong or the cooperation between Blitzmax and MS Windows is the problem:
If I write ClipboardWrite("TEST") ONE time in source code then I can use ClipboardRead() as many times as desired.
But if I write earlier ClipboardWrite("TEST") (exactly) 4 times (simulating the user copying the text several times into clipboard) and then call ClipboardRead() several times, I got after some times "Unhandled memory exception error" in the line: cbd=GetClipBoardData(CF_TEXT).
And now if I use in the beginning ClipboardRead("TEST") 5 (!) times I got "Unhandled memory exception error" in line: DrawText "< "+txt+" >",100,100 even before I press any key, just directly after BM finished compiling !
I am working with BM 1.16 and MS Windows XP Version 5.1 Service Pack 2.


d-bug(Posted 2006) [#12]
This is a very old thread over here. In the meantime there a several new methods for doing this :D

Try this one or look over here

Extern "Win32"
   Function OpenClipboard(hwnd:Int)
   Function CloseClipboard()
   Function EmptyClipboard()
   Function SetClipboardData(format:Int,hMem:Byte Ptr)
   Function GlobalAlloc:Byte Ptr(uflags:Int,bytes:Int)
   Function GlobalFree(buffer:Byte Ptr )
End Extern

?win32
   Const GMEM_FIXED = 0
   Const CF_TEXT=$01

   Function TextToClipboard (txt:String)
      If txt <> ""
         Local CPTR:Byte Ptr = GlobalAlloc(GMEM_FIXED,Len(txt)+1)
         For Local i = 0 Until Len(txt)
            CPTR[i] = txt[i]
         Next
         CPTR[Len(txt)+1]=0
         If OpenClipboard(0)
            EmptyClipboard()
            SetClipboardData (CF_TEXT,CPTR)
            CloseClipboard()
         EndIf
         If CPTR Then GlobalFree (CPTR)
      EndIf
   End Function
?

'rem
TextToClipboard ("HubbaBubba schmeckt scheisse")
'end rem