2007 年 8 月 24 日 22 時 1 分

プラグインライブラリを用意する


このアーカイブは同期化されません。 mixi の日記が更新されても、このアーカイブには反映されません。


今日は、昨日作った開発用のライブラリを使って、
適当なサンプルとなるプラグイン実装を用意する。

例によって Hello World にでもしとこうか。

========== HelloSaver.cs ==========

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Loafer.ScreenSaver.Plugins {

    public class HelloSaver : ISaver {

        public virtual void Configure(IWin32Window owner) {
            MessageBox.Show(owner,
                    "設定できるオプションがありません。", "設定",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public virtual void Create(IWin32Window window,
                int width, int height) {
            this.window = window;
            this.width = width;
            this.height = height;
        }

        public virtual void Destroy() {
        }

        public virtual void Paint(Graphics g, Rectangle bounds) {

            // 背景を黒で塗りつぶす
            g.FillRectangle(Brushes.Black, bounds);

            // 中央寄せの書式を用意し
            StringFormat format = new StringFormat(
                    StringFormat.GenericDefault);
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // 描画する
            g.DrawString("Hello World!",
                    SystemFonts.MessageBoxFont, Brushes.White,
                    new Rectangle(0, 0, width, height), format);

        }

        protected IWin32Window window;
        protected int width;
        protected int height;

    }

}

========== end of HelloSaver.cs ==========

設定するような項目がないため、Configure メソッドでは
「設定できるオプションがありません。」と
メッセージボックスを表示しておく。

Create メソッドはスクリーンセーバー初期化時に呼ばれ、
ウィンドウ(ハンドル)とそのサイズが渡される。
これは、後で利用するために記憶しておく。

Paint メソッドではこのサイズを基準として、
画面の描画を行うようにする。
上記では、画面の中央に描画するために利用している。

簡単な例なので、必要なコードはこれだけで良い。
後は、アセンブリにビルドしておこう。

$ csc /w:4 /t:library /out:HelloSaver.dll ^
        /r:System.dll /r:System.Drawing.dll ^
        /r:System.Windows.Forms.dll ^
        /r:Loafer.ScreenSaver.dll ^
        HelloSaver.cs

昨日と同じようなコマンドライン引数に加えて、
Loafer.ScreenSaver.dll を参照に加える必要がある。
これで、アセンブリ「HelloSaver.dll」ができた。



Copyright (c) 1994-2007 Project Loafer. All rights reserved.