Integrating the API with the Windows Event Loop

Several examples throughout this book use the function SPD_MainLoop() to process events. If you are creating an application that contains a separate event loop, you must use SPD_ProcessEvents() instead to allow the two to coexist. The example below shows how to process the SPD event loop in a Windows MFC application. You must use this method as a replacement for the calls to SPD_MainLoop() in examples in 6.1 and 6.6.

...
#include "spd.h"
...
#define ID_SPD_PROCESS 5000
...
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_UPDATE_COMMAND_UI_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM,
OnUpdateViewStyles)
ON_COMMAND_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnViewStyle)
END_MESSAGE_MAP()
...
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this,
TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
//
// Initialize the API with application name = "MyApp"
// application qualifier = "MyQual"
SPD_InitClient("MyApp", "MyQual");
//
// Create a timer to process events. Every second (arbitrary interval)
// a WM_TIMER message will be received with an event id of ID_SPD_PROCESS
//
SetTimer( ID_SPD_PROCESS, 1000, NULL );
return 0;
}
...
//
// WM_TIMER message handler
//
// The SetTimer() call made during initialization causes a WM_TIMER message with
// ID_SPD_PROCESS as the event number to be issued every second (arbitrary
// interval).
// When the message is received, SPD_ProcessEvents is called.
//
void CMainFrame::OnTimer(UINT nIDEvent)
{
SPD_errorTP spdErr;
if (nIDEvent == ID_SPD_PROCESS)
{
spdErr = SPD_ProcessEvents();
if (spdErr != SPD_normalCN)
{
// error processing
AfxMessageBox("I forgot to call SPD_InitClient()!");
}
}
else
CFrameWnd::OnTimer(nIDEvent);
}...