From: William Sobel Date: 2001-12-29T09:29:37+09:00 Subject: [ruby-talk:29607] Re: win32api docs "Henning von Rosen" writes: > Hi! > > Where can I find a pointer to some docs that would make the Win32API class > of use? > > Really, all I'd like to do is to read what is in the clipboard, process it > and put it back. > please, if you know any pointers of use or illustrative examples, post them! > > /H Here's a _small_ clipboard class. It turned out to be harder than I first suspected because of the GlobalAlloc crap. Anyway it works with text, (I haven't tested with anything else), and allows you to manipulate the clipboard. There is a clipboard view with messages and the like to respond whenever a change is made to the clipboard. That is much more in depth and requires a window message loop and some more obscure incantations... - Will require 'Win32API' class Clipboard CF_TEXT = 1 CF_BITMAP = 2 CF_METAFILEPICT = 3 CF_SYLK = 4 CF_DIF = 5 CF_TIFF = 6 CF_OEMTEXT = 7 CF_DIB = 8 CF_PALETTE = 9 CF_PENDATA = 10 CF_RIFF = 11 CF_WAVE = 12 CF_UNICODETEXT = 13 CF_ENHMETAFILE = 14 CF_HDROP = 15 CF_LOCALE = 16 CF_MAX = 17 CF_OWNERDISPLAY = 0x0080 CF_DSPTEXT = 0x0081 CF_DSPBITMAP = 0x0082 CF_DSPMETAFILEPICT = 0x0083 CF_DSPENHMETAFILE = 0x008E CF_PRIVATEFIRST = 0x0200 CF_PRIVATELAST = 0x02FF CF_GDIOBJFIRST = 0x0300 CF_GDIOBJLAST = 0x03FF GMEM_MOVEABLE = 0x0002 @@openClipboard = Win32API.new('user32', 'OpenClipboard', ['L'], 'I') @@closeClipboard = Win32API.new('user32', 'CloseClipboard', [], 'I') @@getClipboardData = Win32API.new('user32', 'GetClipboardData', ['I'], 'P') @@setClipboardData = Win32API.new('user32', 'SetClipboardData', ['I', 'I'], 'I') @@isClipboardFormatAvailable = Win32API.new('user32', 'IsClipboardFormatAvailable', ['I'], 'I') @@emptyClipboard = Win32API.new('user32', 'EmptyClipboard', [], 'I') @@globalAlloc = Win32API.new('kernel32', 'GlobalAlloc', ['I', 'I'], 'I') @@globalLock = Win32API.new('kernel32', 'GlobalLock', ['I'], 'I') @@globalUnlock = Win32API.new('kernel32', 'GlobalUnlock', ['I'], 'I') @@memcpy = Win32API.new('msvcrt', 'memcpy', ['I', 'P', 'I'], 'I') def open raise RuntimeError('Can not open clipboard') unless @@openClipboard.Call(0) != 0 end def close @@closeClipboard.Call() end def data(format = CF_TEXT) @@getClipboardData.Call(format) end def hasFormat?(format) @@isClipboardFormatAvailable.Call(format) != 0 end def data=(data) setData(data) data end def setData(data, type = CF_TEXT) # Empty the clipboard @@emptyClipboard.Call # Null terminate data << 0 if type == CF_TEXT # Global Allocate a movable piece of memory. hmem = @@globalAlloc.Call(GMEM_MOVEABLE, data.length) mem = @@globalLock.Call(hmem) @@memcpy.Call(mem, data, data.length) @@globalUnlock.Call(hmem) # Set the new data @@setClipboardData.Call(type, hmem) != 0 end end if $0 == __FILE__ clip = Clipboard.new clip.open if clip.hasFormat?(Clipboard::CF_TEXT) p clip.data clip.data = "hello" end clip.close end