Windows Messages

A Windows message is a type of event generated by the operating system and queued up for an application. The Windows Platform SDK defines constants using the naming convention WM_XXX for messages. The Events package defines the IWinEvent interface, which provides methods for accessing the message ID, WPARAM, LPARAM, and result value of a Windows message. All Window events implement the IWinEvent interface. The CWinEvent template class makes it easy to implement new Windows event classes by providing a default implementation of the IWinEvent interface.

Implementing the WM_PAINT message by using the CWinEvent class shows how the WM_PAINT message is implemented using the CWinEvent class. First, an interface is derived from IWinEvent that defines a method for cracking the WM_PAINT message.

Implementing the WM_PAINT message by using the CWinEvent class

class __declspec(uuid("8A6AF181-40A7-11d3-AF0D-006008AFE059"))

IWindowPaintEvent : public IWinEvent

{

public:

virtual HDC GetDC() const = 0;

};

Next, the class CWindowPaintEvent is derived from the CWinEventBase class, as shown in Deriving CWindowPaintEvent from the CWinEventBase class. The template parameters for CWinEventBase are the interface for the event and the GUID for that interface. The CWindowPaintEvent class implements the Dispatch() method inherited from IEvent and the GetDC() method inherited from IWindowPaintEvent.

Deriving CWindowPaintEvent from the CWinEventBase class

class CWindowPaintEvent : public CWinEventBase<IWindowPaintEvent,

&IID_IWindowPaintEvent>

{

virtual bool Dispatch(IQueryGuid* pIListener);

virtual HDC GetDC() const;

};

 

bool CWindowPaintEvent::Dispatch(IQueryGuid* pIListener)

{

bool bHandled = false;

IWindowListener* pIWindowListener =

guid_cast<IWindowListener*>(pIListener);

 

if (pIWindowListener != NULL)

{

bHandled = pIWindowListener->OnPaint(GetDC());

pIWindowListener->Release();

}

 

return bHandled;

}

 

HDC CWindowPaintEvent::GetDC() const

{

return (HDC) GetWParam();

}