普通のWindowsアプリケーション、プログラムを起動して画面のサイズ、位置などを変更して終了すると、もう一回プログラムを起動すれば前回終了した状態のまま(画面のサイズ、位置など)で起動することをできます。普通のやり方としては、必要なサイズ、位置情報をファイルに書き込んで、次回起動するとき参照して再現とします。
MFCとか、VB.NETとかで実現すれば既存のpreferencesクラスを使えますが、Adobe AIR 1.1に対しては、そんなクラスが提供されてないです。下記の「CPreferencesManager」というクラスは、スターテク関数で必要なpreferences情報を格納できるクラスです。
該当クラスの使い方としては、ウインドウの移動、リサイズイベントにAttachすることをひつようです。
1、windowMoveイベントへ下記のようなソースを追加する:
- windowMove=”CPreferencesManager.setInt(’main.window.x’, this.nativeWindow.x.toString() );
- CPreferencesManager.setInt(’main.window.y’, this.nativeWindow.y.toString());”
2、creationCompleteの後ろ、ウインドウの移動処理を追加する
- var posX:int = CPreferencesManager.getInt(’main.window.x’ );
- var posY:int = CPreferencesManager.getInt(’main.window.y’ );
- move( posX, posY );
下記はCPreferencesManagerクラスのソースコード:
- package com.some.package
- {
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.net.registerClassAlias;
- import flash.utils.Dictionary;
- import flash.utils.Endian;
- import flash.utils.IDataInput;
- import flash.utils.IDataOutput;
- import flash.utils.IExternalizable;
- public class CPreferencesManager
- {
- private static const PREFS_FILENAME:String = ‘prefs.defaults’;
- private static var _prefValues:Dictionary;
- /**
- * Creates out static instance
- **/
- static public function init():void
- {
- if ( null == _prefValues ) {
- _prefValues = new Dictionary();
- loadPreferences();
- }
- }
- /**
- * Load the preferences file
- */
- static private function loadPreferences():void {
- init();
- var fp: File = File.applicationStorageDirectory;
- fp = fp.resolvePath( PREFS_FILENAME );
- if ( fp.exists ) {
- var _prefsStream:FileStream;
- _prefsStream = new FileStream();
- _prefsStream.open( fp, FileMode.READ);
- _prefsStream.endian = Endian.BIG_ENDIAN;
- readExternal( _prefsStream );
- _prefsStream.close();
- } else {
- savePreferences();
- }
- }
- /**
- * Saves the preferences file
- **/
- static private function savePreferences():void {
- init();
- var fp: File = File.applicationStorageDirectory;
- fp = fp.resolvePath( PREFS_FILENAME );
- var _prefsStream:FileStream;
- _prefsStream = new FileStream();
- _prefsStream.open( fp, FileMode.WRITE );
- _prefsStream.endian = Endian.BIG_ENDIAN;
- writeExternal( _prefsStream );
- _prefsStream.close();
- }
- /**
- * Sets a preferences value
- **/
- static public function setPreference( name:String, value:Object ):void {
- init();
- _prefValues[ name ] = value;
- savePreferences();
- }
- /**
- * Sets an <int> preferences value
- **/
- static public function setInt( name:String, value:int ):void {
- setPreference( name, value );
- }
- /**
- * Get a preferences values
- **/
- static public function getPreference( name:String ):* {
- init();
- var value:Object;
- try {
- value = _prefValues[ name ];
- } catch( e:Error ) {
- value = null;
- }
- return value;
- } // getPreference
- /**
- * Get an <b>int</b> preferences values
- **/
- static public function getInt( name:String ):int {
- var value:* = getPreference( name );
- if ( null == value ) {
- return 0;
- } else {
- return value as int;
- }
- }
- /**
- * Loads serializes preferences data structure from <b>input:IDataInput</b>.
- **/
- static private function readExternal(input:IDataInput):void
- {
- registerClassAlias( “flash.utils.Dictionary”, Dictionary );
- _prefValues = input.readObject() as Dictionary;
- } // readExternal
- /**
- * Writes preferences data as a serialized structure to <b>output:IDataOutput</b>.
- **/
- static private function writeExternal(output:IDataOutput):void
- {
- registerClassAlias( “flash.utils.Dictionary”, Dictionary );
- output.writeObject( _prefValues );
- } // writeExternal
- } // class
- } // package
メインコンテンツEND ■
Posted on Wednesday, 5th November 2008 by admin
Tags: Adobe, CPreferencesManager, preferences
Posted in AIR | Comments (1) | 379 views

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