When the message broadcast option is set in the Document Converter products, the printer driver broadcasts messages to all running applications each time one of the following events occur:
Printer Enabled.
Start of document.
Start of page.
End of page.
End of document.
Printer Buffer is full.
Cancelling document.
Each Windows messages is made of three parts:
A message ID. This is a 32-bit value.
A first parameter known as wParam. This is a 16-bit value under Windows 98 and a 32-bit value under Windows NT/2000/XP.
A second parameter known as lParam. This is a 32-bit value.
The message Id generated by the Document Converter products can be retrieved by calling the Windows API RegisterMessage function:
nPDFDriverMessage = RegisterWindowMessage(_T("PDF Driver Event"))
This is needed to intercept messages generated by the printer driver.
Associated with each printer event, is an event code which is given in the wParam parameter. The following event codes are defined by the Windows Device Driver Kit:
Event |
Value |
Description |
---|---|---|
DOCUMENTEVENT_STARTDOCPRE |
5 |
Before StartDoc is handled. |
DOCUMENTEVENT_STARTDOCPOST |
13 |
After StartDoc is handled. |
DOCUMENTEVENT_STARTPAGE |
6 |
StartPage handled. |
DOCUMENTEVENT_ENDPAGE |
7 |
EndPage handled. |
DOCUMENTEVENT_ENDDOCPRE |
8 |
Before EndDoc is handled. |
DOCUMENTEVENT_ENDDOCPOST |
12 |
After EndDoc is handled. |
DOCUMENTEVENT_LAST |
14 |
|
// this event is privately defined by the Document Converter products
#define DOCUMENTEVENT_ENABLEDPRE(DOCUMENTEVENT_LAST + 1) // before checking if printer is enabled
Event |
wParam |
LOWORD(lParam) |
HIWORD(lParam) |
---|---|---|---|
Before StartDoc is handled. |
DOCUMENTEVENT_STARTDOCPRE |
JobID |
hDC |
After StartDoc is handled. |
DOCUMENTEVENT_STARTDOCPOST |
JobID |
hDC |
StartPage. |
DOCUMENTEVENT_STARTPAGE |
JobID |
hDC |
EndPage. |
DOCUMENTEVENT_ENDPAGE |
JobID |
hDC |
Before EndDoc is handled. |
DOCUMENTEVENT_ENDDOCPRE |
JobID |
hDC |
After EndDoc is handled. |
DOCUMENTEVENT_ENDDOCPOST |
JobID |
hDC |
Event |
LOWORD(wParam) |
HIWORD(wParam) |
lParam |
---|---|---|---|
Before checking if printer is enabled. |
DOCUMENTEVENT_ENABLEDPRE |
0 |
|
Before StartDoc is handled. |
DOCUMENTEVENT_STARTDOCPRE |
0 |
hDC |
After StartDoc is handled. |
DOCUMENTEVENT_STARTDOCPOST |
JobID |
hDC |
StartPage. |
DOCUMENTEVENT_STARTPAGE |
JobID |
hDC |
EndPage. |
DOCUMENTEVENT_ENDPAGE |
JobID |
hDC |
Before EndDoc is handled. |
DOCUMENTEVENT_ENDDOCPRE |
JobID |
hDC |
After EndDoc is handled. |
DOCUMENTEVENT_ENDDOCPOST |
JobID |
hDC |
The drawback of this method of intercepting messages is that the printer driver will broadcast messages to all running applications which might result in slowing down the whole system. To avoid this message broadcast, the printer driver can be programmed to send messages only to a specific application or window by calling the SendMessagesTo function.
UINT nPDFEventMessage = 0; // ID of the message sent by the driver
BEGIN_MESSAGE_MAP(CDLLTestAppDlg, CDialog)
// trap PDF driver events
ON_REGISTERED_MESSAGE( nPDFEventMessage, OnPDFEvent )
END_MESSAGE_MAP()
BOOL CDLLTestAppDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// get windows version number
OSVERSIONINFO osvi;
memset( &osvi, 0, sizeof( osvi ) );
osvi.dwOSVersionInfoSize = sizeof( osvi );
GetVersionEx( &osvi );
m_bNT = (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT);
// get ID of message sent by PDF driver
nPDFEventMessage = RegisterWindowMessage( "PDF Driver Event" );
// send messages to our application only
TCHAR className[64];
GetClassName( m_hWnd, className, sizeof(className) );
SendMessagesTo( theApp.m_converter, className );
// make sure event broadcast option is set
SetFileNameOptions( theApp.m_converter, BroadcastMessages );
return TRUE;
}
LONG CDLLTestAppDlg::OnPDFEvent( UINT wParam, LONG lParam )
{
// a Document Converter event has occured
//
DWORD jobId;
HDC hDC;
WORD eventId;
// get event ID
eventId = LOWORD( wParam );
if ( m_bNT )
{
// Windows NT/2000/XP
jobId = HIWORD( wParam );
hDC = (HDC)lParam;
}
else
{
// Windows 98/Me
jobId = LOWORD( lParam );
hDC = (HDC)HIWORD( lParam );
}
switch ( eventId )
{
case DOCUMENTEVENT_ENABLEDPRE: // before checking if printer is enabled
m_log.AddString( "Before checking if printer is enabled" );
// activate printer before starting to print
// this is needed with the developer version only
EnablePrinter( theApp.m_converter, "Amyuni PDF Converter Evaluation, "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA" );
break;
case DOCUMENTEVENT_STARTDOCPRE: // before StartDoc is handled by the printer
char buf[255] = "";
// display file name of document being printed
GetGeneratedFilename( theApp.m_converter, buf, sizeof( buf ) );
m_log.AddString( CString( "Destination file: " ) + buf );
m_log.AddString( "StartDoc called but not handled yet" );
break;
case DOCUMENTEVENT_STARTDOCPOST: // after StartDoc is handled by the printer
m_log.AddString( "StartDoc called and handled by the driver" );
break;
case DOCUMENTEVENT_STARTPAGE: // StartPage handled by the printer
m_log.AddString( "StartPage called and handled" );
break;
case DOCUMENTEVENT_ENDPAGE: // EndPage handled by the printer
m_log.AddString( "EndPage called and handled" );
break;
case DOCUMENTEVENT_ENDDOCPRE: // before EndDoc is handled by the printer
{
// get title of document being printed
char buf[255] = "";
GetDocumentTitle( theApp.m_converter, jobId, buf, sizeof( buf ) );
// display title of document being printed
m_log.AddString( CString( "Document title: " ) + buf );
m_log.AddString( "EndDoc called but not handled yet" );
break;
}
case DOCUMENTEVENT_ENDDOCPOST: // after EndDoc is handled by the printer
m_log.AddString( "EndDoc called and handled" );
break;
default:
return DOCUMENTEVENT_UNSUPPORTED;
}
return DOCUMENTEVENT_SUCCESS;
}