普通のWindowsアプリケーション、プログラムを起動して画面のサイズ、位置などを変更して終了すると、もう一回プログラムを起動すれば前回終了した状態のまま(画面のサイズ、位置など)で起動することをできます。普通のやり方としては、必要なサイズ、位置情報をファイルに書き込んで、次回起動するとき参照して再現とします。

MFCとか、VB.NETとかで実現すれば既存のpreferencesクラスを使えますが、Adobe AIR 1.1に対しては、そんなクラスが提供されてないです。下記の「CPreferencesManager」というクラスは、スターテク関数で必要なpreferences情報を格納できるクラスです。

該当クラスの使い方としては、ウインドウの移動、リサイズイベントにAttachすることをひつようです。

1、windowMoveイベントへ下記のようなソースを追加する:

  1. windowMove=”CPreferencesManager.setInt(main.window.x’, this.nativeWindow.x.toString() );
  2. CPreferencesManager.setInt(main.window.y’, this.nativeWindow.y.toString());”

2、creationCompleteの後ろ、ウインドウの移動処理を追加する

  1. var posX:int = CPreferencesManager.getInt(main.window.x);
  2. var posY:int = CPreferencesManager.getInt(main.window.y);
  3. move( posX, posY );

下記はCPreferencesManagerクラスのソースコード:

  1. package com.some.package
  2. {
  3.     import flash.filesystem.File;
  4.     import flash.filesystem.FileMode;
  5.     import flash.filesystem.FileStream;
  6.     import flash.net.registerClassAlias;
  7.     import flash.utils.Dictionary;
  8.     import flash.utils.Endian;
  9.     import flash.utils.IDataInput;
  10.     import flash.utils.IDataOutput;
  11.     import flash.utils.IExternalizable;
  12.     public class CPreferencesManager
  13.     {
  14.         private static const PREFS_FILENAME:String        = ‘prefs.defaults’;
  15.         private static var _prefValues:Dictionary;
  16.         /**
  17.          * Creates out static instance
  18.          **/
  19.         static public function init():void
  20.         {
  21.             if ( null == _prefValues ) {
  22.                 _prefValues = new Dictionary();
  23.                 loadPreferences();
  24.             }
  25.         }
  26.         /**
  27.          * Load the preferences file
  28.          */
  29.         static private function loadPreferences():void {
  30.             init();
  31.             var fp: File = File.applicationStorageDirectory;
  32.             fp = fp.resolvePath( PREFS_FILENAME );
  33.             if ( fp.exists ) {
  34.                 var    _prefsStream:FileStream;
  35.                 _prefsStream = new FileStream();
  36.                 _prefsStream.open( fp, FileMode.READ);
  37.                 _prefsStream.endian = Endian.BIG_ENDIAN;
  38.                 readExternal( _prefsStream );
  39.                 _prefsStream.close();
  40.             } else {
  41.                 savePreferences();
  42.             }
  43.         }
  44.         /**
  45.          * Saves the preferences file
  46.          **/
  47.         static private function savePreferences():void {
  48.             init();
  49.             var fp: File = File.applicationStorageDirectory;
  50.             fp = fp.resolvePath( PREFS_FILENAME );
  51.             var _prefsStream:FileStream;
  52.             _prefsStream = new FileStream();
  53.             _prefsStream.open( fp, FileMode.WRITE );
  54.             _prefsStream.endian = Endian.BIG_ENDIAN;
  55.             writeExternal( _prefsStream );
  56.             _prefsStream.close();
  57.         }
  58.         /**
  59.          * Sets a preferences value
  60.          **/
  61.         static public function setPreference( name:String, value:Object ):void {
  62.             init();
  63.             _prefValues[ name ] = value;
  64.             savePreferences();
  65.         }
  66.         /**
  67.          * Sets an <int> preferences value
  68.          **/
  69.         static public function setInt( name:String, value:int ):void {
  70.             setPreference( name, value );
  71.         }
  72.         /**
  73.          * Get a preferences values
  74.          **/
  75.         static public function getPreference( name:String ):* {
  76.             init();
  77.             var value:Object;
  78.             try {
  79.                 value = _prefValues[ name ];
  80.             } catch( e:Error ) {
  81.                 value = null;
  82.             }
  83.             return value;
  84.         } // getPreference
  85.         /**
  86.          * Get an <b>int</b> preferences values
  87.          **/
  88.         static public function getInt( name:String ):int {
  89.             var value:* = getPreference( name );
  90.             if ( null == value ) {
  91.                 return 0;
  92.             } else {
  93.                 return value as int;
  94.             }
  95.         }
  96.         /**
  97.          * Loads serializes preferences data structure from <b>input:IDataInput</b>.
  98.          **/
  99.         static private function readExternal(input:IDataInput):void
  100.         {
  101.             registerClassAlias(flash.utils.Dictionary”, Dictionary );
  102.             _prefValues = input.readObject() as Dictionary;
  103.         } // readExternal
  104.         /**
  105.          * Writes preferences data as a serialized structure to <b>output:IDataOutput</b>.
  106.          **/
  107.         static private function writeExternal(output:IDataOutput):void
  108.         {
  109.             registerClassAlias(flash.utils.Dictionary”, Dictionary );
  110.             output.writeObject( _prefValues );
  111.         } // writeExternal
  112.     } // class
  113.  
  114. } // package
メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Wednesday, 5th November 2008 by admin

Tags: , ,
Posted in AIR | Comments (1) | 379 views

Related Posts

One Response to “CPreferencesManager: Adobe AIRのpreferencesマネージャークラスである”

  1. Adobe AIRアプリケーションのインストールパスにファイルURIを取得するサンプルソースコード | DigiTechLog Dot Com Says:

    [...] この前に、Adobe AIR 1.5でSQLiteデータベースを暗号化とするサンプルソースコード、CPreferencesManager: Adobe AIRのpreferencesマネージャークラスであるなどを勉強しました。今回はAIRの勉強を続きましょうー [...]

Leave a Reply