有两个程序,a,b。   
  单击a程序的一个button后,b程序根据a程序的操作立刻反应,然后进行某个操作。应该怎么办?
  这里的主要难点是如何让数据在两个程序间交换。
谢谢   

解决方案 »

  1.   

    进程通讯(自定义消息)SenderMain.pas 
    unit SenderMain;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
      Const WM_COMM = WM_APP+100;
      Const str='Receiver';
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      function ShowProcess(Whandle: HWND):boolean ;
    var
      Form1: TForm1;
    implementation
    {$R *.dfm}
    function ShowProcess(Whandle: HWND):boolean ;
    begin
    ShowWindow(Whandle,SW_restore);
    SetForegroundWindow(Whandle);
    SetActiveWindow(Whandle);
    result :=true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
    whd : HWND;
    begin
    whd:=FindWindow(nil,pchar(str));
    ShowProcess(whd);
    SendMessage(whd,WM_COMM,0,0);
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    var
    whd : HWND;
    begin
    whd:=FindWindow(nil,pchar(str));
    ShowProcess(whd);
    SendMessage(whd,WM_COMM,0,1);
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    var
    whd : HWND;
    begin
    whd:=FindWindow(nil,pchar(str));
    ShowProcess(whd);
    SendMessage(whd,WM_COMM,1,0);
    end;
    end.
    receivemain.pasunit receivemain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
      Const WM_COMM = WM_APP+100;
    type
      TForm1 = class(TForm)
        Edit1: TEdit;
      private
        { Private declarations }
        procedure userpro(var msg:TMessage); message WM_COMM;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation//uses receivemain;{$R *.dfm}
    procedure TForm1.userpro(var msg:TMessage); 
    begin
    if(msg.WParam=0)and(msg.LParam=0)then
    Edit1.Text:='命令一 ';
    if(msg.WParam=0)and(msg.LParam=1)then
    Edit1.Text:='命令二 ';
    if(msg.WParam=1)and(msg.LParam=0)then
    Edit1.Text:='命令三 ';
    end;end.
      

  2.   

    进程通信(自定义数据)
    进程通信(自定义数据)
    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    Const WM_COMM = WM_APP+100;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        Button2: TButton;
        Button3: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        procedure GetUsername(var msg:TWmCopyData); message WM_COPYDATA;
        procedure ReturnResult(var msg:TMessage); message WM_COMM;
      public
        { Public declarations }
      end;
      function EnumWndProc(AWnd:HWND;AlParam:LPARAM):Boolean;stdcall;
    var
      Form1: TForm1;
    implementation
    {$R *.dfm}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Clear;
      EnumWindows(@EnumWndProc,0);//枚举窗体,发送消息
    end;
    function EnumWndProc(AWnd:HWND;AlParam:LPARAM):Boolean;stdcall;
    var
      WndCaption: array[0..254] of Char;
      ds: TCopyDataStruct;
    begin
      GetWindowText(AWnd, @WndCaption, 254);
      if WndCaption[0]<>chr(0) then
        Form1.ListBox1.Items.Add(Format('%d=%s',[AWnd,StrPas(WndCaption)]));
      if WndCaption='彭轩' then
      begin
         ds.cbData := Length (Form1.Edit1.Text) + 1;
         GetMem (ds.lpData, ds.cbData ); //为传递的数据区分配内存
         StrCopy (ds.lpData, PChar (Form1.Edit1.Text));
         SendMessage(AWnd,WM_COPYDATA,Form1.Handle,Cardinal(@ds));
      end;
      Result := True;
    end;
    procedure TForm1.ReturnResult(var msg: TMessage);
    begin
      if msg.WParam=1 then
         ShowMessage('返回');
    end;
    procedure TForm1.GetUsername(var msg: TWmCopyData);
    begin
      //接收其它进程发来的消息,并判断消息来源不是自己本身
      //取出消息携带的数据与Edit1.Text比较
      if msg.From<>Form1.Handle then
      begin
        if Edit1.Text=StrPas(msg.CopyDataStruct^.lpData) then
           SendMessage(msg.From,WM_COMM,1,0);
      end;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      ListBox1.Clear;
      EnumWindows(@EnumWndProc,0);
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      Edit1.Text:='aaa';
    end;
    end.
     注意 EnumWindows(@EnumWndProc,0);//枚举窗体,发送消息
    在枚举窗体时,会认为同一个程序的窗口和工程是两个窗体,比如一个工程中
    Form1.Caption:='进程通信2';
    Application.Title:='进程通信';
    这里会找到两个窗体 "进程通信2"和"进程通信"