お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"サーバーオートメーションのイベント取得(1)"



みなさん、こんにちは。十兵衛です。

オートメーションオブジェクトで作成したイベントをクライアント側で取得する
方法です。Delphi4のサンプルを参考にしました。




【サーバー側でイベントを発生させる】
1:新規に通常のプロジェクト(アプリケーション)を作成します。
2:次に[新規作成]→[ActiveX]で[オートメーションオブジェクト]を作
  成します。忘れずに[イベントサポートのためのコードを作成]をチェック
  します。
3:表示されたタイプライブラリエディタでメインのインターフェースにプロパ
  ティを追加します。(例:YourName)
4:表示されたタイプライブラリエディタでイベントインターフェースにメソッ
  ドを追加します。(例:OnChangeとOnChangeParam)
5:4で追加したイベントメソッドの内、OnChangeParamにはパラメータとして
  名前:YourName、タイプ:WideStringにします。
6:実装クラスでプロパティを設定するコードとイベントを呼び出すコードを書
  きます。

【以下作成されたコード】

unit SimpleSV2;

interface

uses
  ComObj, ActiveX, AxCtrls, SimpleSaver_TLB;

type
  TSimpleCom = class(TAutoObject, IConnectionPointContainer, ISimpleCom)
  private
    { Private 宣言 }
    FConnectionPoints: TConnectionPoints;
    FEvents: ISimpleComEvents;

    //プロパティ保持用の変数
    FYourName:WideString;
  public
    procedure Initialize; override;
  protected
    { Protected 宣言 }
    property ConnectionPoints: TConnectionPoints read FConnectionPoints
      implements IConnectionPointContainer;
    procedure EventSinkChanged(const EventSink: IUnknown); override;

    //インターフェースに宣言されている「YourName」プロパティを設定するた
    //めのメソッド
    function Get_YourName: WideString; safecall;
    procedure Set_YourName(const Value: WideString); safecall;
  end;

implementation

uses SimpleSV1, ComServ;

procedure TSimpleCom.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as ISimpleComEvents;
end;

procedure TSimpleCom.Initialize;
begin
  inherited Initialize;
  FConnectionPoints := TConnectionPoints.Create(Self);
  if AutoFactory.EventTypeInfo <> nil then
    FConnectionPoints.CreateConnectionPoint(AutoFactory.EventIID,
      ckSingle, EventConnect);

  //プロパティ用の変数を初期化
  FYourName := '';
end;

function TSimpleCom.Get_YourName: WideString;
begin
  //変数をそのまま返却
  Result := FYourName;
end;

procedure TSimpleCom.Set_YourName(const Value: WideString);
var
  OldName:WideString;
begin
  //プロパティが変更されていたら、次の三手順を行います。
  //1:サーバーのメインフォームに「新旧のプロパティ内容を表示」
  //2:OnChangeイベントを発生させる。
  //3:OnChangeParamイベントを発生させる。
  if Value <> FYourName then begin
     OldName := FYourName;
     FYourName := Value;
     Form1.ListBox1.Items.Add('OnChange: '+
                               OldName + ' To '+FYourName);
     if FEvents <> nil then begin
        FEvents.OnChange;
        FEvents.OnChangeParam(FYourName);
     end;
  end;
end;

initialization
  TAutoObjectFactory.Create(ComServer, TSimpleCom, Class_SimpleCom,
    ciMultiInstance, tmApartment);
end.

サーバー側でのイベント呼び出しはFEvents変数を使って呼び出すだけなので案
外簡単でした。

                                     99/05/04(火) 23:56 十兵衛(BZT01311)

Original document by 十兵衛          氏 ID:(BZT01311)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum