码迷,mamicode.com
首页 > 编程语言 > 详细

转载图片浏览器-MFC+Flash

时间:2014-12-02 20:32:50      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

转载自:http://www.cnblogs.com/gameprogram/archive/2011/12/13/2285634.html

import flash.events.Event;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.ContextMenuEvent;
import flash.display.Loader;
import fl.motion.MatrixTransformer;
import flash.display.Sprite;
import flash.display.Shape;
import flash.system.LoaderContext;
import flash.display.DisplayObject;
import flash.geom.Transform;
import flash.geom.Point;
import flash.geom.Matrix;

/*

function fl_ClickToPosition(event:MouseEvent):void
{

    
    fscommand("MsgBox", "callC++");
}

function ChangeButton(msg:String):void
{
    btn.label = msg;
}
*/

fscommand("fullscreen","true")

var isEnd = false;
var showImg:Sprite;
var ldr :Loader;
var pic_width:Number;
var pic_height:Number;


var img:Bitmap;

// for drag move
var canDrag = false;
var LBDownX:Number;
var LBDownY:Number;

// for scale
var scaleW;
var scaleH;

// for rotate
var isRBDown = false;
var rotRadian = 0;
var rbX = 0;
var rbY = 0;
var rotScalar = 0.002;
var originX;
var originY;

// for change image
var go_limit = 0;
var go_left = false;
var go_right = false;
var size_scalarx = 1;
var size_scalary = 1;

//stage.stageWidth = 1300;

stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
//stage.addEventListener(MouseEvent.MOUSE_MOVE, onRBMove);


ExternalInterface.addCallback("MouseWheel",ZoomTo);
ExternalInterface.addCallback("RBDown",onRBDown);
ExternalInterface.addCallback("RBUp", onRBUp);
ExternalInterface.addCallback("RBMove",onRBMove);
ExternalInterface.addCallback("RotateX",onMoveRotateX);
ExternalInterface.addCallback("InitImage",LoadImage);
ExternalInterface.addCallback("ShowMouse",ShowMouse);

function ShowMouse():void
{
    Mouse.show();
}

var square:Shape = new Shape();
this.addChild(square);

function drawSqaure(x:Number, y:Number, w:Number, h:Number):void
{
    square.graphics.clear();
    square.graphics.lineStyle(2, 0);
    square.graphics.beginFill(200);
    //square.rotation = 20;
    square.graphics.drawRect(x, y, w, h);
    square.graphics.endFill();
}

stage.addEventListener(Event.RESIZE, DoResize);
function DoResize(evt:Event):void
{
    //fscommand("Resize",""+stage.stageHeight);
    go_limit = stage.stageWidth/10;

}


function Look_Left_Img(evt:Event):void
{
    trace("left click");
    fscommand("Look_Left_Img", "callC++");
}

function Look_Right_Img(evt:Event):void
{
    fscommand("Look_Right_Img", "callC++");
}

//function InitUI():void
//{
//    button_left.height = stage.stageHeight/5;
//    button_right.height = button_left.height;
//    button_left.width = stage.stageWidth/21;
//    button_left.x = button_left.width/2;
//    button_right.x = stage.stageWidth-button_right.width/2;
//    button_left.y = stage.stageHeight/2;
//    button_right.y = button_left.y;
//
//
//    button_left.addEventListener(MouseEvent.CLICK, Look_Left_Img);
//    button_right.addEventListener(MouseEvent.CLICK, Look_Right_Img);
//}

function InitCursor():void
{
    go_limit = stage.stageWidth/10;
    //fscommand("MsgBox",""+stage.stageWidth)
    left_cur.visible = false;
    right_cur.visible = false;
    normal_cur.visible = false;
    
    //left_cur.height = stage.stageHeight/10;
//    right_cur.height = left_cur.height;
//    left_cur.width = stage.stageWidth/15;
//    right_cur.width = left_cur.width;
//
//    normal_cur.height = stage.stageHeight/15;
//    normal_cur.width = stage.stageWidth/22;
    
//    fscommand("MsgBox",""+stage.scaleX);
    
//    normal_cur.scaleX = 1/stage.scaleX
    //normal_cur.scaleY = 1/stage.scaleY
    
    
    left_cur.scaleX = size_scalarx;
    left_cur.scaleY = size_scalary;
    normal_cur.scaleX = size_scalarx;
    normal_cur.scaleY = size_scalary;
    right_cur.scaleX = size_scalarx;
    right_cur.scaleY = size_scalary;
    trace("scaleX"+normal_cur.scaleX)
    
    
    
//    var cur_size = 16;
//    left_cur.height = cur_size;
//    left_cur.width = cur_size;
//    right_cur.height = cur_size;
//    right_cur.width = cur_size;
//    normal_cur.height = cur_size;
//    normal_cur.width = cur_size;
    
    trace(flash.system.Capabilities.screenResolutionX);
    
    
}

