EventManager CWnd Derived

Environment: VC6

This article presents an Event Manager class to route messages to CWnd derived classes. The concept of this class is based on the popular Publisher / Subscriber idea. Any class shall be able to publish an event to a CEventManager object (single instance). Any class that is CWnd derived shall be able to subscribe to the event. When an event is signalled by the Publisher, the event manager shall propagate the message to the subscribers in the order of subscription.

To use this class, first create a single instance of the CEventManager class. The simplest way is to declare the variable in the file containing the CWinApp derived class.
// CWinApp derived class header file.
#include "EventManager.h"
extern CEventManager m_evtManager;

...
// CWinApp derived class source file.
CEventManager m_evtManager;
After this any class that wishes use the CEventManager needs to make sure that the CWinApp derived class' header file is included in that class first.

To publish an event declare the event id in the stdafx.h first as

// stdafx.h
#define MSG_MYMESSAGE   WM_USER+1
Then call the following function in the publisher's constructor
// publisher's class constructor
m_evtManager.PublishEvent(MSG_MYMESSAGE);

Likewise in the subscribers class constructor,

// publisher's class constructor
m_evtManager.SubscribeEvent(this, MSG_MYMESSAGE);
For the subscriber, we would need to declare a function to handle the event and create the message map as follows.
// message map
ON_MESSAGE(MSG_MYMESSAGE, OnMyMessage)
and the function shall be declared as follows
// afx messages
afx_msg void OnMyMessage(WPARAM, LPARAM)
In this fashion, we can have as many subscribers listening to a single event.

For a publisher to signal an event, use the following command,

// publisher
m_evtManager.SignalEvent(MSG_MYMESSAGE,wparam,lparam);

The method described here might not be optimized and there are in fact other alternatives. But what the CEventManager class shall allow us to do is to experiment and control how messages might propagate throughout the application. Thus any improvements are always welcomed.

The demo project attached demonstrates how messages might be propagated throughout the application. The CMainFrame class shall broadcast the Window Moved event which shall be caught by the CView class who shall inturn signal the CDisplayDlg1 class.

Downloads

Download demo project - 52 Kb
Download source - 2 Kb


Comments

  • Great, I like it

    Posted by Legacy on 04/04/2002 12:00am

    Originally posted by: liner alpha

    I removed the event manager's dependency of CWnd and changed the subscriber from CWnd* to HWND. It works in ATL as fine as previously works in MFC.

    Reply
  • Nice Program

    Posted by Legacy on 04/03/2002 12:00am

    Originally posted by: Chris Richardson

    Hi,

    Your idea here is a good one, and I like the neat demo program. Great work. I did notice one bug, and had one suggestion for others who will download this.

    The bug is in DisplayDlg1.cpp, in the MoveDialog function. When you move the window around, the dialog will eventually be sized to nothing. This is because the client rectangle dimensions are being used instead of the window rectangle dimensions. This code will work better:

    CRect rcClient;
    // Call GetWindowRect instead of GetClientRect.
    GetWindowRect(rcClient);

    MoveWindow((int)wParam, (int)lParam, rcClient.Width(), rcClient.Height());


    The suggestion I had for other users, is that if you want to be able to see the HTML page, change the URL in CoolUIDoc.cpp on line 35 to point to the correct location of the HTML page.

    Once again, nice work.

    Chris Richardson

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

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date