1

FlashDevelop で作成された単一ウィンドウの AIR アプリケーションがあり、ユーザーがウィンドウを閉じたときにウィンドウのサイズと位置を記憶させたいと考えています。再度開くと、サイズが変更されてその位置に移動します。

それを行う AS3 スクリプトが見つかりましたが、私のアプリケーションには AS3 がありません。html と js ファイルしかありません。

ウィンドウが閉じられると、ウィンドウの状態が保存され、ウィンドウが開かれると、保存された状態が読み込まれ、保存された状態にサイズ変更/移動されます。これが私が $(document).ready に持っているものです

    var width = screen.width;
    var height = screen.height;
    var p=new DOMParser();
    var xml,x,y,windowWidth,windowHeight;
    var f = window.runtime.flash.filesystem.File
      .applicationDirectory.resolvePath("appPosition.xml");
    if (f.exists){
        var s = new window.runtime.flash.filesystem.FileStream();
        try {
            s.open(f,window.runtime.flash.filesystem.FileMode.READ);
            xml=p.parseFromString(s.readUTFBytes(s.bytesAvailable),"text/xml");
            x = parseInt(xml.childNodes[0].getAttribute("x"),10);
            y = parseInt(xml.childNodes[0].getAttribute("y"),10); 
            windowWidth = parseInt(xml.childNodes[0].getAttribute("width"),10);
            windowHeight = 
                parseInt(xml.childNodes[0].getAttribute("height"),10);
            x = (x >= width) ? 20 : x;
            y = (y >= height) ? 0 : y;
            x = (x <= (width*-1)) ? 20 : x;
            y = (y <= (height*-1)) ? 0 : y;
            if (windowWidth >= (width-60) && windowHeight >= (height-60)) {
//the x and y are not set
                window.runtime.flash.display.NativeWindow.x =20;
                window.runtime.flash.display.NativeWindow.y = 0;
                window.resizeTo(900,600);
// not yet sure if the following works
                window.runtime.flash.display.NativeWindow.maximize();
            }else{
// x and y don't do anything here either
                window.runtime.flash.display.NativeWindow.x = x;
                window.runtime.trace("sitting window x:",x);
                window.runtime.flash.display.NativeWindow.y = y;
                window.resizeTo(windowWidth,windowHeight);
            }
        }catch (e) {
            window.runtime.trace(e.message);
        }finally{
            s.close();
        }
        return;

ウィンドウが閉じられたら、関数でウィンドウの状態を保存したいのですが、何を試しても関数が呼び出されません: $(window).on("close"... または window.onclose= または <document onunluoad=. .. 長いガイドには、現在のウィンドウを取得する方法がないようです: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/ javascript_tools_guide.pdf ウィンドウの作成はカバーされており、一度それを操作することはできますが、ウィンドウを作成することはありません。application.xml は次のようになります。

...
  <initialWindow>
    <title>App Title</title>
    <content>main.html</content>
...

だから私は次の質問があります:

  1. 現在のウィンドウにイベント リスナーを追加するにはどうすればよいですか?
  2. ロード時に現在のウィンドウ x と y を設定するにはどうすればよいですか?
  3. 現在、サイズが変更されているように見えますが、最初はデフォルトのサイズで開き、以前に保存された値にサイズ変更されるため、ウィンドウが表示される前にサイズを変更して移動できますか。
4

1 に答える 1

1

window.nativeWindow を使用して解決しました (nativeWindow の小文字の n に注意してください。AS3 では大文字です)。コードは次のとおりです。

$(document).ready(function(){
    var width = screen.width;
    var height = screen.height;
    var p=new DOMParser();
    var xml,x,y,windowWidth,windowHeight;
    var f = window.runtime.flash.filesystem.File
      .applicationDirectory.resolvePath("appPosition.xml");
    if (f.exists){
        var s = new window.runtime.flash.filesystem.FileStream();
        try {
            s.open(f,window.runtime.flash.filesystem.FileMode.READ);
            xml=p.parseFromString(s.readUTFBytes(s.bytesAvailable),"text/xml");
            x = parseInt(xml.childNodes[0].getAttribute("x"),10);
            y = parseInt(xml.childNodes[0].getAttribute("y"),10); 
            windowWidth = parseInt(xml.childNodes[0]
              .getAttribute("width"),10);
            windowHeight = parseInt(xml.childNodes[0]
             .getAttribute("height"),10);
            x = (x >= width) ? 20 : x;
            y = (y >= height) ? 0 : y;
            x = (x <= (width*-1)) ? 20 : x;
            y = (y <= (height*-1)) ? 0 : y;
            if (windowWidth >= (width-60) && windowHeight >= (height-60)) {
                window.nativeWindow.x =20;
                window.nativeWindow.y = 0;
                window.resizeTo(866,600);
                window.nativeWindow.height = height-60;
                window.nativeWindow.maximize();
            }else{
                window.nativeWindow.x = x;
                window.nativeWindow.y = y;
                window.resizeTo(windowWidth,windowHeight);
            }
        }catch (e) {
            window.runtime.trace(e.message);
        }finally{
            s.close();
            window.nativeWindow.visible=true;
        }
        return;
    }
    try{
        window.nativeWindow.x = 20;
        window.nativeWindow.y = 0;
        window.nativeWindow.width = 866;
        window.nativeWindow.height = height-60;
        window.nativeWindow.visible=true;
    }catch(e){
        window.runtime.trace(e.message);
    }

    window.nativeWindow.addEventListener
      (window.runtime.flash.events.Event.CLOSING, function(e){
        var xml = '<position x="'+window.nativeWindow.x 
          +'" y="'+window.nativeWindow.y+'" width="'
          + window.nativeWindow.width + '" height="' 
          + window.nativeWindow.height + '"/>';
            var f = window.runtime.flash.filesystem.File.
          applicationDirectory.resolvePath("appPosition.xml");
        f = new window.runtime.flash.filesystem.File(f.nativePath);
        var s = new window.runtime.flash.filesystem.FileStream();
        try{
            s.open(f,window.runtime.flash.filesystem.FileMode.WRITE);
            s.writeUTFBytes(xml);
        }catch (e){
            window.runtime.trace(e.message);
        }finally{
            s.close();
        }
    });

});

application.xml で:

  <initialWindow>
    <title>app title</title>
    <content>main.html</content>
    <systemChrome>standard</systemChrome>
    <transparent>false</transparent>
    <visible>false</visible>

visible false を設定すると、ユーザーが最後に閉じた位置にスムーズに表示されます。

于 2013-03-04T02:32:47.720 に答える