Windows Message Broadcaster

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 that exists 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 windows 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.

The CBroadcaster class is designed as a static class that keeps the user from having to create an instance of the class, but leaves the user with the burden of calling the cleanup function. It could have been done as a singleton, but the overhead is too much for such a simple class.

How It Works

Initialization: Initialize the broadcaster class by calling CBroadcaster::InitBroadcaster();. This method does nothing at this 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 by 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.

To examine the return value of each send/post message, the caller must call CBroadcaster::StartSend or CBroadcaster::StartPost to initialize the send/post process. The user then can call CBroadcaster::SendNext or CBroadcaster::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

BOOL CMyApp::InitInstance()
{
   CWinApp::InitInstance();
   CBroadcaster::InitBroadcaster();

   ...

}

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

   return CWinApp::ExitInstance();
}

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
   CBroadcaster::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);
}


About the Author

Ali Rafiee

Ali Rafiee has been developing windows applications for the past 14 years using Visual C++, 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 for the past 7 years. 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 him (He is always trying to learn something new).

Downloads

Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming webcast to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds