Ooops
November 28, 2020, 02:26:33 PM

Author Topic: [bmx] Clipboard Module by JoshK [ 1+ years ago ]  (Read 750 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Clipboard Module by JoshK [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Clipboard Module
Author : JoshK
Posted : 1+ years ago

Description : The GadgetCopy() commands are better for text, but this will paste pixmaps to the clipboard as well.

Code :
Code: BlitzMax
  1. ' Clipboard Text - Copy/Paste functions
  2. Strict
  3.  
  4. Module leadwerks.clipboard
  5. ModuleInfo "Version: 1.0.0"
  6. ModuleInfo "Author: Joshua Klint"
  7. ModuleInfo "www.leadwerks.com"
  8.  
  9. Import brl.pixmap
  10. Import pub.win32
  11.  
  12. Extern "Win32"
  13.         Function OpenClipboard%(hwnd%)
  14.         Function CloseClipboard%()
  15.         Function EmptyClipboard%()
  16.         Function IsClipboardFormatAvailable%(format%)
  17.         Function GetClipboardData:Byte Ptr(Format:Int)
  18.         Function SetClipboardData(format%, hMem:Byte Ptr)
  19.         Function GlobalAlloc(Flags:Int, Bytes:Int)
  20.         Function GlobalFree(Mem:Int)
  21.         Function GlobalLock:Byte Ptr(Mem:Int)
  22.         Function GlobalUnlock(Mem:Int)
  23.         Function CreateBitmap:Byte Ptr(width:Int,height:Int,colorplanes:Int,bpp:Int,data:Byte Ptr)
  24. End Extern
  25.  
  26. Const CF_TEXT%=$1
  27. Const CF_BITMAP%=2
  28. Const GMEM_MOVEABLE%=$2
  29. Const GMEM_DDESHARE%=$2000
  30.  
  31. ' -----------------------------------------------
  32.  
  33. Function ClipboardText:String()
  34.         If Not OpenClipboard(0) Return ""
  35.         Local TextBuf:Byte Ptr = GetClipboardData(CF_TEXT)
  36.         CloseClipboard()
  37.         Return String.FromCString(TextBuf)
  38. End Function
  39.        
  40. Function SetClipboardText:Int(txt:String)
  41.         Local result:Int=False
  42.         If txt$="" Return
  43.         Local TextBuf:Byte Ptr = Txt.ToCString()
  44.         Local Memblock:Int = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, txt.Length+1)
  45.         Local DataBuf:Byte Ptr = GlobalLock(Memblock)
  46.         MemCopy DataBuf, TextBuf, Txt.length
  47.         If OpenClipboard(0)
  48.                 EmptyClipboard
  49.                 SetClipboardData CF_TEXT, DataBuf
  50.                 CloseClipboard
  51.                 result=True
  52.         EndIf
  53.         GlobalUnlock Memblock
  54.         GlobalFree Memblock
  55.         Return result
  56. End Function
  57.  
  58. Function SetClipboardPixmap:Int(pixmap:TPixmap)
  59.         If Not pixmap Return
  60.         Local result:Int=False
  61.         Local hbitmap:Byte Ptr
  62.         If pixmap.format<>PF_BGRA8888 pixmap=ConvertPixmap(pixmap,PF_BGRA8888)
  63.         If Not pixmap Return
  64.         hbitmap=CreateBitmap(pixmap.width,pixmap.height,1,32,pixmap.pixels)
  65.         If Not hbitmap Return
  66.         If OpenClipboard(0)
  67.                 EmptyClipboard
  68.                 SetClipboardData CF_BITMAP,hbitmap
  69.                 CloseClipboard
  70.                 result=True
  71.         EndIf
  72.         DeleteObject Int(hbitmap)
  73.         Return result
  74. EndFunction


Comments :


TaskMaster(Posted 1+ years ago)

 Leadwerks, are you still using this code?Does it always work for you?It fails about half the time for me, and also sometimes makes my app crash.I am only using the SetClipboardText function.


xmlspy(Posted 1+ years ago)

 How can this be changed to allow formatted text such as text in text areas?


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal