最新ToolTip类
最近发现原来的那个ToolTip类使用起来还是不方便,故我又重新整理了一个更方便更灵活的ToolTip类.
新ToolTip类
复制内容到剪贴板代码:
package project.do93.pflip.view
{
import flash.accessibility.AccessibilityProperties;
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.text.*;
/**
* @link kinglong@gmail.com
* @author Kinglong
* @version 0.1
* @since 20090608
* @playerversion fp9+
* 热区提示
*/
public class ToolTip extends Sprite {
private static var instance:ToolTip = null;
private var label:TextField;
private var area:DisplayObject;
public function ToolTip() {
label = new TextField();
label.autoSize = TextFieldAutoSize.LEFT;
label.selectable = false;
label.multiline = false;
label.wordWrap = false;
label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
label.text = "提示信息";
label.x = 5;
label.y = 2;
addChild(label);
redraw();
visible = false;
mouseEnabled = mouseChildren = false;
}
private function redraw() {
var w:Number = 10 + label.width;
var h:Number = 4 + label.height;
this.graphics.clear();
this.graphics.beginFill(0x000000, 0.4);
this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
this.graphics.moveTo(6, 3 + h);
this.graphics.lineTo(12, 3 + h);
this.graphics.lineTo(9, 8 + h);
this.graphics.lineTo(6, 3 + h);
this.graphics.endFill();
this.graphics.beginFill(0xffffff);
this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
this.graphics.moveTo(3, h);
this.graphics.lineTo(9, h);
this.graphics.lineTo(6, 5 + h);
this.graphics.lineTo(3, h);
this.graphics.endFill();
}
public static function init(base:DisplayObjectContainer) {
if (instance == null) {
instance = new ToolTip();
base.addChild(instance);
}
}
public static function register(area:DisplayObject, message:String):void {
if(instance != null){
var prop:AccessibilityProperties = new AccessibilityProperties();
prop.description = message;
area.accessibilityProperties = prop;
area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
}
}
public static function unregister(area:DisplayObject):void {
if (instance != null) {
area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
}
}
public function show(area:DisplayObject):void {
this.area = area;
this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
label.text = area.accessibilityProperties.description;
redraw();
}
public function hide():void {
this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
this.area = null;
visible = false;
}
public function move(point:Point):void {
var lp:Point = this.parent.globalToLocal(point);
this.x = lp.x - 6;
this.y = lp.y - label.height - 12;
if(!visible){
visible = true;
}
}
private function handler(event:MouseEvent):void {
switch(event.type) {
case MouseEvent.MOUSE_OUT:
this.hide();
break;
case MouseEvent.MOUSE_MOVE:
this.move(new Point(event.stageX, event.stageY));
break;
case MouseEvent.MOUSE_OVER:
this.show(event.target as DisplayObject);
this.move(new Point(event.stageX, event.stageY))
break;
}
}
}
}
调用实例
复制内容到剪贴板代码:
import project.do93.pflip.view.ToolTip;
var base:Sprite = new Sprite();
addChild(base);
var sp:Sprite = new Sprite();
sp.mouseEnabled=sp.mouseChildren=false;
addChild(sp);
//初始化ToolTip;
ToolTip.init(sp);
//与显示元素关联。
ToolTip.register(xxx_mc, "http://www.klstudio.com");
ToolTip.register(btn1, "我是按钮1");
ToolTip.register(btn2, "我是按钮2");
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |