中国开发网: 论坛: 程序员情感CBD: 贴子 241520
JoJo
看看这样贴代码行不行
package com.tksoft.control
{

import mx.core.UIComponent;
import mx.managers.CursorManager;
import mx.managers.CursorManagerPriority;
import flash.display.*;
import flash.events.*;

public class ComponentControl extends UIComponent
{
/******************************************************************************
Public Properties
******************************************************************************/
[Bindable]public var bdColor: uint = 0;
[Bindable]public var fcColor: uint = 0xFFFFFF;
[Bindable]public var fcAlpha: Number = 0.5;
[Bindable]public var cbSize: uint = 6;

/******************************************************************************
Private Fields
******************************************************************************/
private var ctrlBoxs: Array;

public static var CB_NW: int = 0;
public static var CB_N: int = 1;
public static var CB_NE: int = 2;
public static var CB_W: int = 3;
public static var CB_E: int = 4;
public static var CB_SW: int = 5;
public static var CB_S: int = 6;
public static var CB_SE: int = 7;

/******************************************************************************
Override Functions
******************************************************************************/
override public function updateDisplayList(param0: Number, param1:
Number): Void
{
super.updateDisplayList(param0, param1);
drawBorder();
layoutCtrlBoxs();
syncControl();
}

override public function initialize(): Void
{
super.initialize();
ctrlBoxs = new Array();
for (var i: int = 0; i <= CB_SE; i++)
{
var cb: CtrlBox = new CtrlBox();
cb.bdColor = this.bdColor;
cb.fcColor = this.fcColor;
cb.fcAlpha = this.fcAlpha;
cb.owner = this;
cb.cbid = i;
cb.visible = false;
ctrlBoxs.push(cb);
this.addChild(cb);
}
this.addEventListener(MouseEventType.MOUSE_DOWN, onMouseDownHandle);
this.addEventListener(MouseEventType.MOUSE_OVER, onMouseOverHandle);

this.initCtrlBoxs();
}

/******************************************************************************
Initialize Functions
******************************************************************************/
private function initCtrlBoxs(): Void
{
for (var i: int = 0; i < ctrlBoxs.length; i++)
{
var cb: CtrlBox = ctrlBoxs[i];
switch (cb.cbid)
{
case CB_NW:
case CB_SE:
cb.cursor = this.cursorNWSE;
cb.cursor_x = this.cursorNWSE_x;
cb.cursor_y = this.cursorNWSE_y;
break;
case CB_N:
case CB_S:
cb.cursor = this.cursorNS;
cb.cursor_x = this.cursorNS_x;
cb.cursor_y = this.cursorNS_y;
break;
case CB_NE:
case CB_SW:
cb.cursor = this.cursorNESW;
cb.cursor_x = this.cursorNESW_x;
cb.cursor_y = this.cursorNESW_y;
break;
case CB_W:
case CB_E:
cb.cursor = this.cursorWE;
cb.cursor_x = this.cursorWE_x;
cb.cursor_y = this.cursorWE_y;
break;
}
}
}

/******************************************************************************
Graphics Functions
******************************************************************************/
private function drawBorder(): Void
{
var g: Graphics = this.graphics;
g.clear();
g.lineStyle(0, bdColor);
g.beginFill(fcColor, 0.1);
g.drawRect(0, 0, width, height);
g.endFill();
g.lineStyle(0, fcColor, fcAlpha);
g.drawRect( - 1, - 1, width + 2, height + 2);
if (width > 2 && height > 2)
g.drawRect(1, 1, width - 2, height - 2);
}

private function layoutCtrlBoxs(): Void
{
for (var i: int = 0; i < ctrlBoxs.length; i++)
{
this.layoutCtrlBox(ctrlBoxs[i]);
}
}

private function layoutCtrlBox(cb: CtrlBox): Void
{
if (needShowCtrlBox(cb))
{
cb.width = this.cbSize;
cb.height = this.cbSize;
cb.visible = true;
var lx: Number = - cbSize / 2;
var cx: Number = (width - cbSize) / 2;
var rx: Number = width - cbSize / 2;
var ty: Number = - cbSize / 2;
var cy: Number = (height - cbSize) / 2;
var by: Number = height - cbSize / 2;
switch (cb.cbid)
{
case CB_NW:
cb.x = lx;
cb.y = ty;
break;
case CB_N:
cb.x = cx;
cb.y = ty;
break;
case CB_NE:
cb.x = rx;
cb.y = ty;
break;
case CB_W:
cb.x = lx;
cb.y = cy;
break;
case CB_E:
cb.x = rx;
cb.y = cy;
break;
case CB_SW:
cb.x = lx;
cb.y = by;
break;
case CB_S:
cb.x = cx;
cb.y = by;
break;
case CB_SE:
cb.x = rx;
cb.y = by;
break;
}
}
else
{
cb.visible = false;
}
}

private function needShowCtrlBox(cb: CtrlBox): Boolean
{
/*
if (cb.cbid == CB_SE) return true;
if (this.width < this.cbSize){
switch(cb.cbid){
case CB_NW:
case CB_N:
case CB_SW:
case CB_S:
return false;
}
}
if (this.width < this.cbSize*2){
switch(cb.cbid){
case CB_N:
case CB_S:
return false;
}
}
if (this.height < this.cbSize){
switch(cb.cbid){
case CB_NW:
case CB_W:
case CB_E:
return false;
}
}
if (this.height < this.cbSize*2){
switch(cb.cbid){
case CB_W:
case CB_E:
return false;
}
}
*/
return true;
}
/******************************************************************************
Cursor Functions
******************************************************************************/
private var cursorID: Number = 0;
[Embed(source = "images\MOVE.png")]private var cursorMove: Class;

[Embed(source = "images\NESW.png")]private var cursorNESW: Class;
private var cursorNESW_x: Number = - 7;
private var cursorNESW_y: Number = - 7;
[Embed(source = "images\NS.png")]private var cursorNS: Class;
private var cursorNS_x: Number = - 4;
private var cursorNS_y: Number = - 10;
[Embed(source = "images\NWSE.png")]private var cursorNWSE: Class;
private var cursorNWSE_x: Number = - 7;
private var cursorNWSE_y: Number = - 7;
[Embed(source = "images\WE.png")]private var cursorWE: Class;
private var cursorWE_x: Number = - 10;
private var cursorWE_y: Number = - 4;

private function clearCursor(): Void
{
if (cursorID == 0)
return ;
CursorManager.removeCursor(cursorID);
cursorID = 0;
}

private function setMoveCursor(): Void
{
if (cursorID != 0)
return ;
cursorID = CursorManager.setCursor(cursorMove,
CursorManagerPriority.MEDIUM, - 10, - 10);
}

/******************************************************************************
Action Functions
******************************************************************************/
private var __sx: Number;
private var __sy: Number;
private var __sw: Number;
private var __sh: Number;
private var __ox: Number;
private var __oy: Number;
private function saveLocation(): Void
{
__sx = parent.mouseX;
__sy = parent.mouseY;
__ox = x;
__oy = y;
}
private function saveSize(): Void
{
__sw = width;
__sh = height;
}
private function startMove(): Void
{
this.saveLocation();
this.setCapture();
this.startDrag(false, parent.getBounds(this));
}

private function updateMove(): Void
{
var dx: Number = __ox + (parent.mouseX - __sx);
var dy: Number = __oy + (parent.mouseY - __sy);
this.move(dx, dy);
//this.setLocation(dx,dy);
//this.saveLocation();
this.syncControl();
}

private function stopMove(): Void
{
this.stopDrag();
this.releaseCapture();
this.updateMove();
this.syncControl();
}

private function setLocation(nx: Number, ny: Number): Void
{
x = nx;
y = ny;
}

private function setSize(nw: Number, nh: Number): Void
{
width = nw;
height = nh;
}

public function startResize(): Void
{
saveLocation();
saveSize();
}

public function resizeS(): Void
{
var nh: Number = __sh + parent.mouseY - __sy;
if (nh < 0)
nh = 0;
setSize(width, nh);
//startResize();
}

public function resizeE(): Void
{
var nw: Number = __sw + parent.mouseX - __sx;
if (nw < 0)
nw = 0;
setSize(nw, height);
//startResize();
}

public function resizeSE()
{
var nw: Number = __sw + parent.mouseX - __sx;
if (nw < 0)
nw = 0;
var nh: Number = __sh + parent.mouseY - __sy;
if (nh < 0)
nh = 0;
setSize(nw, nh);
//startResize();
}

public function resizeN()
{
var nh: Number = __sh - parent.mouseY + __sy;
if (nh < 0)
nh = 0;
var ny: Number = __oy + parent.mouseY - __sy;
if (ny > (y + nh))
ny = y + nh;
setSize(width, nh);
setLocation(x, ny);
//startResize();
}

public function resizeW()
{
var nw: Number = __sw - parent.mouseX + __sx;
if (nw < 0)
nw = 0;
var nx: Number = __ox + parent.mouseX - __sx;
if (nx > (x + nw))
nx = x + nw;
setSize(nw, height);
setLocation(nx, y);
//startResize();
}

public function resizeNW()
{
var nw: Number = __sw - parent.mouseX + __sx;
if (nw < 0)
nw = 0;
var nh: Number = __sh - parent.mouseY + __sy;
if (nh < 0)
nh = 0;
var nx: Number = __ox + parent.mouseX - __sx;
if (nx > (x + nw))
nx = x + nw;
var ny: Number = __oy + parent.mouseY - __sy;
if (ny > (y + nh))
ny = y + nh;
setSize(nw, nh);
setLocation(nx, ny);
//startResize();
}

public function resizeNE()
{
var nw: Number = __sw + parent.mouseX - __sx;
if (nw < 0)
nw = 0;
var nh: Number = __sh - parent.mouseY + __sy;
if (nh < 0)
nh = 0;
var ny: Number = __oy + parent.mouseY - __sy;
if (ny > (y + nh))
ny = y + nh;
setSize(nw, nh);
setLocation(x, ny);
//startResize();
}

public function resizeSW()
{
var nw: Number = __sw - parent.mouseX + __sx;
if (nw < 0)
nw = 0;
var nh: Number = __sh + parent.mouseY - __sy;
if (nh < 0)
nh = 0;
var nx: Number = __ox + parent.mouseX - __sx;
if (nx > (x + nw))
nx = x + nw;
setSize(nw, nh);
setLocation(nx, y);
//startResize();
}

/******************************************************************************
Event Handle
******************************************************************************/
private function onMouseDownHandle(e: MouseEvent): Void
{
if (!(e.target == this))
return ;
this.setMoveCursor();
this.startMove();
this.addEventListener(MouseEventType.MOUSE_UP, onMouseUpHandle);
this.addEventListener(MouseEventType.MOUSE_MOVE, onMouseMoveHandle);
}

private function onMouseMoveHandle(e: MouseEvent): Void
{
if (!(e.target == this))
return ;
if (!e.buttonDown)
{
this.onMouseUpHandle(e);
return ;
}
updateMove();
}

private function onMouseUpHandle(e: MouseEvent): Void
{
stopMove();
this.removeEventListener(MouseEventType.MOUSE_UP, onMouseUpHandle);
this.removeEventListener(MouseEventType.MOUSE_MOVE,
onMouseMoveHandle);
this.clearCursor();
}

private function onMouseOverHandle(e: MouseEvent): Void
{
if (!(e.target == this))
return ;
for (var i: int = 0; i < ctrlBoxs.length; i++)
{
var cb: CtrlBox = ctrlBoxs[i];
cb.clearEventBinding();
}
this.setCapture();
this.setMoveCursor();
this.addEventListener(MouseEventType.MOUSE_OUT, onMouseOutHandle);
}

private function onMouseOutHandle(e: MouseEvent): Void
{
this.releaseCapture();
this.clearCursor();
this.removeEventListener(MouseEventType.MOUSE_OUT, onMouseOutHandle)
;
}
/******************************************************************************
Control Property
******************************************************************************/
private var __control: DisplayObject;
[Bindable("controlChanged")]public function get control(): DisplayObject
{
return __control;
}
public function set control(value: DisplayObject): Void
{
__control = value;
bindControl();
}

public function bindControl(): Void
{
if (control == null)
return ;
if (control.parent != this.parent)
return ;
this.parent.setChildIndex(this, parent.numChildren - 1);
this.setLocation(control.x, control.y);
this.setSize(control.width, control.height);
}

public function syncControl(): Void
{
if (control == null)
return ;
if (control.parent != this.parent)
return ;
control.x = x;
control.y = y;
control.width = width;
control.height = height;
}
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录