// load image
function LoadImage(filename:String, width:Number, height:Number):void
{
    
    //size_scalarx = stage.stageWidth/flash.system.Capabilities.screenResolutionX;
    //size_scalary = stage.stageHeight/flash.system.Capabilities.screenResolutionY;
    trace("size_scalar:"+size_scalarx)
    if (ldr == null)
    {
        ldr = new Loader();
    }
    else
    {
        ldr.unload();
    }
    
    ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
    ldr.load(new URLRequest(filename), new LoaderContext());
    pic_width = width;
    pic_height = height;
    
    
    InitCursor();
    
    //InitUI();
}
LoadImage("E:/1.jpg", 460, 360);

function calculatePos(img):void
{
    trace(stage.stageWidth);
    trace(stage.stageHeight);
    var m_img_w = stage.stageWidth;
    var m_img_h = stage.stageHeight;
    var fix_w = m_img_w;
    var fix_h = m_img_h;
    
    if (pic_width > pic_height)
    {
        fix_w = m_img_w;
        fix_h = pic_height * m_img_w / pic_width;
    }
    else
    {
        fix_w = pic_width * m_img_h / pic_height;
        fix_h = m_img_h;
    }
    
    trace(fix_w);
    img.width = fix_w;
    img.height = fix_h;
    
    var wx = m_img_w - fix_w;
    var wy = m_img_h - fix_h;
    
    var m_img_x = 0;
    var m_img_y = 0;
    
    if (wx > 0)
        wx = wx/2;
    if (wy > 0)
        wy = wy/2;
        
    if( m_img_x > 0 )
        m_img_x = 0;

    if( m_img_x < wx )
        m_img_x = wx;

    if(m_img_y > 0)
        m_img_y = 0;

    if( m_img_y < wy )
        m_img_y = wy;    
        
    trace(m_img_x);
    showImg.x = m_img_x;
    showImg.y = m_img_y;
}

function reset_cursor():void
{
    trace("cur_X:"+stage.mouseX)
    if (stage.mouseX < go_limit)
    {
        //trace(evt.stageX);
        left_cur.visible = true;
        normal_cur.visible = false;
        
        go_left = true
    }
    else if ( stage.mouseX > stage.stageWidth - go_limit)
    {
        right_cur.visible = true;
        normal_cur.visible = false;
        
        go_right = true;
    }
    else
    {
        go_left = false;
        go_right = false;
        left_cur.visible = false;
        right_cur.visible = false;
        normal_cur.visible = true;
        normal_cur.x = stage.mouseX;
        normal_cur.y = stage.mouseY;
    }
    
    if (go_left)
    {
        left_cur.x = stage.mouseX;
        left_cur.y = stage.mouseY;
    }
    else if(go_right)
    {
        right_cur.x = stage.mouseX;
        right_cur.y = stage.mouseY;
    }
}

function ldr_complete(evt:Event):void
{
     var bmp:Bitmap = ldr.content as Bitmap;
    //trace(bmp);
     scaleW = 0.5 * bmp.width;
    scaleH = 0.5 * bmp.height;


    img = new Bitmap(bmp.bitmapData);
   // img.x = 0;
   // img.y = 0;
    img.rotation = 0;
    //img.width = 300;
    //img.height = 100;
    if (showImg != null)
        this.removeChild(showImg);
    showImg = new Sprite();

    
    //img.scaleX = size_scalarx;
    //img.scaleY = size_scalary;
    calculatePos(img);
    showImg.addChild(img);
    this.addChild(showImg);
    //showImg.rotationY =0;
    originX = 0;
    originY = 0;
    
    //showImg.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    showImg.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    showImg.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    showImg.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
    
    //swapChildren(left_cur,showImg);
    setChildIndex(normal_cur, 3);
    setChildIndex(left_cur, 2);
    setChildIndex(right_cur, 1);
    setChildIndex(showImg, 0);
    //swapChildren(right_cur,showImg);
    
    reset_cursor();
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove)
stage.addEventListener(MouseEvent.CLICK, onStageMouseClick)
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown)

