ToolTip类的升级版

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

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

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

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


调用实例

  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值