2004/09/12 | [教程]ResizableWindow源码
类别(我闪推荐) | 评论(0) | 阅读(192) | 发表于 08:27

摘自:AOL Blog

//****************************************************************************
// 版权及最终解释权归AOL所有
// 大小可以由鼠标拖动的Window组件
// 由mx.containers.Window类继承
//****************************************************************************

import mx.containers.Window;
import mx.controls.SimpleButton;
import mx.events.EventDispatcher;


[IconFile("ResizableWindowIcon.png")]

class ResizableWindow extends Window
{
        static var symbolName:String = "ResizableWindow";
        static var symbolOwner:Object = Object(ResizableWindow);
        static var version:String = "0.01";
       
        // private var _maxwidth:Number;
        // private var _maxheight:Number;
        // private var _minwidth:Number;
        // private var _minheight:Number;
        // private var _rangewidth:Number;
        // private var _rangeheight:Number;
       
       
        /* 当前类参数
        * 及当前类+父类参数组合
        */

        var clipParameters:Object = { //maxWidth:1, maxHeight:1, minWidth:1, minHeight:1
        };
        static var mergedClipParameters:Boolean = UIObject.mergeClipParameters(
        mx.containers.Window.prototype.clipParameters,
                                                                               ResizableWindow.prototype.clipParameters);
       
       
        /*
        * 拖动按钮
        */

        private var handle:SimpleButton;
       
        /*
        * 鼠标原位置
        */

        private var _oldx:Number;
        private var _oldy:Number;
       
        /*
        * 可控大小窗口按钮的皮肤
        */

        [Inspectable(verbose=1, category="Skins")]
        var skinResize:String = "ResizeButton";
       
        // /*
        //  * 可控大小窗口的最大宽度
        //  */
        // [Inspectable (name="ResizableWindow Maximal Width" defaultValue=550 type="Number", category="Values")]
        // public function set maxWidth(val:Number):Void
        // {
        // _maxwidth = val;
        // _rangewidth = _maxwidth - _minwidth;
        // }
       
        // public function get maxWidth():Number
        // {
        // return _maxwidth;
        // }
       
        // /*
        //  * 可控大小窗口的最小宽度
        //  */
        // [Inspectable (name="ResizableWindow Minimum Width" defaultValue=20 type="Number", category="Values")]
        // public function set minWidth(val:Number):Void
        // {
        // _minwidth = val;
        // _rangewidth = _maxwidth - _minwidth;
        // }
       
        // public function get minWidth():Number
        // {
        // return _minwidth;
        // }
       
        // /*
        //  * 可控大小窗口的最大高度
        //  */
        // [Inspectable (name="ResizableWindow Maximal Height" defaultValue=400 type="Number", category="Values")]
        // public function set maxHeight(val:Number):Void
        // {
        // _maxheight= val;
        // _rangeheight= _maxheight - _minheight;
        // }
       
        // public function get maxHeight():Number
        // {
        // return _maxheight;
        // }
       
        // /*
        //  * 可控大小窗口的最小高度
        //  */
        // [Inspectable (name="ResizableWindow Minimum Height" defaultValue=40 type="Number", category="Values")]
        // public function set minHeight(val:Number):Void
        // {
        // _minheight = val;
        // _rangeheight = _maxheight - _minheight;
        // }
       
        // public function get minHeight():Number
        // {
        // return _minheight;
        // }
       
        /*
        * 构造函数
        */

        function MyComponent()
        {
               
        }
       
        /* 方法
        * 初始化组件
        */

        function init(Void):Void
        {
                super.init();
        }
       
        /* 方法
        * 改变组件大小
        */

        function size(Void):Void
        {
                super.size();
                // handle.setSize();
        }
       
        /*
        * 建立组件的子项
        * ResizableWindow组件有一个子项要建立
        * 那就是使得Window可以变化的一个按钮
        */

        private function createChildren(Void):Void
        {
                super.createChildren();
               
                var resizeObj:Object = new Object();
                resizeObj.falseUpSkin = skinResize;
                resizeObj.falseOverSkin = skinResize;
                resizeObj.falseDownSkin = skinResize;
                handle = createClassObject(SimpleButton, "handle", 100,resizeObj);
                handle.tabEnabled = false;
                handle.onPress = startResize;
                handle.onRelease = stopResize;
                handle.onReleaseOutside = stopResize;
        }
       
        /*
        * 布局
        * 布置可控窗口大小按钮的位置
        */

        private function doLayout(Void):Void
        {
                super.doLayout();
                handle.move(width-handle.width, height-handle.height);
        }
       
        /*
        * 事件处理方法
        * 当按钮被按下,窗口的大小开始随着变化
        */

        private function startResize(Void):Void
        {
                // trace("start");
                this.useHandCursor=true;
                this._parent._oldx=this._parent._xmouse;
                this._parent._oldy=this._parent._ymouse;
                this._parent.onMouseMove = this._parent.resize;
        }
       
        /*
        * 事件处理方法
        * 当按钮被拖动,窗口的大小变化
        */

        private function resize(Void):Void
        {
                // trace("resize");
                var dx = _xmouse - _oldx;
                var dy = _ymouse - _oldy;
                if((width>minWidth && height>minHeight ) || (dx>0 && dy>0))
                setSize(width +dx, height +dy);
                _oldx=_xmouse;
                _oldy=_ymouse;
        }
       
        /*
        * 事件处理方法
        * 当按钮被释放,窗口的大小变化停止
        */

        private function stopResize(Void):Void
        {
                this.useHandCursor=false;
                delete this._parent.onMouseMove;
                this._parent.releaseFocus();
        }
       
}

0

评论Comments