Flex で何かと使える TitleWindow をマウスで普通のウインドウみたいにリサイズを可能にしてみた。
そもそもなぜこのコントロールはこんなに中途半端なんだろう。クローズボタンはあるのに。
基本 MouseEvent を拾ってるだけ。
その時のソース。
<?xml version="1.0" encoding="utf-8"?> <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="Resizable Draggable Window" showCloseButton="true" mouseDown="onThis_mouseDown(event);" mouseMove="onThis_mouseMove(event);" mouseUp="onThis_mouseUp(event);" close="onThis_close();" creationComplete="onThis_creationComplete();" width="400" height="300" > <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.core.Application; import mx.managers.PopUpManager; private const SIZE_DRAGAREA:int = 5; private const SIZE_MIN_WIDTH:int = 40; private const SIZE_MIN_HEIGHT:int = 30; private var blDragTop:Boolean = false; private var blDragRight:Boolean = false; private var blDragBottom:Boolean = false; private var blDragLeft:Boolean = false; private var iDragPosHeight:int = 0; private var iDragPosWidth:int = 0; private function onThis_creationComplete():void { // set EventListener on parent Application for mouseUp and mouseMove Application.application.parent.addEventListener(MouseEvent.MOUSE_UP, onParent_mouseUp); Application.application.parent.addEventListener(MouseEvent.MOUSE_MOVE, onParent_mouseMove); } private function onThis_mouseDown(event:MouseEvent):void { // check Top pos if( event.localY < SIZE_DRAGAREA ) { blDragTop = true; iDragPosHeight = event.localY; } // check Right pos if( this.width - SIZE_DRAGAREA < event.localX ) { blDragRight = true; iDragPosWidth = this.width - event.localX; } // check Bottom pos if( this.height - SIZE_DRAGAREA < event.localY ) { blDragBottom = true; iDragPosHeight = this.height - event.localY; } // check Left pos if( event.localX < SIZE_DRAGAREA ) { blDragLeft = true; iDragPosWidth = event.localX; } } private function onParent_mouseMove(event:MouseEvent):void { doDrag(event.stageX, event.stageY); } private function onThis_mouseMove(event:MouseEvent):void { doDrag(event.stageX, event.stageY); } private function onParent_mouseUp(event:MouseEvent):void { outDragMode(); } private function onThis_mouseUp(event:MouseEvent):void { outDragMode(); } private function onThis_close():void { PopUpManager.removePopUp(this); } private function doDrag(intX:int, intY:int):void { if( blDragTop ) { if( this.y + this.height - intY + iDragPosHeight > SIZE_MIN_HEIGHT ) { this.height = this.y + this.height - intY + iDragPosHeight; this.y = intY - iDragPosHeight; } else { this.y = this.y + this.height - SIZE_MIN_HEIGHT; this.height = SIZE_MIN_HEIGHT; } } if( blDragRight ) { if( intX - this.x + iDragPosWidth > SIZE_MIN_WIDTH ) { this.width = intX - this.x + iDragPosWidth; } else { this.width = SIZE_MIN_WIDTH; } } if( blDragBottom ) { if( intY - this.y + iDragPosHeight > SIZE_MIN_HEIGHT ) { this.height = intY - this.y + iDragPosHeight; } else { this.height = SIZE_MIN_HEIGHT; } } if( blDragLeft ) { if( this.x + this.width - intX + iDragPosWidth > SIZE_MIN_WIDTH ) { this.width = this.x + this.width - intX + iDragPosWidth; this.x = intX - iDragPosWidth; } else { this.x = this.x + this.width - SIZE_MIN_WIDTH; this.width = SIZE_MIN_WIDTH; } } } private function outDragMode():void { blDragTop = blDragRight = blDragBottom = blDragLeft = false; } ]]> </mx:Script> </mx:TitleWindow>