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.
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)
Thank you JB,
this is great work. Where did you got the "FromCString" from ? Can't find it...
Welcome.
Have a look in Language Reference > Strings
Very usefull ! many thanks.
I have try to make a function to put text in clipboard but
seem don'"t work ?
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
Function ClipboardWrite(Text:String)
Const CF_TEXT = 1
Local cbd:Int
If OpenClipboard(0)
cbd=SetClipBoardData(CF_TEXT,Text)
CloseClipboard
EndIf
End Function
Graphics 1024,768,0
Global txt:String = ""
ClipboardWrite("TEST")
Repeat
Cls
If (KeyDown(Key_LControl) And KeyHit(Key_V)) txt = ClipboardRead()
DrawText "< "+txt+" >",100,100
Flip
FlushMem
Until KeyHit(Key_Escape)
I have find (thanks Birdie)
Extern "Win32"
Function OpenClipboard:Int (hwnd:Int)
Function ReadClipboard:Int (hwnd:Int)
Function CloseClipboard:Int ()
Function IsClipboardFormatAvailable:Int (format:Int)
Function EmptyClipboard:Int ()
Function GetClipboardData(format:Int)
Function SetClipboardData:Int (format:Int,hMem:Byte Ptr)
Function GlobalAlloc:Byte Ptr( uflags, bytes )
Function GlobalFree( buf:Byte Ptr )
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
Function ClipboardWrite(Text:String)
Const CF_TEXT = 1
Local cbd:Int
Local hbuf:Byte Ptr
If OpenClipboard(0)
EmptyClipboard
hbuf = GlobalAllocString(Text)
cbd=SetClipBoardData(CF_TEXT, hbuf)
CloseClipboard
GlobalFree( hbuf )
EndIf
End Function
Function GlobalAllocString:Byte Ptr( txt$ )
Local p:Byte Ptr= GlobalAlloc( GMEM_FIXED , Len(txt)+1 )
For Local i=0 Until Len(txt)
p[i]=txt[i]
Next
p[Len(txt)+1]=0
Return p
EndFunction
Graphics 1024,768,0
Global txt:String = ""
ClipboardWrite("TEST")
Repeat
Cls
If (KeyDown(Key_LControl) And KeyHit(Key_V)) txt = ClipboardRead()
DrawText "< "+txt+" >",100,100
Flip
FlushMem
Until KeyHit(Key_Escape)
thanks for sharing d:bug and Filax :)
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.
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.
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
hereExtern "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