Code:
/* snap to the border if within the distance */
var distance = 50;
var snapInterval, direct;
/* if the app is snaping to the screen border*/
var moving = false;
/* move the window to the screen border */
function snapTo(direct)
{
var wnd = getMainWnd();
var reach = false;/* reach the border of the screen? */
switch (direct)
{
case 0: /* left */
if (wnd.left > 0)
wnd.move(wnd.left - 1, wnd.top, wnd.left + wnd.width - 1,
wnd.top + wnd.height);
if (wnd.left == 0) reach = true;
break;
case 1:/* top */
if (wnd.top > 0)
wnd.move(wnd.left, wnd.top - 1, wnd.left + wnd.width,
wnd.top + wnd.height - 1);
if (wnd.top == 0) reach = true;
break;
case 2: /* right */
if (wnd.left + wnd.width < SysInfo.workareaRight)
wnd.move(wnd.left + 1, wnd.top, wnd.left + wnd.width + 1,
wnd.top + wnd.height);
if (wnd.left + wnd.width == SysInfo.workareaRight) reach = true;
break;
case 3: /* bottom */
if (wnd.top + wnd.height < SysInfo.workareaBottom)
wnd.move(wnd.left, wnd.top + 1, wnd.left + wnd.width,
wnd.top + wnd.height + 1);
if (wnd.top + wnd.height == SysInfo.workareaBottom) reach = true;
break;
}
if (reach)
{
/* reach the border, stop moving */
Application.clearInterval(snapInterval);
moving = false;
}
}
/*
handle the WM_ONEXITSIZEMOVE to see if
we need to snap the app to the screen border
*/
getMainWnd().onExitSizeMove = function ()
{
/* Is the app snaping to screen border? */
if (moving) return;
direct = -1;
// left
if (this.left > 0 && this.left <= distance) direct = 0;
// top
else if (this.top > 0 && this.top <= distance) direct = 1;
//right
else if (this.left < SysInfo.workareaRight - this.width &&
this.left >= SysInfo.workareaRight - this.width - distance)
direct = 2;
//bottom
else if (this.top < SysInfo.workareaBottom - this.height &&
this.top >= SysInfo.workareaBottom - this.height - distance)
direct = 3;
if (direct >= 0)
{
// Move the window
snapInterval = Application.setInterval(snapTo, 10, direct);
moving = true;
}
}