---
title: "ToolTip类的升级版"
author: "hereson3"
date: "2010-01-07T12:05:09Z"
tags: []
categories: ["编程语言"]
slug: "tooltip"
draft: false
lastmod: "2010-01-07T12:05:09Z"
id: 564621
url: "https://kungsoft.iteye.com/blog/564621"
---

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

///////////////////////////------------------本文来自[http://www.klstudio.com/post/198.html![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html)  

这次做公司一个项目，发现原来的那个[ToolTip类![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/158.html) 使用起来还是不方便，故我又重新写一个更方便更灵活的ToolTip类，现在把相关源代码也公布出来。  
新ToolTip类

[view plain![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [copy to clipboard![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [print![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [?![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html)

001. package  project.do93.pflip.view
002. {
003. import  flash.accessibility.AccessibilityProperties;
004. import  flash.display.\*;
005. import  flash.events.\*;
006. import  flash.geom.Point;
007. import  flash.text.\*;
008. /\**
009. * @link kinglong@gmail.com
010. * @author Kinglong
011. * @version 0.1
012. * @since 20090608
013. * @playerversion fp9+
014. * 热区提示
015. \*/
016. public   class  ToolTip  extends  Sprite {
017. private   static  var instance:ToolTip =  null ;
018. private  var label:TextField;
019. private  var area:DisplayObject;
020. public  function ToolTip() {
021. label =  new  TextField();
022. label.autoSize = TextFieldAutoSize.LEFT;
023. label.selectable =  false ;
024. label.multiline =  false ;
025. label.wordWrap =  false ;
026. label.defaultTextFormat =  new  TextFormat( "宋体" ,  12 ,  0x666666 );
027. label.text =  "提示信息" ;
028. label.x =  5 ;
029. label.y =  2 ;
030. addChild(label);
031. redraw();
032. visible =  false ;
033. mouseEnabled = mouseChildren =  false ;
034. }
035. private  function redraw() {
036. var w:Number =  10  + label.width;
037. var h:Number =  4  + label.height;
038. this .graphics.clear();
039. this .graphics.beginFill( 0x000000 ,  0.4 );
040. this .graphics.drawRoundRect( 3 ,  3 , w, h,  5 ,  5 );
041. this .graphics.moveTo( 6 ,  3  + h);
042. this .graphics.lineTo( 12 ,  3  + h);
043. this .graphics.lineTo( 9 ,  8  + h);
044. this .graphics.lineTo( 6 ,  3  + h);
045. this .graphics.endFill();
046. this .graphics.beginFill( 0xffffff );
047. this .graphics.drawRoundRect( 0 ,  0 , w, h,  5 ,  5 );
048. this .graphics.moveTo( 3 , h);
049. this .graphics.lineTo( 9 , h);
050. this .graphics.lineTo( 6 ,  5  + h);
051. this .graphics.lineTo( 3 , h);
052. this .graphics.endFill();
053. }
054. public   static  function init(base:DisplayObjectContainer) {
055. if  (instance ==  null ) {
056. instance =  new  ToolTip();
057. base.addChild(instance);
058. }
059. }
060. public   static  function register(area:DisplayObject, message:String): void  {
061. if (instance !=  null ){
062. var prop:AccessibilityProperties =  new  AccessibilityProperties();
063. prop.description = message;
064. area.accessibilityProperties = prop;
065. area.addEventListener(MouseEvent.MOUSE\_OVER, instance.handler);
066. }
067. }
068. public   static  function unregister(area:DisplayObject): void  {
069. if  (instance !=  null ) {
070. area.removeEventListener(MouseEvent.MOUSE\_OVER, instance.handler);
071. }
072. }
073. public  function show(area:DisplayObject): void  {
074. this .area = area;
075. this .area.addEventListener(MouseEvent.MOUSE\_OUT,  this .handler);
076. this .area.addEventListener(MouseEvent.MOUSE\_MOVE,  this .handler);
077. label.text = area.accessibilityProperties.description;
078. redraw();
079. }
080. public  function hide(): void  {
081. this .area.removeEventListener(MouseEvent.MOUSE\_OUT,  this .handler);
082. this .area.removeEventListener(MouseEvent.MOUSE\_MOVE,  this .handler);
083. this .area =  null ;
084. visible =  false ;
085. }
086. public  function move(point:Point): void  {
087. var lp:Point =  this .parent.globalToLocal(point);
088. this .x = lp.x -  6 ;
089. this .y = lp.y - label.height -  12 ;
090. if (!visible){
091. visible =  true ;
092. }
093. }
094. private  function handler(event:MouseEvent): void  {
095. switch (event.type) {
096. case  MouseEvent.MOUSE\_OUT:
097. this .hide();
098. break ;
099. 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![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [copy to clipboard![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [print![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html) [?![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/post/198.html)

01. import  project.do93.pflip.view.ToolTip;
02. var base:Sprite =  new  Sprite();
03. addChild(base);
04. var sp:Sprite =  new  Sprite();
05. sp.mouseEnabled=sp.mouseChildren= false ;
06. addChild(sp);
07. //初始化ToolTip;
08. ToolTip.init(sp);
09. //与显示元素关联。
10. ToolTip.register(xxx\_mc,  "http://www.klstudio.com" );
11. ToolTip.register(btn1,  "我是按钮1" );
12. ToolTip.register(btn2,  "我是按钮2" );

演示效果  
&lt;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"&gt; &lt;/object&gt;  
相关文件打包下载：[http://www.klstudio.com/upload/tooltip.rar![](http://i.ixnp.com/images/v6.18/t.gif)](http://www.klstudio.com/upload/tooltip.rar)