home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2001 September
/
PCWorld_2001-09_cd.bin
/
Software
/
Topware
/
fprint
/
fpdk400.exe
/
samples
/
msvc
/
Apisamp.cpp
next >
Wrap
C/C++ Source or Header
|
2000-07-18
|
10KB
|
390 lines
//
// apisamp.cpp
//
// Copyright (c) 1999-2000 FinePrint Software
// All Rights Reserved.
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "fpapi.h"
#define ZeroInit(pb) memset((void *)pb,0,sizeof(*(pb)))
#define NUM_ELEMENTS(array) (sizeof(array)/sizeof(array[0]))
// forward references
void ShowError (LPCTSTR pszFunc, FpError fpe);
void ShowError (LPCTSTR pszFunc, DWORD dwError);
void Warning (LPCTSTR pszFmt, ...);
/*--------------------------------------------------------------------------
| WinMain
--------------------------------------------------------------------------*/
int WINAPI WinMain (
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR pszCmdLine,
int nCmdShow)
{
HFinePrint hfp = 0;
HFpStat hStat;
LPCTSTR pszStat = TEXT ("FPDK stationery");
LOGFONT lf;
FpError fpe;
DWORD cbAttr;
LPTSTR pch;
TCHAR szFinePrinter[MAX_PATH];
TCHAR szFpdk[MAX_PATH];
TCHAR sz[256];
FpJobCount jcOrig;
FpJobStatus js;
// get the root FPDK folder
GetModuleFileName (NULL, szFpdk, MAX_PATH);
pch = _tcsrchr (szFpdk, TEXT ('\\'));
*pch = 0;
pch = _tcsrchr (szFpdk, TEXT ('\\'));
*pch = 0;
pch = _tcsrchr (szFpdk, TEXT ('\\'));
*pch = 0;
pch = _tcsrchr (szFpdk, TEXT ('\\'));
*pch = 0;
// get the name of the first FinePrinter
if ((fpe = fpGetFinePrinterName (0, szFinePrinter)) != 0) {
ShowError (TEXT ("fpGetFinePrinterName"), fpe);
goto Return;
}
// open a FinePrint session
if ((fpe = fpOpen (szFinePrinter, &hfp)) != 0) {
ShowError (TEXT ("fpOpen"), fpe);
goto Return;
}
// retrieve current FinePrinter settings
eLayoutType lt;
eBorderType bt;
eMarginType mt;
BOOL fAcross;
BOOL fDuplex;
DWORD cCopies;
if ((cbAttr = fpGetLayoutAttr (hfp, eliLayout, <, sizeof (lt))) == 0)
goto Return;
cbAttr = fpGetLayoutAttr (hfp, eliBorders, &bt, sizeof (bt));
cbAttr = fpGetLayoutAttr (hfp, eliOrder, &fAcross, sizeof (fAcross));
cbAttr = fpGetLayoutAttr (hfp, eliStationery, sz, sizeof (sz));
cbAttr = fpGetLayoutAttr (hfp, eliForm, sz, sizeof (sz));
cbAttr = fpGetLayoutAttr (hfp, eliDestPrinter, sz, sizeof (sz));
cbAttr = fpGetLayoutAttr (hfp, eliMargins, &mt, sizeof (mt));
cbAttr = fpGetLayoutAttr (hfp, eliDuplex, &fDuplex, sizeof (fDuplex));
cbAttr = fpGetLayoutAttr (hfp, eliCopies, &cCopies, sizeof (cCopies));
// select the N-up, borders, and margins settings
if ((fpe = fpSetLayoutAttr (hfp, eliLayout, (const void *) eLayout4)) != 0) {
ShowError (TEXT ("fpSetLayoutAttr"), fpe);
goto Return;
}
if ((fpe = fpSetLayoutAttr (hfp, eliBorders, (const void *) eBordersOn)) != 0) {
ShowError (TEXT ("fpSetLayoutAttr"), fpe);
goto Return;
}
if ((fpe = fpSetLayoutAttr (hfp, eliMargins, (const void *) eMarginMedium)) != 0) {
ShowError (TEXT ("fpSetLayoutAttr"), fpe);
goto Return;
}
// create a new stationery
if ((fpe = fpCreateStationery (hfp, pszStat, &hStat)) != 0) {
ShowError (TEXT ("fpCreateStationery"), fpe);
goto Return;
}
// set the watermark text
if ((fpe = fpSetStationeryAttr (
hfp,
hStat,
esiWatermark,
esiaText,
TEXT ("FPDK watermark"))) != 0) {
ShowError (TEXT ("fpSetStationeryAttr"), fpe);
goto Return;
}
// set the footer text
if ((fpe = fpSetStationeryAttr (
hfp,
hStat,
esiFooter,
esiaText,
TEXT ("<Left>FPDK left<Center>FPDK center<Right>FPDK right"))) != 0) {
ShowError (TEXT ("fpSetStationeryAttr"), fpe);
goto Return;
}
// get the current watermark font
if ((cbAttr = fpGetStationeryAttr (
hfp,
hStat,
esiWatermark,
esiaFont,
&lf,
sizeof (lf))) != 0) {
// set the font to Times New Roman
_tcscpy (lf.lfFaceName, TEXT ("Times New Roman"));
if ((fpe = fpSetStationeryAttr (
hfp,
hStat,
esiWatermark,
esiaFont,
&lf)) != 0) {
ShowError (TEXT ("fpSetStationeryAttr"), fpe);
goto Return;
}
}
// close the stationery
if ((fpe = fpCloseStationery (hfp, hStat)) != 0) {
ShowError (TEXT ("fpCloseStationery"), fpe);
goto Return;
}
// select the stationery
if ((fpe = fpSetLayoutAttr (hfp, eliStationery, pszStat)) != 0) {
ShowError (TEXT ("fpSetLayoutAttr"), fpe);
goto Return;
}
#if 0
//
// Set a callback. To see the callback in action, just print a job to
// the FinePrint Driver.
//
// WARNING: this callback stays in effect until either fpClearCallbackDll
// is called, or the dispatcher exits. See the FPDK documentation for
// more details.
//
DWORD cEntryPoints;
TCHAR sz2[256];
_stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("samples\\msvc\\callback.mfc\\DebugA\\fpcallm.dll")); // MFC callback
_stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("samples\\msvc\\callback\\DebugA\\fpcall.dll")); // Win32 callback
_stprintf (sz2, TEXT ("Attempting to load callback DLL \"%s\""), sz);
MessageBox (NULL, sz2, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
if ((fpe = fpSetCallbackDll (hfp, sz, &cEntryPoints)) != 0) {
ShowError (TEXT ("fpSetCallbackDll"), fpe);
goto Return;
}
_stprintf (sz, TEXT ("%d callback entry point(s) installed."), cEntryPoints);
MessageBox (NULL, sz, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
#endif
#if 0
// print a file (assumes FinePrint Driver is the default Windows printer)
SHELLEXECUTEINFO sei;
ZeroInit (&sei);
sei.cbSize = sizeof (sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = TEXT ("print");
sei.nShow = SW_SHOWNORMAL;
_stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("doc\\fpdk.doc"));
sei.lpFile = sz;
if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
ShowError (TEXT ("fpGetJobCount"), fpe);
goto Return;
}
_stprintf (sz2, TEXT ("Attempting to print file \"%s\""), sei.lpFile);
MessageBox (NULL, sz2, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
if (!ShellExecuteEx (&sei)) {
ShowError (TEXT ("ShellExecuteEx"), GetLastError ());
goto Return;
}
// wait maximum 1 minute for job to start, 10 minutes to complete
fpe = fpWaitForJob (hfp, &jcOrig, sei.hProcess, 60, 600, &js);
if (sei.hProcess != NULL)
CloseHandle (sei.hProcess);
if (fpe != 0) {
ShowError (TEXT ("fpWaitForJob"), fpe);
goto Return;
}
#endif
#if 0
// show the FinePrint dialog
DWORD dwDlg;
fpe = fpDisplayDialog (hfp, &dwDlg);
#endif
#if 0
// print the current jobs to the dest printer
fpe = fpPrintAllJobs (hfp, NULL, TRUE, TRUE);
#endif
#if 1
// print to the FinePrint driver
PRINTER_DEFAULTS pd;
HANDLE hPrinter;
int cbDevmode;
DEVMODE *pDevmode;
HDC hdc;
DOCINFO di;
// turn off the FinePrint dialog
fpe = fpSetDeferAll (hfp, TRUE);
fpSetShowDlg (hfp, ShowDlg_Never);
// get FinePrint's current DEVMODE settings
ZeroInit (&pd);
pd.DesiredAccess = PRINTER_ALL_ACCESS;
OpenPrinter (szFinePrinter, &hPrinter, &pd);
cbDevmode = DocumentProperties (
NULL,
hPrinter,
szFinePrinter,
NULL,
NULL,
0);
pDevmode = (DEVMODE *) new BYTE [cbDevmode];
memset (pDevmode, 0, cbDevmode);
DocumentProperties (
NULL,
hPrinter,
szFinePrinter,
pDevmode,
NULL,
DM_OUT_BUFFER);
ClosePrinter (hPrinter);
// create a printer DC and print to it
hdc = CreateDC (NULL, szFinePrinter, NULL, pDevmode);
delete pDevmode;
ZeroInit (&di);
di.cbSize = sizeof (di);
di.lpszDocName = TEXT ("FPDK sample app");
// get the job count before we start the new job
if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
ShowError (TEXT ("fpGetJobCount"), fpe);
goto Return;
}
// print the job
StartDoc (hdc, &di);
StartPage (hdc);
TextOut (hdc, 0, 0, TEXT ("Job 1"), 5);
EndPage (hdc);
EndDoc (hdc);
// wait for the job to complete
if ((fpe = fpWaitForJob (hfp, &jcOrig, NULL, 0, 60, &js)) != 0) {
ShowError (TEXT ("fpWaitForJob"), fpe);
goto Return;
}
// get the job count before we start the new job
if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
ShowError (TEXT ("fpGetJobCount"), fpe);
goto Return;
}
// print the job
StartDoc (hdc, &di);
StartPage (hdc);
TextOut (hdc, 0, 0, TEXT ("Job 2"), 5);
EndPage (hdc);
EndDoc (hdc);
// wait for the job to complete
if ((fpe = fpWaitForJob (hfp, &jcOrig, NULL, 0, 60, &js)) != 0) {
ShowError (TEXT ("fpWaitForJob"), fpe);
goto Return;
}
// send jobs to the physical printer
fpPrintAllJobs (hfp, NULL, TRUE, TRUE);
DeleteDC (hdc);
#endif
// success
MessageBeep (0);
MessageBox (
NULL,
TEXT ("All tests completed successfully!"),
TEXT ("Fpapi Sample Program"),
MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
Return:
if (hfp != 0) {
// close the FinePrint session (keeping the jobs)
fpClose (hfp, FALSE);
}
return (0);
}
/*--------------------------------------------------------------------------
| ShowError
--------------------------------------------------------------------------*/
void ShowError (LPCTSTR pszFunc, FpError fpe)
{
TCHAR sz[256];
wsprintf (
sz,
TEXT ("%s failed with error code %d."),
pszFunc,
fpe);
MessageBeep (0);
MessageBox (
NULL,
sz,
TEXT ("Fpapi Sample Program"),
MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
}
/*--------------------------------------------------------------------------
| ShowError
--------------------------------------------------------------------------*/
void ShowError (LPCTSTR pszFunc, DWORD dwError)
{
TCHAR sz[256];
wsprintf (
sz,
TEXT ("%s failed with error code %d."),
pszFunc,
dwError);
MessageBeep (0);
MessageBox (
NULL,
sz,
TEXT ("Fpapi Sample Program"),
MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
}
/*--------------------------------------------------------------------------
| Warning
|
| Displays a warning message.
--------------------------------------------------------------------------*/
void Warning (LPCTSTR pszFmt, ...)
{
// expand the message string
va_list args;
TCHAR sz[512];
va_start (args, pszFmt);
wvsprintf (sz, pszFmt, args);
va_end (args);
MessageBeep (0);
MessageBox (
NULL,
sz,
TEXT ("Fpapi Sample Program"),
MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
}