Code archives/Miscellaneous/Clipboard - Text Copy & Paste
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
A couple of functions for reading and writing text to and from the clipboard. a$=ReadClipboardText() Opens the clipboard and passes any available text into a$. WriteClipboardText "Hello World!" Opens the clipboard and sends the message there clearing any previous text as it goes. Credits to the following coders: Ed from Mars - for clearing up a couple of userlib declarations. Darkuni - for tracking down potentional collisons when accessing the clipboard. NOTE: ===== You need to save a 'user32.decls' file into the userlibs path. See the declarations in the code. | |||||
; Clipboard Text Read / Write ; =========================== ; Syntax Error & Ed from Mars ; userlib declarations - 'user32.decls' ; ********************************************* ; .lib "user32.dll" ; OpenClipboard%(hwnd%):"OpenClipboard" ; CloseClipboard%():"CloseClipboard" ; ExamineClipboard%(format%):"IsClipboardFormatAvailable" ; EmptyClipboard%():"EmptyClipboard" ; GetClipboardData$(format%):"GetClipboardData" ; SetClipboardData%(format%,txt$):"SetClipboardData" ; ********************************************* Print "Clipboard Test." Print "~~~~~~~~~~~~~~~" Print "Enter a message for the clipboard." Print "Alternatively, leave BLANK to read clipboard." a$=Input$(">") If a$="" a$=ReadClipboardText$() Print a$ Else WriteClipboardText a$ Print "Text sent to clipboard. Open NotePad and paste!" EndIf Print Chr$(13)+"---------------------------------" a$=Input$("Press RETURN to end ...") End ;----------------------------------- Function WriteClipboardText(txt$) Local cb_TEXT=1 If txt$="" Then Return If OpenClipboard(0) EmptyClipboard SetClipboardText cb_TEXT,txt$ CloseClipboard EndIf End Function ;----------------------------------- Function ReadClipboardText$() Local cb_TEXT=1 Local txt$="" If OpenClipboard(0) If ExamineClipboard(cb_TEXT) txt$=GetClipboardText$(cb_TEXT) EndIf CloseClipboard EndIf Return txt$ End Function |
Comments
| ||
Does this work with Blitz3D? |
| ||
Yes. |
| ||
Hi, I tried your Clipboard - Text Copy & Paste. I found a problem in the declaration you used ;GetClipboardData$(format%):"GetClipboardData" But, in the function you called SetClipboardText cb_TEXT,txt$ I just changed the SetClipboardText to SetClipboardData as well as GetClipboardText$(cb_TEXT) should use the word data instead of text. That is all, thought to let you know so that you could change it. Great functions!!! |
| ||
Junk! Doesn't work. |
| ||
Print "Clipboard Test." Print "~~~~~~~~~~~~~~~" Print "Enter a message for the clipboard." Print "Alternatively, leave BLANK to read clipboard." a$=Input$(">") If a$="" a$=ReadClipboardText$() Print a$ Else WriteClipboardText a$ Print "Text sent to clipboard. Open NotePad and paste!" EndIf Print Chr$(13)+"---------------------------------" a$=Input$("Press RETURN to end ...") End ;----------------------------------- Function WriteClipboardText(txt$) Local cb_TEXT=1 If txt$="" Then Return If OpenClipboard(0) EmptyClipboard SetClipboarddata cb_TEXT,txt$ CloseClipboard EndIf End Function ;----------------------------------- Function ReadClipboardText$() Local cb_TEXT=1 Local txt$="" If OpenClipboard(0) If ExamineClipboard(cb_TEXT) txt$=GetClipboarddata$(cb_TEXT) EndIf CloseClipboard EndIf Return txt$ End Function |
| ||
The code above I've had trouble rolling out across multiple systems. There seem to be incompatiablities with Vista in some cases where users are not running as administrator and have separate user-space thingees turned on. :P (Even changing the DECLS and code to work off a bank instead.. it'd work on one system them not another). For a quick and easy way out, I grabbed the clipboard access DLL from here: http://www.icynorth.com/development/clipboard_dll.html (Public domain release: "You are free to use this DLL in any way that you wish without restrictions." from website, although I'd recommend attribution to Brett Kapilik.) After renaming the dll to "clipboard.dll" purely for testing, I created this clipboard.decls file: .lib "clipboard.dll" IsTextOnClipboard%():"IsTextOnClipboard" CopyTextToClipboard%(strText$):"CopyTextToClipboard" GetTextFromClipboard$():"GetTextFromClipboard" And some sample code: If IsTextOnClipboard() Then Print "Text already on the clipboard.." Print "but we're going To wipe it anyway..":Print End If SeedRnd MilliSecs() a$="**"+Str$(Rand(0,1000))+"**" Print "Writing "+a$+" to the clipboard.." CopyTextToClipboard a$ b$=GetTextFromClipboard$() Print "Read "+b$+" from the clipboard.." Print:If a$=b$ then print "SUCCESS!" WaitKey() End Unfortunately the DLL doesn't come with source so I have no idea if it's closing the reads and writes cleanly, though tracing its behaviour and using it extensively it appears to do exactly what it says on the box. +BlackD |
| ||
THANK YOU BlackD!!!!! You just saved my life! http://www.blitzbasic.com/Community/posts.php?topic=78874 (I think, more testing is needed but seems to work fine :) |
| ||
Hi Everyone, Ok here is something strange. I noticed that the good old copy to clipboard broke function in windows vista and above. I also tried BlackD Suggestion. Now when I ran the example it worked just fine. When I tried to use it in my app blitzplus would crash. I used it in another part of my program and it mysteriouly worked. After a major hunt blaming everything but the dll. It turns out that if you call the command "CopyTextToClipboard" and it encounters a "Return" Command it crashes with no warning( Blitz3D bounced me a Memory Access Violation). A way around this (For Me) is to put the Command in a Function instead, call it and when you use the "End Function" Command All Seems to work well. Now Can anyone explain why that is? Hope this can help someone with this nice dll. Happy Programming. |
| ||
Not broken for me - Here you go! |
| ||
Great stuff, works marvelously!(tested on Win7 Ultimate x64) Only I'd throw in an extra waitkey so people can see the text before the image covers it(or put text in after displaying the image): . . Print "Here are the clipboard graphics contents..." WaitKey ;<<< Add waitkey here.. Image2=PasteImageFromClipboard() ;Paste image from clipboard . . |
Code Archives Forum