//舞台属性设置
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
var
selectBox:Sprite =
new
Sprite();
//生成选择框
var
recordX:Number;
//记录初始x坐标
var
recordY:Number;
//记录初始y坐标
var
endX:Number;
//记录结束x坐标
var
endY:Number;
//记录结束y坐标
var
moveX:Number;
//记录移动之后x坐标
var
moveY:Number;
//记录移动之后y坐标
var
myImage:Bitmap;
//显示图片的容器
var
dragFlag:Boolean=
false
;
//用于判断选择框是否正在拖动
var
clickPoint:Point;
//记录开始的选择框对应的舞台坐标点
var
endPoint:Point;
//记录选择框绘制结束后选择框对应的舞台坐标点
initStage();
function
initStage() {
myImage=
new
Bitmap();
addChild(myImage);
bg.addEventListener(MouseEvent.MOUSE_DOWN,startDraw);
//绘制选择框
selectBox.addEventListener(MouseEvent.MOUSE_DOWN,dragSelectBox);
//拖动选择框
selectBox.addEventListener(MouseEvent.MOUSE_UP,stopDragSelectBox);
//停止拖动选择框
}
function
dragSelectBox(_evt:MouseEvent):
void
{
_evt.currentTarget.startDrag();
dragFlag=
true
;
}
function
stopDragSelectBox(_evt:MouseEvent) {
if
(dragFlag==
true
) {
createJPG(mc_image,selectBox,Math.min(selectBox.localToGlobal(clickPoint).x,selectBox.localToGlobal(endPoint).x),Math.min(selectBox.localToGlobal(clickPoint).y,selectBox.localToGlobal(endPoint).y));
selectBox.stopDrag();
dragFlag =
false
;
}
}
//开始绘制
function
startDraw(_evt:MouseEvent):
void
{
stage.addEventListener(Event.ENTER_FRAME,drawSelectBox);
recordX = mouseX;
recordY = mouseY;
clickPoint =
new
Point(recordX,recordY);
}
//绘制选择框
function
drawSelectBox(_evt:Event):
void
{
selectBox.x=selectBox.y=0;
//恢复坐标到原点
selectBox.graphics.clear();
selectBox.graphics.lineStyle(1,0x000000);
selectBox.graphics.beginFill(0xffffff,0.7);
selectBox.graphics.drawRect(recordX,recordY,mouseX-recordX,mouseY-recordY);
selectBox.graphics.endFill();
addChild(selectBox);
stage.addEventListener(MouseEvent.MOUSE_UP,endDraw);
// 鼠标在舞台上放开时的事件
}
//绘制结束
function
endDraw(_evt:MouseEvent):
void
{
stage.removeEventListener(Event.ENTER_FRAME,drawSelectBox);
selectBox.stopDrag();
dragFlag =
false
;
endX = mouseX;
endY = mouseY;
endPoint =
new
Point(endX,endY);
createJPG(mc_image,selectBox,Math.min(recordX,endX),Math.min(recordY,endY));
stage.removeEventListener(MouseEvent.MOUSE_UP,endDraw);
//鼠标在舞台上放开时的事件
}
function
createJPG(m:MovieClip,box:Sprite,originX:Number,originY:Number) {
removeChild(myImage);
var
jpgSource:BitmapData=
new
BitmapData (m.width, m.height);
//先把背景图转换成BitmapData
jpgSource.draw(m);
var
rect:Rectangle =
new
Rectangle(originX,originY,box.width,box.height);
//设置复制的区域和大小
var
pt:Point =
new
Point(0, 0);
//复制到的对象的起始点
var
jpgOut:BitmapData=
new
BitmapData(box.width, box.height);
jpgOut.copyPixels(jpgSource, rect, pt);
//复制区域内容
myImage =
new
Bitmap(jpgOut);
addChild(myImage);
在Flash上点击左键拖动可以复制选中的图片内容到左上角,这个东西的具体用处,大概就是图片信息复制吧。