2011-03-31 88 views
2

您可以设置HTA窗口的大小,但我找不到获取其大小的方法。如何获得HTA窗口的大小?

我能想到的只有document.body.offsetWidth.offsetHeight,但那些给你的视口大小不是实际的窗口大小。

有可能知道吗?

回答

2

似乎没有任何属性或方法来获取该信息。现在beta版本发布的IE9有新的属性outterWidthoutterHeight,但这不是现在的选择。

于是我想出了一个解决方法是:

function addWinSizeGetterFuncs(){ 
    var currViewportWidth = document.body.offsetWidth ; 
    var currViewportHeight = document.body.offsetHeight ; 
    resizeTo(currViewportWidth,currViewportHeight) ; 
    var chromeWidth = currViewportWidth-document.body.offsetWidth ; 
    var chromeHeight = currViewportHeight-document.body.offsetHeight ; 
    resizeTo(currViewportWidth+chromeWidth,currViewportHeight+chromeHeight) ; 
    window.getWidth = new Function('return document.body.offsetWidth+'+chromeWidth) ; 
    window.getHeight = new Function('return document.body.offsetHeight+'+chromeHeight) ; 
} 

什么该函数的作用是创造了window对象两个新的方法:window.getWidth()window.getHeight()。他们将获得实际的窗口大小。
该功能需要存在body对象,因此必须在BODY标签之后执行。


而现在我们在这里,同样的问题适用于窗口位置。你无法获得实际的窗口位置。你可以得到的是相对于屏幕的视口位置。 因此同样workaroud服务于这个问题,以及:

function addWinPosGetterFuncs(){ 
    var currViewportLeft = screenLeft ; 
    var currViewportTop = screenTop ; 
    moveTo(currViewportLeft,currViewportTop) ; 
    var viewportOffsetX = screenLeft-currViewportLeft ; 
    var viewportOffsetY = screenTop-currViewportTop ; 
    moveTo(currViewportLeft-viewportOffsetX,currViewportTop-viewportOffsetY) ; 
    window.getScreenX = new Function('return screenLeft-'+viewportOffsetX) ; 
    window.getScreenY = new Function('return screenTop-'+viewportOffsetY) ; 
} 

同样,函数创建两个新的方法,将检索实际窗口位置:window.getScreenX()window.getScreenY()

问题解决了。我让你们弄清楚功能是如何工作的;)

1

精缩版:

window.pos=new function(w,h,x,y){with(document.body)return(
    resizeTo(w=offsetWidth,h=offsetHeight),resizeTo(w+(w=w-offsetWidth),h+(h=h-offsetHeight)), 
    moveTo (x=screenLeft ,y=screenTop ),moveTo (x-(x=screenLeft-x),y-(y=screenTop-y )),{ 
     w:function(){return offsetWidth +w}, 
     h:function(){return offsetHeight+h}, 
     x:function(){return screenLeft -x}, 
     y:function(){return screenTop -y} 
})}; 
相关问题