function onStageMouseDown(evt:MouseEvent):void
{
    if (showImg.hitTestPoint(evt.stageX, evt.stageY))
    {
        trace("hit");
        canDrag = true;
        LBDownX = evt.stageX;
        LBDownY = evt.stageY;
    }
}

function onStageMouseMove(evt:MouseEvent):void
{
    Mouse.hide();
    if (evt.stageX < go_limit)
    {
        //trace(evt.stageX);
        left_cur.visible = true;
        normal_cur.visible = false;

        go_left = true
        left_cur.x = evt.stageX;
        left_cur.y = evt.stageY;
    }
    else if ( evt.stageX > stage.stageWidth - go_limit)
    {
        right_cur.visible = true;
        normal_cur.visible = false;

        go_right = true;
        right_cur.x = evt.stageX;
        right_cur.y = evt.stageY;
    }
    else
    {
        go_left = false;
        go_right = false;
        left_cur.visible = false;
        right_cur.visible = false;
        normal_cur.visible = true;
        normal_cur.x = evt.stageX;
        normal_cur.y = evt.stageY
    }
    
    if (canDrag)
    {
        //trace("move");
        var offsetX = evt.stageX - LBDownX;
        var offsetY = evt.stageY - LBDownY;
        
        LBDownX = evt.stageX;
        LBDownY = evt.stageY;
        
        showImg.x += offsetX;
        showImg.y += offsetY;
    }

    
}

function onStageMouseClick(evt:MouseEvent):void
{
    if (go_left)
    {
        trace("left");
        Mouse.hide();
        fscommand("Look_Left_Img", "callC++");
        //LoadImage("E:/mm.jpg", 850, 1000);
    }
    else if (go_right)
    {
        trace("right");
        
        fscommand("Look_Right_Img", "callC++");
        Mouse.hide();
    }
}



//function onMouseDown(evt:MouseEvent):void
//{
//    trace("mouse down");
//    canDrag = true;
//    LBDownX = evt.stageX;
//    LBDownY = evt.stageY;
//}

function onMouseMove(evt:MouseEvent):void
{
    
}

function onMouseUp(evt:MouseEvent):void
{
    canDrag = false;
}

function onMouseWheel(evt:MouseEvent):void
{
    trace(evt.delta);
    ZoomTo(evt.delta*0.2 , evt.delta*0.2);
}

function IsPosInImage(posX:Number, posY:Number):Boolean
{
    return img.hitTestPoint(posX,posY);
    
}

function ScaleAroundPoint(objToScale:DisplayObject,regX:Number, regY:Number, scaleX:Number, scaleY:Number):void
{
    if (!objToScale)
    {
        return;
    }
    
    var transformVector:Point = new Point((regX - objToScale.x)*scaleX, (regY - objToScale.y)*scaleY);
    
    objToScale.x = regX - transformVector.x;
    objToScale.y = regY - transformVector.y;
    objToScale.scaleX *= scaleX;
    objToScale.scaleY *= scaleY;
}

// scale
function ZoomTo(scalerH:Number, scalerW:Number):void
{
    if ((img.height + scaleH * scalerH) > 0 && (img.width + scaleW * scalerW) > 0)
    {
        
        if (IsPosInImage(stage.stageWidth/2, stage.stageHeight/2))
        {
            trace("hit");
            //imgMatrix.translate(-stage.width/2, -stage.height/2);
            //imgMatrix.tx -=stage.width/2;
            //imgMatrix.ty -=stage.height/2;
            //imgMatrix.scale(scalerW+1.0, scalerH + 1.0);
            
            //imgMatrix.tx +=stage.width/2;
            //imgMatrix.ty +=stage.height/2;
            
            //imgMatrix.translate(stage.width/2, stage.height/2);
            ScaleAroundPoint(showImg, stage.stageWidth/2, stage.stageHeight/2, scalerW+1.0, scalerH + 1.0);
        }
        else
        {
            //imgMatrix.translate(-(transformPoint.x + originX), -(transformPoint.y + originY));
//            imgMatrix.scale(scalerW+1.0, scalerH + 1.0);
//            imgMatrix.translate(transformPoint.x + originX, transformPoint.y + originY);
            ScaleAroundPoint(showImg, showImg.x+showImg.width/2, showImg.y+showImg.height/2, scalerW+1.0, scalerH + 1.0);
        }
        
        //showImg.scaleX += scalerW;
        //showImg.scaleY += scalerH;
        

        trace((showImg.x+showImg.width/2), (showImg.y+showImg.height/2));
        
        //showImg.transform.matrix = imgMatrix;
        
        //drawSqaure(showImg.x, showImg.y, showImg.width, showImg.height);
        //drawSqaure(img.x, img.y, img.width, img.height);
    }
}

