FlashCCでデスクトップガジェットを作成するメモ
ActionScript3 + Air で、Windowsのデスクトップに置いておけるガジェットを作成するメモ。以下のようなガジェットを作成できます。
基本的には普通のFlashと同じ手順で作れるけど、
- タスクバーに表示されない、不定形ウインドウで表示する
- ステータスバーが無いので、アプリ自体をドラッグで移動&ダブルクリックで終了する
- OSの起動時、ガジェットも自動的に起動する
- 表示位置は前回終了時を記憶する
のような仕様にしようとしたら、それなりにコードを書く必要があったのでメモ。
普通にウインドウを表示するとタスクバーに表示されるので、その対策にウインドウ非表示で起動して、ActionScriptからNativeWindowクラスで新しくウインドウを開く。Main
というクラスでムービークリップを作成して、それをActionScriptで新しいウインドウに配置する。…としています。
スタートアップへの登録は専用の命令で可能。表示位置の記憶はSharedObjectを使用。
具体的には、「ファイル → AIR 3.6 for Desktop 設定 → 詳細 → 初期ウインドウ設定」で「最大化を有効にする」「最小化を有効にする」「リサイズを有効にする」「起動後に表示する」のチェックを外した上で、以下のActionScriptでウインドウを表示する。
//アプリのデータを復元
var sharedObject:SharedObject = SharedObject.getLocal('App');
var appX:int = 100;
var appY:int = 100;
if (sharedObject.data.x) {
appX = sharedObject.data.x;
}
if (sharedObject.data.y) {
appY = sharedObject.data.y;
}
//スタートアップに登録
if (sharedObject.data.startup != true) {
NativeApplication.nativeApplication.startAtLogin = true;
}
//ウインドウを開く
var initOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
initOptions.transparent = true;
initOptions.systemChrome = NativeWindowSystemChrome.NONE;
initOptions.type = NativeWindowType.LIGHTWEIGHT;
var mainWindow:NativeWindow = new NativeWindow(initOptions);
mainWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
mainWindow.stage.align = StageAlign.TOP_LEFT;
mainWindow.stage.stageWidth = this.stage.stageWidth;
mainWindow.stage.stageHeight = this.stage.stageHeight;
mainWindow.x = appX;
mainWindow.y = appY;
mainWindow.activate();
//メインのMCを配置
var m:Main = new Main();
m.x = Math.floor(m.width / 2);
m.y = Math.floor(m.height / 2);
mainWindow.stage.addChild(m);
//移動処理
m.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownToMove);
function mouseDownToMove(event:Event):void
{
mainWindow.startMove();
}
//終了処理
m.doubleClickEnabled = true;
m.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickToExit);
function doubleClickToExit(event:MouseEvent):void
{
closeWindow();
}
mainWindow.addEventListener(Event.CLOSING, closingToExit);
function closingToExit(event:Event):void
{
closeWindow();
}
function closeWindow():void
{
//アプリのデータを記録
var sharedObject:SharedObject = SharedObject.getLocal('App');
sharedObject.data.startup = true;
sharedObject.data.x = mainWindow.x;
sharedObject.data.y = mainWindow.y;
sharedObject.flush();
//終了
NativeApplication.nativeApplication.exit();
}
this.stop();
Mainムービークリップの内容は、基本的には普通のFlashと同じ要領で作れます。
まだまだ勉強中なので、ツッコミ歓迎…。_(:3 」∠ )_