基于安全考虑,Flash只允许将数据写入系统剪贴板中,因此,Flash之中只能执行System下的setClipboard方法.

基于Flash的:

例子如下:

使用AS将数据写入内存
  1. package?{ ??
  2. ????import?flash.display.Sprite; ??
  3. ????import?flash.system.System; ??
  4. ??
  5. ????public?class?SystemExample?extends?Sprite?{ ??
  6. ????????public?function?SystemExample()?{ ??
  7. ????????????System.setClipboard("写入内存的数据"); ??
  8. ????????} ??
  9. ????} ??
  10. }??

但是,在AIR中会自由多了,允许读取与写入,但也不是没妈管的孩子,什么事都能做.

在AIR中能读取与写入的有六种数据格式,都由ClipboardFormats来撑管,我们看看这六种数据格式
BITMAP_FORMAT:图像数据。
FILE_LIST_FORMAT:文件数组。
HTML_FORMAT :HTML 数据。
RICH_TEXT_FORMAT:RTF 格式数据。
TEXT_FORMAT:字符串数据。
URL_FORMAT:URL 字符串。

看个由HELP文档中挖出来的例子吧,作用是向剪贴板写入一个随机数:

AS3代码
  1. import?flash.desktop.ClipboardFormats; ??
  2. ? ??
  3. ?Clipboard.generalClipboard.setDataHandler(ClipboardFormats.TEXT_FORMAT,?randomNumberGenerator); ??
  4. ??
  5. ?public?function?randomNumberGenerator():String{ ??
  6. ?????return?Math.random().toString(); ??
  7. ?}??

跟据多种数据格式做分类处理,以下是写来获取剪贴板图像源的类文件,将图像源转化为图片.

忘了说明一点,这是用在Flex中的,所以toBitmap中返加UIComponent类型

AS3代码
  1. /** ?
  2. ?????*?... ?
  3. ?????*?@author?WZH(shch8.com) ?
  4. ?????*?获取系统剪贴板 ?
  5. ?????*/??
  6. package?com.flashNote.getData ??
  7. { ??
  8. ????????import?flash.desktop.Clipboard; ??
  9. ????????import?flash.desktop.ClipboardFormats; ??
  10. ????????import?flash.display.Bitmap; ??
  11. ????????import?flash.display.BitmapData; ??
  12. ???????? ??
  13. ????????import?mx.core.UIComponent; ??
  14. ????????public?class?GetBoardData ??
  15. ????????{ ??
  16. ????????private?static?var?Instance:GetBoardData=new?GetBoardData; ??
  17. ????????public?static?function?getInstance():GetBoardData{ ??
  18. ????????????return?Instance; ??
  19. ????????}? ??
  20. ????????public?function?GetBoardData() ??
  21. ????????{ ??
  22. ???????????? ??
  23. ????????} ??
  24. ????????public?function?getData():BoardFormat{ ??
  25. ????????????var?returnObj:BoardFormat; ??
  26. ????????????returnObj.sort=getSelFormat(); ??
  27. ????????????returnObj.data=Clipboard.generalClipboard.getData(returnObj.sort) ??
  28. ????????????var?bmd:BitmapData?=returnObj.data?as?BitmapData; ??
  29. ????????????toBitmap(bmd) ??
  30. ????????????return?returnObj; ??
  31. ????????} ??
  32. ????????private?function?toBitmap(bmd:BitmapData):UIComponent{ ??
  33. ????????????var?ui:UIComponent=new?UIComponent; ??
  34. ????????????var?bm:Bitmap?=?new?Bitmap(bmd); ??
  35. ????????????ui.addChild(bm); ??
  36. ????????????return?ui; ??
  37. ????????} ??
  38. ????????private?function?getSelFormat():String{ ??
  39. ????????????var?backStr:String; ??
  40. ????????????backStr=getFormat(ClipboardFormats.BITMAP_FORMAT) ??
  41. ????????????if(backStr!=""){ ??
  42. ????????????????return?backStr; ??
  43. ????????????} ??
  44. ????????????backStr=getFormat(ClipboardFormats.FILE_LIST_FORMAT) ??
  45. ????????????if(backStr!=""){ ??
  46. ????????????????return?backStr; ??
  47. ????????????} ??
  48. ????????????backStr=getFormat(ClipboardFormats.HTML_FORMAT) ??
  49. ????????????if(backStr!=""){ ??
  50. ????????????????return?backStr; ??
  51. ????????????} ??
  52. ????????????backStr=getFormat(ClipboardFormats.TEXT_FORMAT) ??
  53. ????????????if(backStr!=""){ ??
  54. ????????????????return?backStr; ??
  55. ????????????} ??
  56. ????????????backStr=getFormat(ClipboardFormats.URL_FORMAT) ??
  57. ????????????if(backStr!=""){ ??
  58. ????????????????return?backStr; ??
  59. ????????????} ??
  60. ????????????function?getFormat(str:String):String{ ??
  61. ????????????????if(Clipboard.generalClipboard.hasFormat(str)){ ??
  62. ????????????????????return?str ??
  63. ????????????????}else{ ??
  64. ????????????????????return?""; ??
  65. ????????????????} ??
  66. ????????????} ??
  67. ????????????return?null; ??
  68. ????????} ??
  69. ????} ??
  70. }??

?

AS3代码
  1. /** ?
  2. ?????*?... ?
  3. ?????*?@author?WZH(shch8.com) ?
  4. ?????*?传送参数 ?
  5. ?????*/??
  6. package?com.flashNote.getData ??
  7. { ??
  8. ????public?class?BoardFormat ??
  9. ????{ ??
  10. ????????public?var?sort:String; ??
  11. ????????public?var?data:Object; ??
  12. ????} ??
  13. }??