ITeye博客

ToolTip类的升级版

hereson3 · 2010-01-07 12:05 · ActionScript · 编程语言 · Markdown

些ToolTip类确实比之前的方便多了,收藏

///////////////////////////------------------本文来自http://www.klstudio.com/post/198.html  

这次做公司一个项目,发现原来的那个ToolTip类 使用起来还是不方便,故我又重新写一个更方便更灵活的ToolTip类,现在把相关源代码也公布出来。
新ToolTip类

view plain copy to clipboard print ?

  1. package  project.do93.pflip.view
  2. {
  3. import  flash.accessibility.AccessibilityProperties;
  4. import  flash.display.*;
  5. import  flash.events.*;
  6. import  flash.geom.Point;
  7. import  flash.text.*;
  8. /**
  9. * @link kinglong@gmail.com
  10. * @author Kinglong
  11. * @version 0.1
  12. * @since 20090608
  13. * @playerversion fp9+
  14. * 热区提示
  15. */
  16. public   class  ToolTip  extends  Sprite {
  17. private   static  var instance:ToolTip =  null ;
  18. private  var label:TextField;
  19. private  var area:DisplayObject;
  20. public  function ToolTip() {
  21. label =  new  TextField();
  22. label.autoSize = TextFieldAutoSize.LEFT;
  23. label.selectable =  false ;
  24. label.multiline =  false ;
  25. label.wordWrap =  false ;
  26. label.defaultTextFormat =  new  TextFormat( "宋体" ,  12 ,  0x666666 );
  27. label.text =  "提示信息" ;
  28. label.x =  5 ;
  29. label.y =  2 ;
  30. addChild(label);
  31. redraw();
  32. visible =  false ;
  33. mouseEnabled = mouseChildren =  false ;
  34. }
  35. private  function redraw() {
  36. var w:Number =  10  + label.width;
  37. var h:Number =  4  + label.height;
  38. this .graphics.clear();
  39. this .graphics.beginFill( 0x000000 ,  0.4 );
  40. this .graphics.drawRoundRect( 3 ,  3 , w, h,  5 ,  5 );
  41. this .graphics.moveTo( 6 ,  3  + h);
  42. this .graphics.lineTo( 12 ,  3  + h);
  43. this .graphics.lineTo( 9 ,  8  + h);
  44. this .graphics.lineTo( 6 ,  3  + h);
  45. this .graphics.endFill();
  46. this .graphics.beginFill( 0xffffff );
  47. this .graphics.drawRoundRect( 0 ,  0 , w, h,  5 ,  5 );
  48. this .graphics.moveTo( 3 , h);
  49. this .graphics.lineTo( 9 , h);
  50. this .graphics.lineTo( 6 ,  5  + h);
  51. this .graphics.lineTo( 3 , h);
  52. this .graphics.endFill();
  53. }
  54. public   static  function init(base:DisplayObjectContainer) {
  55. if  (instance ==  null ) {
  56. instance =  new  ToolTip();
  57. base.addChild(instance);
  58. }
  59. }
  60. public   static  function register(area:DisplayObject, message:String): void  {
  61. if (instance !=  null ){
  62. var prop:AccessibilityProperties =  new  AccessibilityProperties();
  63. prop.description = message;
  64. area.accessibilityProperties = prop;
  65. area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
  66. }
  67. }
  68. public   static  function unregister(area:DisplayObject): void  {
  69. if  (instance !=  null ) {
  70. area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
  71. }
  72. }
  73. public  function show(area:DisplayObject): void  {
  74. this .area = area;
  75. this .area.addEventListener(MouseEvent.MOUSE_OUT,  this .handler);
  76. this .area.addEventListener(MouseEvent.MOUSE_MOVE,  this .handler);
  77. label.text = area.accessibilityProperties.description;
  78. redraw();
  79. }
  80. public  function hide(): void  {
  81. this .area.removeEventListener(MouseEvent.MOUSE_OUT,  this .handler);
  82. this .area.removeEventListener(MouseEvent.MOUSE_MOVE,  this .handler);
  83. this .area =  null ;
  84. visible =  false ;
  85. }
  86. public  function move(point:Point): void  {
  87. var lp:Point =  this .parent.globalToLocal(point);
  88. this .x = lp.x -  6 ;
  89. this .y = lp.y - label.height -  12 ;
  90. if (!visible){
  91. visible =  true ;
  92. }
  93. }
  94. private  function handler(event:MouseEvent): void  {
  95. switch (event.type) {
  96. case  MouseEvent.MOUSE_OUT:
  97. this .hide();
  98. break ;
  99. case  MouseEvent.MOUSE_MOVE:
  100. this .move( new  Point(event.stageX, event.stageY));
  101. break ;
  102. case  MouseEvent.MOUSE_OVER:
  103. this .show(event.target as DisplayObject);
  104. this .move( new  Point(event.stageX, event.stageY))
  105. break ;
  106. }
  107. }
  108. }
  109. }

调用实例

view plain copy to clipboard print ?

  1. import  project.do93.pflip.view.ToolTip;
  2. var base:Sprite =  new  Sprite();
  3. addChild(base);
  4. var sp:Sprite =  new  Sprite();
  5. sp.mouseEnabled=sp.mouseChildren= false ;
  6. addChild(sp);
  7. //初始化ToolTip;
  8. ToolTip.init(sp);
  9. //与显示元素关联。
  10. ToolTip.register(xxx_mc,  "http://www.klstudio.com" );
  11. ToolTip.register(btn1,  "我是按钮1" );
  12. ToolTip.register(btn2,  "我是按钮2" );

演示效果
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"> </object>
相关文件打包下载:http://www.klstudio.com/upload/tooltip.rar

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。