package
1. {
2. import mx.core.UIComponent;
3.
4. {
5.
6. import flash.accessibility.AccessibilityProperties;
7. import flash.display.*;
8. import flash.events.*;
9. import flash.geom.Point;
10. import flash.text.*;
11.
12. /**
13. * @link kinglong@gmail.com
14. * @author Kinglong
15. * @version 0.1
16. * @since 20090608
17. * @playerversion fp9+
18. * 热区提示
19. */
20. public class ToolTip extends UIComponent{
21. private static var instance:ToolTip = null;
22. private var label:TextField;
23. private var area:DisplayObject;
24. public function ToolTip() {
25.
26. label = new TextField();
27. label.autoSize = TextFieldAutoSize.LEFT;
28. label.selectable = false;
29. label.multiline = false;
30. label.wordWrap = false;
31. label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
32. label.text = "提示信息";
33. label.x = 5;
34. label.y = 2;
35. addChild(label);
36. redraw();
37. visible = false;
38. mouseEnabled = mouseChildren = false;
39. }
40.
41. private function redraw() {
42. var w:Number = 10 + label.width;
43. var h:Number = 4 + label.height;
44. this.graphics.clear();
45. this.graphics.beginFill(0x000000, 0.4);
46. this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
47. this.graphics.moveTo(6, 3 + h);
48. this.graphics.lineTo(12, 3 + h);
49. this.graphics.lineTo(9, 8 + h);
50. this.graphics.lineTo(6, 3 + h);
51. this.graphics.endFill();
52. this.graphics.beginFill(0xffffff);
53. this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
54. this.graphics.moveTo(3, h);
55. this.graphics.lineTo(9, h);
56. this.graphics.lineTo(6, 5 + h);
57. this.graphics.lineTo(3, h);
58. this.graphics.endFill();
59. }
60.
61. public static function init(base:DisplayObjectContainer) {
62. if (instance == null) {
63. instance = new ToolTip();
64. base.addChild(instance);
65. }
66. }
67.
68. public static function register(area:DisplayObject, message:String):void {
69. if(instance != null){
70. var prop:AccessibilityProperties = new AccessibilityProperties();
71. prop.description = message;
72. area.accessibilityProperties = prop;
73. area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
74. }
75. }
76.
77. public static function unregister(area:DisplayObject):void {
78. if (instance != null) {
79. area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
80. }
81. }
82.
83. public function show(area:DisplayObject):void {
84. this.area = area;
85. this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
86. this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
87. label.text = area.accessibilityProperties.description;
88. redraw();
89. }
90.
91.
92. public function hide():void {
93. this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
94. this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
95. this.area = null;
96. visible = false;
97. }
98.
99. public function moves(point:Point):void {
100. var lp:Point = this.parent.globalToLocal(point);
101. this.x = lp.x - 6;
102. this.y = lp.y - label.height - 12;
103. if(!visible){
104. visible = true;
105. }
106. }
107.
108. private function handler(event:MouseEvent):void {
109. switch(event.type) {
110. case MouseEvent.MOUSE_OUT:
111. this.hide();
112. break;
113. case MouseEvent.MOUSE_MOVE:
114. this.moves(new Point(event.stageX, event.stageY));
115. break;
116. case MouseEvent.MOUSE_OVER:
117. this.show(event.target as DisplayObject);
118. this.moves(new Point(event.stageX, event.stageY))
119. break;
120. }
121. }
122.
123. }
124.
125. }
126. }
package { import mx.core.UIComponent; { 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 UIComponent{ 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 moves(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.moves(new Point(event.stageX, event.stageY)); break; case MouseEvent.MOUSE_OVER: this.show(event.target as DisplayObject); this.moves(new Point(event.stageX, event.stageY)) break; } } } } }
测试代码:
view plaincopy to clipboardprint?
0. <?xml version="1.0" encoding="utf-8"?>
1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
2.
3. <mx:Script>
4. <!--[CDATA[
5.
6. private function init():void{
7.
8. ToolTip.init(this);
9. //与显示元素关联。
10. ToolTip.register(checkBox, "http://www.klstudio.com");
11. ToolTip.register(button, "我是按钮1");
12. ToolTip.register(img, "我是图片!");
13.
14.
15. }
16.
17.
18. ]]-->
19. </mx:Script>
20. <mx:Button id="button" x="211" y="117" label="Button"/>
21. <mx:CheckBox id="checkBox" x="156" y="289" label="Checkbox"/>
22.
23. <mx:TextArea id="img" x="183" y="498"/>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <!--[CDATA[ private function init():void{ ToolTip.init(this); //与显示元素关联。 ToolTip.register(checkBox, "http://www.klstudio.com"); ToolTip.register(button, "我是按钮1"); ToolTip.register(img, "我是图片!"); } ]]--> </mx:Script> <mx:Button id="button" x="211" y="117" label="Button"/> <mx:CheckBox id="checkBox" x="156" y="289" label="Checkbox"/> <mx:TextArea id="img" x="183" y="498"/>
我只是在原作者代码的基础上做了此许改动,修改后可以直接在flex中的mxml中使用
源地址:http://www.klstudio.com/post/198.html
1. {
2. import mx.core.UIComponent;
3.
4. {
5.
6. import flash.accessibility.AccessibilityProperties;
7. import flash.display.*;
8. import flash.events.*;
9. import flash.geom.Point;
10. import flash.text.*;
11.
12. /**
13. * @link kinglong@gmail.com
14. * @author Kinglong
15. * @version 0.1
16. * @since 20090608
17. * @playerversion fp9+
18. * 热区提示
19. */
20. public class ToolTip extends UIComponent{
21. private static var instance:ToolTip = null;
22. private var label:TextField;
23. private var area:DisplayObject;
24. public function ToolTip() {
25.
26. label = new TextField();
27. label.autoSize = TextFieldAutoSize.LEFT;
28. label.selectable = false;
29. label.multiline = false;
30. label.wordWrap = false;
31. label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
32. label.text = "提示信息";
33. label.x = 5;
34. label.y = 2;
35. addChild(label);
36. redraw();
37. visible = false;
38. mouseEnabled = mouseChildren = false;
39. }
40.
41. private function redraw() {
42. var w:Number = 10 + label.width;
43. var h:Number = 4 + label.height;
44. this.graphics.clear();
45. this.graphics.beginFill(0x000000, 0.4);
46. this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
47. this.graphics.moveTo(6, 3 + h);
48. this.graphics.lineTo(12, 3 + h);
49. this.graphics.lineTo(9, 8 + h);
50. this.graphics.lineTo(6, 3 + h);
51. this.graphics.endFill();
52. this.graphics.beginFill(0xffffff);
53. this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
54. this.graphics.moveTo(3, h);
55. this.graphics.lineTo(9, h);
56. this.graphics.lineTo(6, 5 + h);
57. this.graphics.lineTo(3, h);
58. this.graphics.endFill();
59. }
60.
61. public static function init(base:DisplayObjectContainer) {
62. if (instance == null) {
63. instance = new ToolTip();
64. base.addChild(instance);
65. }
66. }
67.
68. public static function register(area:DisplayObject, message:String):void {
69. if(instance != null){
70. var prop:AccessibilityProperties = new AccessibilityProperties();
71. prop.description = message;
72. area.accessibilityProperties = prop;
73. area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
74. }
75. }
76.
77. public static function unregister(area:DisplayObject):void {
78. if (instance != null) {
79. area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
80. }
81. }
82.
83. public function show(area:DisplayObject):void {
84. this.area = area;
85. this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
86. this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
87. label.text = area.accessibilityProperties.description;
88. redraw();
89. }
90.
91.
92. public function hide():void {
93. this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
94. this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
95. this.area = null;
96. visible = false;
97. }
98.
99. public function moves(point:Point):void {
100. var lp:Point = this.parent.globalToLocal(point);
101. this.x = lp.x - 6;
102. this.y = lp.y - label.height - 12;
103. if(!visible){
104. visible = true;
105. }
106. }
107.
108. private function handler(event:MouseEvent):void {
109. switch(event.type) {
110. case MouseEvent.MOUSE_OUT:
111. this.hide();
112. break;
113. case MouseEvent.MOUSE_MOVE:
114. this.moves(new Point(event.stageX, event.stageY));
115. break;
116. case MouseEvent.MOUSE_OVER:
117. this.show(event.target as DisplayObject);
118. this.moves(new Point(event.stageX, event.stageY))
119. break;
120. }
121. }
122.
123. }
124.
125. }
126. }
package { import mx.core.UIComponent; { 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 UIComponent{ 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 moves(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.moves(new Point(event.stageX, event.stageY)); break; case MouseEvent.MOUSE_OVER: this.show(event.target as DisplayObject); this.moves(new Point(event.stageX, event.stageY)) break; } } } } }
测试代码:
view plaincopy to clipboardprint?
0. <?xml version="1.0" encoding="utf-8"?>
1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
2.
3. <mx:Script>
4. <!--[CDATA[
5.
6. private function init():void{
7.
8. ToolTip.init(this);
9. //与显示元素关联。
10. ToolTip.register(checkBox, "http://www.klstudio.com");
11. ToolTip.register(button, "我是按钮1");
12. ToolTip.register(img, "我是图片!");
13.
14.
15. }
16.
17.
18. ]]-->
19. </mx:Script>
20. <mx:Button id="button" x="211" y="117" label="Button"/>
21. <mx:CheckBox id="checkBox" x="156" y="289" label="Checkbox"/>
22.
23. <mx:TextArea id="img" x="183" y="498"/>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <!--[CDATA[ private function init():void{ ToolTip.init(this); //与显示元素关联。 ToolTip.register(checkBox, "http://www.klstudio.com"); ToolTip.register(button, "我是按钮1"); ToolTip.register(img, "我是图片!"); } ]]--> </mx:Script> <mx:Button id="button" x="211" y="117" label="Button"/> <mx:CheckBox id="checkBox" x="156" y="289" label="Checkbox"/> <mx:TextArea id="img" x="183" y="498"/>
我只是在原作者代码的基础上做了此许改动,修改后可以直接在flex中的mxml中使用
源地址:http://www.klstudio.com/post/198.html