把程序2写成程序1的ole server

解决方案 »

  1.   

    //程序1 监听程序
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;const
      MY_MESSAGE = WM_USER + 10;type
      TForm1 = class(TForm)
      private
        { Private declarations }
        procedure MYMESSAGE(var Msg: TMessage); message MY_MESSAGE;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.MYMESSAGE(var Msg: TMessage);
    begin
      Caption := Format('Time:%.6f,W:%d,L:%d', [Now, Msg.WParam, Msg.LParam]);
    end;end.
    //程序2 操作程序
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
      private
        { Private declarations }
        FHandle: THandle;
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}const
      MY_MESSAGE = WM_USER + 10;procedure TForm2.FormCreate(Sender: TObject);
    begin
      FHandle := FindWindow('TForm1', 'Form1');
      if FHandle = 0 then ShowMessage('没有运行监听程序');
    end;procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      if FHandle = 0 then Exit;
      SendMessage(FHandle, MY_MESSAGE, X, Y);
    end;end.