// radian to degree
function RadianToDegree(radian:Number):Number
{
    return radian * 180/Math.PI;
}

// get radian after mouseMove
function RadianAfterMove(mouseX:Number, mouseY:Number):Number
{
    var flag:Boolean = true;
                
    //根据鼠标偏移距离来决定旋转角度大小
    var diffX:Number = mouseX - rbX;
    var diffY:Number = mouseY - rbY;
        
    var distance:Number = Math.sqrt(diffX*diffX+diffY*diffY);
        
    rbX = mouseX;
    rbY = mouseY;
    
    if (Math.abs(diffY) >= Math.abs(diffX))
    {
        if (diffY < 0)
            flag = false;
    }
    else
    {
        if (diffX < 0)
            flag = false;
    }
                           
    if (flag)
    {    
        rotRadian += distance * rotScalar;
        return distance * rotScalar;

    }
    else
    {
        rotRadian -= distance * rotScalar;
        return -distance * rotScalar;
    }
    
    //return rotRadian;
}

// right button down
function onRBDown(posX:Number, posY:Number):void
{
    isRBDown = true;
    rbX = posX;
    rbY = posY;
    //normal_cur.visible = false;
    //size_scalarx = stage.mouseX/rbX;
    //size_scalary = stage.mouseY/rbY;
}

// mouse move when right button down
//function onRBMove(evt:MouseEvent):void
function onRBMove(posX:Number, posY:Number):void
{
    if (isRBDown)
    {
        trace(showImg);
        normal_cur.x = normal_cur.x+ (posX - rbX)*size_scalarx;
        normal_cur.y = normal_cur.y + (posY - rbY)* size_scalary;
        //var imgMatrix:Matrix3D = showImg.transform.matrix3D; 
//
//        var centerX:Number =  img.width / 2; 
//
//        var centerY:Number =  img.height / 2; 
//
//        var centerPoint:Vector3D = new Vector3D(centerX, centerY, 0); 
//
//        var transformPoint:Vector3D = imgMatrix.transformVector(centerPoint);
//
//        imgMatrix.prependTranslation((transformPoint.x + originX), (transformPoint.y + originY), 0);
//
//        imgMatrix.prependRotation(RadianAfterMove(posX, posY)*10, Vector3D.Z_AXIS);
//
//        imgMatrix.prependTranslation(-transformPoint.x + originX, -transformPoint.y + originY, 0);
//
//        showImg.transform.matrix3D = imgMatrix;

        var imgMatrix:Matrix = showImg.transform.matrix;
        
        var centerX:Number = img.width/2;
        var centerY:Number = img.height/2;
        
        var centerPoint:Point = new Point(centerX, centerY);
        var transformPoint:Point = imgMatrix.transformPoint(centerPoint);
        
        imgMatrix.translate(-transformPoint.x, -transformPoint.y);
        imgMatrix.rotate(RadianAfterMove(posX, posY))
        imgMatrix.translate(transformPoint.x, transformPoint.y);
        
        showImg.transform.matrix = imgMatrix;
        //drawSqaure(showImg.x, showImg.y, showImg.width, showImg.height);
        //drawSqaure(img.x, img.y, img.width, img.height);
    }
}

// right button up
function onRBUp():void
{
    isRBDown = false;
    //normal_cur.visible = true;
    //normal_cur.x = stage.mouseX;
    //normal_cur.y = stage.mouseY;
}

function onMoveRotateX(posX:Number, posY:Number):void
{
    showImg.rotationX += RadianAfterMove(posX, posY)*10;
}

stop();

功能为左键拖动图片,中键缩放图片,右键旋转图片。

这是flash里的代码,生成SWF后,用MFC中的shockwaveflash读入,再截获右键消息,使右键菜单屏蔽。

转载图片浏览器-MFC+Flash

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/Qianqian-Dong/p/4138289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!