import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=600)
winform.add(
button={cls="button";text="旋转图片";left=597;top=276;right=714;bottom=370;z=2};
button6={cls="button";text="改变图片大小";left=602;top=126;right=713;bottom=207;z=3};
custom={cls="custom";text="custom";left=0;top=0;right=300;bottom=300;bgcolor=12632256;z=1}
)
/*}}*/
import mouse.hook
//io.open();
/*
import console;
*/
var show = false;
var rotateOn = false;
var resizeOn = false;
var cusRect = null; //custom控件的Rect
var angle = 0; //鼠标绕图片中心旋转的角度
var mouseSpeed = 10;
var distance = 0; //鼠标位置与图片左上角的距离
var brush = gdip.solidBrush( 0xFFFFFFFF );
var bmp = gdip.bitmap("d:\Project\aardio\backup\asuka.bmp")
var img = gdip.bitmap(bmp.width,bmp.height)
var graphics = gdip.graphics(img);
//为了模拟绕中心旋转,取得画布平移的XY
//思路:知道两点坐标,可获取中点坐标
var getOffsetXY = function( w,h,angle ){
var sinA = math.sin(math.rad(angle));
var cosA = math.cos(math.rad(angle));
var x = (w*cosA-h*sinA)/2 ;
var y = (w*sinA+h*cosA)/2 ;
x = w/2 - x;
y = h/2 - y;
return x,y;
}
//一定程度避免重复计算
//顺时针旋转1°画布平移的x,y值
var tsltX,tsltY = getOffsetXY( bmp.width,bmp.height,1 );
//逆时针旋转1°画布平移的x,y值
var negTsltX,negTsltY = getOffsetXY( bmp.width,bmp.height,-1 );
//鼠标绕图片中心旋转时相比上一次移动的角度
//很大程度是旋转1°
var offsetAngle = function(cusRect,mx,my){
var x = mx - (cusRect.right+cusRect.left)/2;
var y = my - (cusRect.bottom+cusRect.top)/2;
var newAngle = ..math.round( ..math.deg(..math.atan2(y,x)))
var offsetAngle = newAngle - angle;
angle = newAngle;
return offsetAngle;
}
//绘制图片
var draw = function( msg,p ){
show = true;
gdi.windowBuffer( winform.custom.hwnd,
function( hdc,hMemDc,hMemBitmap,width,height ){
var g = gdip.graphics(hMemDc)
g.compositingQuality = 2/*_GdipCompositingQualityHighQuality*/ ;
g.smoothingMode = 2/*_GdipSmoothingModeHighQuality*/ ;
select( msg ) {
case "rotate" {
graphics.fillRectangle(brush,0,0,bmp.width,bmp.height)
graphics.rotate( p.angle, 1/*_MatrixOrderAppend*/ )
graphics.translate( p.tsltX,p.tsltY, 1/*_MatrixOrderAppend*/)
graphics.drawImageRect(bmp,0,0,bmp.width,bmp.height)
g.setClipRect(0,0,width,height);
g.drawImageRect(img,0,0,width,height)
}
case "room" {
graphics.drawImageRect(bmp,0,0,bmp.width,bmp.height)
//graphics.drawImageRectRect(bmp,0,0,width,height,0,0,bmp.width,bmp.height)
g.drawImageRect(img,0,0,width,height)
//g.drawImageScale(img,winform.custom.getRect())
//io.print(bmp.width,bmp.height,img.width,img.height)
}
case "redraw" {
g.drawImageRect(img,0,0,width,height)
}
}
g.delete();
::BitBlt(hdc, 0, 0, width, height, hMemDc, 0, 0, 0xCC0020/*_SRCCOPY*/);
}
)
}
//定义获取鼠标的移动速度
SystemParametersInfo = ::User32.api("SystemParametersInfoA","int(int uAction,int uParam,int &lpvParam,int fuWinIni)" )
winform.button.oncommand = function(id,event){
cusRect = winform.custom.getRect(true);
rotateOn = true;
//获得当前鼠标速度以便恢复
var rs,lpvParam = SystemParametersInfo(0x70/*_SPI_GETMOUSESPEED*/,0,0,0)
mouseSpeed = lpvParam;
var lowMouseSpeed = 5;
//降低鼠标速度
::User32.SystemParametersInfo(0x71/*_SPI_SETMOUSESPEED*/,0,lowMouseSpeed,0)
var mx,my = win.getMessagePos();
var x = mx - (cusRect.right+cusRect.left)/2;
var y = my - (cusRect.bottom+cusRect.top)/2;
angle = ..math.round( ..math.deg(..math.atan2(y,x)))
var hk = mouse.hook();
hk.proc = function(msg,x,y,mouseData,injected,flags,timeStamp,extraInfo){
if( injected ) return;
select(msg) {
case 0x201/*_WM_LBUTTONDOWN*/{
rotateOn = false;
hk.close();
::User32.SystemParametersInfo(0x71/*_SPI_SETMOUSESPEED*/,0,mouseSpeed,0);
return true;
}
case 0x200/*_WM_MOUSEMOVE*/{
if(rotateOn){
var offsetAngle = offsetAngle(cusRect,x,y);
var prama = {
["angle"] = offsetAngle;
};
select( offsetAngle ) {
case 1 {
prama = {
["angle"] = 1;
["tsltX"] = tsltX;
["tsltY"] = tsltY;
}
draw( "rotate",prama );
}
case -1 {
prama = {
["angle"] = -1;
["tsltX"] = negTsltX;
["tsltY"] = negTsltY;
}
draw( "rotate",prama );
}
case !0 {
prama.tsltX,prama.tsltY = getOffsetXY( bmp.width,bmp.height,offsetAngle );
draw( "rotate",prama );
}
}
}
}
}
}
}
winform.button6.oncommand = function(id,event){
var mx,my = win.getMessagePos();
cusRect = winform.custom.getRect(true);
distance = math.round( math.sqrt( (cusRect.left-mx)**2+(cusRect.top-my)**2 ) );
resizeOn = true;
var hk = mouse.hook();
hk.proc = function(msg,x,y,mouseData,injected,flags,timeStamp,extraInfo){
if( injected ) return;
select(msg) {
case 0x201/*_WM_LBUTTONDOWN*/{
resizeOn = false;
hk.close();
return true;
}
case 0x200/*_WM_MOUSEMOVE*/{
if(resizeOn){
var d = math.round( math.sqrt( (x-cusRect.left)**2+(y-cusRect.top)**2 ) );
var ratio = math.round( d/distance,2 );
var w = (cusRect.right - cusRect.left)*ratio;
var h = (cusRect.bottom - cusRect.top)*ratio;
winform.custom.setPos(,,w,h); //这里窗口会闪烁
draw( "room" );
}
}
}
}
}
winform.custom.onEraseBkgnd = function(hwnd,message,wParam,lParam){
if(show){
draw( "redraw" );
return false;
}
//show = true;
}
winform.onClose = function(hwnd,message,wParam,lParam){
bmp.dispose();
img.dispose();
graphics.delete();
}
winform.custom.image = "d:\Project\aardio\backup\asuka.bmp";
winform.show()
win.loopMessage();