如题,SendMessage和PostMessage需要指定接受者的句柄,可是,如果我想同时给多个进程发消息,又不想一个个的SendMessage的话,该怎么做到?即我想这样做,我这个程序发一个消息,其它所有正在运行的程序都可以接收到,不知可不可以做到,如何做到?

解决方案 »

  1.   

    SendMessage(HWND_BROADCAST,UserMessage,handle,0)
    //UserMessage :自己定义的消息
      

  2.   

    有个API可以广播消息好象是
    BroadcastSystemMessage
    The BroadcastSystemMessage function sends a message to the specified recipients. The recipients can be applications, installable drivers, network drivers, system-level device drivers, or any combination of these system components. To receive additional information if the request is defined, use the BroadcastSystemMessageEx function. long BroadcastSystemMessage(
      DWORD dwFlags,           // broadcast option 
      LPDWORD lpdwRecipients,  // recipients 
      UINT uiMessage,          // message 
      WPARAM wParam,           // first message parameter 
      LPARAM lParam            // second message parameter 
    );
    Parameters
    dwFlags 
    [in] Specifies the broadcast option. This parameter can be one or more of the following values. Value Meaning 
    BSF_ALLOWSFW Windows 2000/XP: Enables the recipient to set the foreground window while processing the message. 
    BSF_FLUSHDISK Flushes the disk after each recipient processes the message. 
    BSF_FORCEIFHUNG Continues to broadcast the message, even if the time-out period elapses or one of the recipients is hung.  
    BSF_IGNORECURRENTTASK Does not send the message to windows that belong to the current task. This prevents an application from receiving its own message.  
    BSF_NOHANG Forces a hung application to time out. If one of the recipients times out, do not continue broadcasting the message.  
    BSF_NOTIMEOUTIFNOTHUNG Waits for a response to the message, as long as the recipient is not hung. Does not time out.  
    BSF_POSTMESSAGE Posts the message. Do not use in combination with BSF_QUERY.  
    BSF_QUERY Sends the message to one recipient at a time, sending to a subsequent recipient only if the current recipient returns TRUE.  
    BSF_SENDNOTIFYMESSAGE Windows 2000/XP: Sends the message using SendNotifyMessage function. Do not use in combination with BSF_QUERY. 
    lpdwRecipients 
    [in] Pointer to a variable that contains and receives information about the recipients of the message. This parameter can be one or more of the following values. Value Meaning 
    BSM_ALLCOMPONENTS Broadcast to all system components. 
    BSM_ALLDESKTOPS Windows NT/2000/XP: Broadcast to all desktops. Requires the SE_TCB_NAME privilege. 
    BSM_APPLICATIONS Broadcast to applications. 
    BSM_INSTALLABLEDRIVERS Windows 95/98/Me: Broadcast to installable drivers. 
    BSM_NETDRIVER Windows 95/98/Me: Broadcast to network drivers. 
    BSM_VXDS Windows 95/98/Me: Broadcast to all system-level device drivers. 
    When the function returns, this variable receives a combination of these values identifying which recipients actually received the message. If this parameter is NULL, the function broadcasts to all components. uiMessage 
    [in] Specifies the message to be sent. 
    wParam 
    [in] Specifies additional message-specific information. 
    lParam 
    [in] Specifies additional message-specific information. 
    Return Values
    If the function succeeds, the return value is a positive value.If the function is unable to broadcast the message, the return value is –1. If the dwFlags parameter is BSF_QUERY and at least one recipient returned BROADCAST_QUERY_DENY to the corresponding message, the return value is zero. To get extended error information, call GetLastError.Res
    If BSF_QUERY is not specified, the function sends the specified message to all requested recipients, ignoring values returned by those recipients.Windows 95/98/Me: BroadcastSystemMessageW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.Example Code
    For an example, see Terminating a Process. Requirements 
      Windows NT/2000/XP: Included in Windows NT 4.0 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Winuser.h; include Windows.h.
      Library: Use User32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.See Also
    Messages and Message Queues Overview, Message and Message Queue Functions, BroadcastSystemMessageEx, SendNotifyMessage Platform SDK Release: August 2001  What did you think of this topic?
    Let us know.  Order a Platform SDK CD Online
    (U.S/Canada)   (International)  Requirements 
      Windows NT/2000/XP: Included in Windows NT 4.0 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Winuser.h; include Windows.h.
      Library: Use User32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
    See Also
    Messages and Message Queues Overview, Message and Message Queue Functions, BroadcastSystemMessageEx, SendNotifyMessage 
      

  3.   

    to  fengsx(风三笑):
       我試過 
    //程序A中代碼
       procedure TForm1.Button1Click(Sender: TObject);
       begin
          SendMessage(HWND_BROADCAST,$0666,0,0);
       end;
    //程序B中代碼
    procedure Ttest.WndProc(var message:Tmessage);
    begin
      If Message.msg=$0666 then
        showmessage('Program A Is Sending message');
      Dispatch(Message);
    end;
    可是B中接收不到消息,如果我把A中代碼改為
       Handle:=Findwindow(0,'test');
       SendMessage(HWND_BROADCAST,$0666,0,0);
    即SendMessage指定Handle就可以,但這不是我想要的,可是廣播消息的話,為什么B沒有接收到呢?
    ========================================================to  songhtao(三十年孤独):
       我試過BroadcastSystemMessage,可是B也沒收到,可能我寫錯了.
      
      

  4.   

    Sorry, 应是 
    Handle:=Findwindow(0,'test'); 
    SendMessage(Handle,$0666,0,0);
    解释一下,B和A是两个独立的应用程序,B的Name是'test'。这两个程序运行后,我在A中发个消息,B要可以接收到。可是,我不想在A中指定是由谁来接收这个消息,因为我可能还要写程序C或D,它们都可以接收到A中的消息,所以楼上几位提到的广播消息是个好提议,可是我在B中为什么没有收到这个广播消息呢?
      

  5.   

    自己定义的消息是要注册的!发送程序中const UserMessageStr = 'testmsg';var
      UserMessage : UINT;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
       UserMessage := RegisterWindowMessage(UserMessageStr); //注册自己的消息
       if UserMessage=0 then
       begin
          //
          showmessage('注册失败!');
       end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       SendMessage(HWND_BROADCAST,UserMessage,handle,0);  //广播消息
    end;//-------------------------------------------------------------------------
    在接受程序中const UserMessageStr = 'testmsg';var
      UserMessage : UINT;procedure TForm1.FormCreate(Sender: TObject);
    begin
       UserMessage := RegisterWindowMessage(UserMessageStr); //注册自己的消息
    end;procedure TForm1.WndProc(var Message: TMessage);
    begin
      if(Message.Msg = UserMessage) then
      begin     //接受到
         //你的处理
      end;
      inherited;
    end;
      

  6.   

    http://www.csdn.net/develop/read_article.asp?id=17197
      

  7.   

    测试成功,我结贴了, 可惜分不是很多,但真的感谢大家尤其是 fengsx(风三笑) 的帮助。