Click here to Skip to main content
15,886,837 members
Articles / Desktop Programming / MFC
Article

Windows Message Broadcaster

Rate me:
Please Sign up or sign in to vote.
4.52/5 (15 votes)
27 Feb 2008CPOL3 min read 76.1K   1.6K   41   7
Using this class, you can broadcast a message to windows without having to know their handle. You can also broadcast the same message to multiple windows at the same time.

Introduction

When developing Doc/View applications using MFC, I have often found the need to send a message from one view or window to another, where the views don't share a common document. An example of this would be to show properties of an object which exist in one view within a property window. As anyone who has tried to do this knows, this involves a painful process of grabbing the property window's handle at creation time and passing it to all the other windows that need to use the property window. Or you can broadcast messages, and anyone call tell you that Microsoft Windows does not have a useful broadcasting mechanism. My CBroadcaster class takes the hassle out of broadcasting messages. It can also be used to send the same message to multiple windows at the same time, with just one call.

How it Works

Initialize the broadcaster class, by calling CBroadcaster::InitBroadcaster();. This method does nothing at the moment. It’s simply a placeholder that couples with CBroadcaster::UninitBroadcaster().

Registering for Reception

The windows that are interested in receiving broadcast messages simply register themselves with the broadcast class using CBroadcaster::Register(…). The method allows a single window to register more than one message, by calling it repeatedly with a new value for Message.
Note: One good practice would be to call CBroadcaster::Unregister(this) in the destructor of your window class.

Sending Messages

There are two ways to send messages. One method will allow the caller to examine the return value of the processed message for each window, and the other will simply send the message to all registered windows and disregard the return values.

In order to examine the return value of each send/post message, the caller must call CBoardcaster::StartSend or CBoardcaster::StartPost to initialize the send/post process. The user can then call CBoardcaster::SendNext or CBoardcaster::PostNext to send a message. While these methods return true, a window interested in the specified message was found and the message was sent. A return value of FALSE indicates the end of the recipient list.

To simply broadcast the message with no regard to return values, call CBroadcaster::SendToAll() or CBroadcaster::PostToAll().

Cleanup

Don't forget to call CBroadcaster::UninitBroadCaster() before your program exits. Because this is a static class that does not have to be instantiated, the destructor will not be called. This method will do all the cleanup necessary in the event that a window did not Unregister itself from the CBroadcaster.

Example code:

C++
BOOL CMyApp::InitInstance()
{
   ...
   CBroadcaster::InitBroadcaster();
   ...
}

int CMyApp::ExitInstance()
{
   ...
   CBroadcaster::UninitBroadcaster();
   ...
}

void CView1::OnInitalUpdate()
{
   CView::OnInitalUpdate();
   CBroadcaster::Register(this,WM_MYMESSAGE);
}

void CView2::OnInitalUpdate()
{
   CView::OnInitalUpdate();
   CBroadcaster::Register(this,WM_MYMESSAGE);
   CBroadcaster::Register(this,WM_ANOTHERMESSAGE);
}

void CView3::OnItemSelected()
{
   LRESULT Result
   CBoardcaster::StartSend(WM_MYMESSAGE,0,pItem,Result);
   while (CBroadcaster::SendNext(Result))
   {
      if (Result == 0)
      {
          MessageBox("The result was not good");
      }
   }
}

void CView4::OnListSelChanged()
{
   CBroadcaster::PostToAll(WM_ANOTHERMESSAGE,0,ItemIndex);
}

Update - Version 2

The first version of the CBroadcaster class has a weakness. The problem surfaces if a window unregisters itself while responding to a message sent from CBroadcaster, when it sends a message to a window, and the window is iterating through its internal list of CRegisteredWindows. When it sends a message to a window, and the window unregisters itself as the result of the message, the unregistering can possibly remove the CRegisteredWindow object from the list, which will put the “message sending” functions' iterator in a bad state, since the current item it was pointing to is no longer there.

The only way around this problem was NOT to delete the actual CRegisteredWindow object, but instead mark it as deleted and then do the actual deletion at a later time. Therefore in version 2, there is a new function called CheckDeleted, which will loop through the registered windows, and delete the ones that are marked as deleted. This method can be called anywhere in the code (of course except when responding to a message from the CBroadcaster). If you prefer to use version 2, I would recommend putting a call to this method in the OnIdle method of your application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Ali Rafiee has been developing windows applications using C++ since 1991, and he hasn't looked back since. Ali has been a software development consultant for must of his career, but he has finally settled down and has been working for an educational software company since 2000. While he is not working, he is either learning C#, flying airplanes, playing with his daughter, or answering peoples question on newsgroups, he finds that to be a great learning tool for himself.

Ali is also a Microsoft Visual C++ MVP.

Comments and Discussions

 
QuestionCan we use these classes in C#? Pin
manoj_8028-Jun-11 7:51
manoj_8028-Jun-11 7:51 
GeneralExcelent! Pin
gizmocuz8-Jun-06 8:03
gizmocuz8-Jun-06 8:03 
GeneralWorking with splitter windows Pin
9090344423-Dec-05 0:55
9090344423-Dec-05 0:55 
GeneralThanks Pin
Frederick.J21-Apr-05 21:35
Frederick.J21-Apr-05 21:35 
GeneralFew Errors found Pin
Massimo Colurcio22-Jan-05 9:20
professionalMassimo Colurcio22-Jan-05 9:20 
GeneralRe: Few Errors found Pin
Ali Rafiee8-Apr-05 6:24
Ali Rafiee8-Apr-05 6:24 
Generalsimple but neat! Pin
Mister Transistor21-Dec-04 1:29
Mister Transistor21-Dec-04 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.