Delphi:如何向其他应用程序发送命令?

Lit*_*per 11 windows delphi process delphi-7

如何从其他Delphi创建的应用程序发送和接收命令?
我想将命令发送到我的其他应用程序.

And*_*and 23

发件人:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  WM_MY_MESSAGE = WM_USER + 1;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  h: HWND;
begin
  h := FindWindow(nil, 'My Second Window');
  if IsWindow(h) then
    SendMessage(h, WM_MY_MESSAGE, 123, 520);
end;

end.
Run Code Online (Sandbox Code Playgroud)

接收器:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

const
  WM_MY_MESSAGE = WM_USER + 1;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  protected
    procedure WndProc(var Message: TMessage); override;    
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WndProc(var Message: TMessage);
begin
  inherited;
  case Message.Msg of
    WM_MY_MESSAGE:
      ShowMessageFmt('The other application sent the data %d and %d.', [Message.WParam, Message.LParam]);
  end;
end;

end.
Run Code Online (Sandbox Code Playgroud)

确保接收表单的标题是"我的第二个窗口".

  • @Kev:你是对的.对不起.对我来说太过分了.@David:谢谢你恢复我的帖子!我很抱歉变得如此沮丧. (5认同)
  • @user你不能教堆栈溢出编程!这是一个问答网站.答案中提供的代码必须被理解为说明性的.无论如何,安德烈亚斯的答案非常好. (2认同)

Sor*_*row 5

Windows消息可能是一个解决方案 - 可以在这里找到一篇有趣的文章:http://delphi.about.com/od/windowsshellapi/a/aa020800a.htm