在D:\ruby\lib\ruby\gems\1.8\gems\win32-clipboard-0.4.3\examples\ 找了这个访问剪贴板的示例代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
##########################################################################
# clipboard_test.rb (win32-clipboard)
#
# Generic test script for those without TestUnit installed, or for
# general futzing.
##########################################################################
Dir.chdir('..') if File.basename(Dir.pwd) == 'examples'
$LOAD_PATH.unshift(Dir.pwd)
$LOAD_PATH.unshift(Dir.pwd + '/lib')
require "win32/clipboard"
require "pp"
include Win32
puts "VERSION: " + Clipboard::VERSION
pp Clipboard.formats
pp Clipboard.data(Clipboard::UNICODETEXT)
pp Clipboard.format_available?(49161)
pp Clipboard.format_name(999999999)
pp Clipboard.format_available?(9999999)
puts "Data was: [" + Clipboard.data + "]"
Clipboard.set_data("foobar")
puts "Data is now: [" + Clipboard.data + "]"
puts "Number of available formats: " + Clipboard.num_formats.to_s
Clipboard.empty
puts "Clipboard emptied"
但是在执行它的时候会报错,说是在 clipboard.rb 里面找不到“ Win32API.new”。于是把
D:\ruby\lib\ruby\gems\1.8\gems\win32-clipboard-0.4.3\lib\win32\
以及 D:\ruby\lib\ruby\site_ruby\1.8\win32\ 的 clipboard.rb 文件里都加上一句:
require 'Win32API'
就好用了。