ToolTip类的升级版

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

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

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

view plaincopy to clipboardprint?

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

调用实例

view plaincopy to clipboardprint?

  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"); 

演示效果
相关文件打包下载:http://www.klstudio.com/upload/tooltip.rar

posted @ 2010-01-07 12:07  真功夫  阅读(222)  评论(0编辑  收藏  举报