home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 October / PCWorld_2002-10_cd.bin / Software / Topware / fprint / fpdk400.exe / samples / msvc / Apisamp.cpp next >
C/C++ Source or Header  |  2000-07-18  |  10KB  |  390 lines

  1. //
  2. //    apisamp.cpp
  3. //
  4. //        Copyright (c) 1999-2000 FinePrint Software
  5. //        All Rights Reserved.
  6. //
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <tchar.h>
  10. #include "fpapi.h"
  11.  
  12. #define ZeroInit(pb)        memset((void *)pb,0,sizeof(*(pb)))
  13. #define NUM_ELEMENTS(array)    (sizeof(array)/sizeof(array[0]))
  14.  
  15. // forward references
  16. void ShowError (LPCTSTR pszFunc, FpError fpe);
  17. void ShowError (LPCTSTR pszFunc, DWORD dwError);
  18. void Warning (LPCTSTR pszFmt, ...);
  19.  
  20. /*--------------------------------------------------------------------------
  21. |    WinMain
  22. --------------------------------------------------------------------------*/
  23. int WINAPI WinMain (
  24.                 HINSTANCE    hInstance,
  25.                 HINSTANCE    hPrevInstance,
  26.                 LPSTR        pszCmdLine,
  27.                 int            nCmdShow)
  28. {
  29.     HFinePrint    hfp = 0;
  30.     HFpStat        hStat;
  31.     LPCTSTR        pszStat = TEXT ("FPDK stationery");
  32.     LOGFONT        lf;
  33.     FpError        fpe;
  34.     DWORD        cbAttr;
  35.     LPTSTR        pch;
  36.     TCHAR        szFinePrinter[MAX_PATH];
  37.     TCHAR        szFpdk[MAX_PATH];
  38.     TCHAR        sz[256];
  39.     FpJobCount    jcOrig;
  40.     FpJobStatus    js;
  41.  
  42. // get the root FPDK folder
  43.     GetModuleFileName (NULL, szFpdk, MAX_PATH);
  44.     pch = _tcsrchr (szFpdk, TEXT ('\\'));
  45.     *pch = 0;
  46.     pch = _tcsrchr (szFpdk, TEXT ('\\'));
  47.     *pch = 0;
  48.     pch = _tcsrchr (szFpdk, TEXT ('\\'));
  49.     *pch = 0;
  50.     pch = _tcsrchr (szFpdk, TEXT ('\\'));
  51.     *pch = 0;
  52.  
  53. // get the name of the first FinePrinter
  54.     if ((fpe = fpGetFinePrinterName (0, szFinePrinter)) != 0) {
  55.         ShowError (TEXT ("fpGetFinePrinterName"), fpe);
  56.         goto Return;
  57.     }
  58.  
  59. // open a FinePrint session
  60.     if ((fpe = fpOpen (szFinePrinter, &hfp)) != 0) {
  61.         ShowError (TEXT ("fpOpen"), fpe);
  62.         goto Return;
  63.     }
  64.  
  65. // retrieve current FinePrinter settings
  66.     eLayoutType    lt;
  67.     eBorderType    bt;
  68.     eMarginType    mt;
  69.     BOOL        fAcross;
  70.     BOOL        fDuplex;
  71.     DWORD        cCopies;
  72.  
  73.     if ((cbAttr = fpGetLayoutAttr (hfp, eliLayout, <, sizeof (lt))) == 0)
  74.         goto Return;
  75.     cbAttr = fpGetLayoutAttr (hfp, eliBorders, &bt, sizeof (bt));
  76.     cbAttr = fpGetLayoutAttr (hfp, eliOrder, &fAcross, sizeof (fAcross));
  77.     cbAttr = fpGetLayoutAttr (hfp, eliStationery, sz, sizeof (sz));
  78.     cbAttr = fpGetLayoutAttr (hfp, eliForm, sz, sizeof (sz));
  79.     cbAttr = fpGetLayoutAttr (hfp, eliDestPrinter, sz, sizeof (sz));
  80.     cbAttr = fpGetLayoutAttr (hfp, eliMargins, &mt, sizeof (mt));
  81.     cbAttr = fpGetLayoutAttr (hfp, eliDuplex, &fDuplex, sizeof (fDuplex));
  82.     cbAttr = fpGetLayoutAttr (hfp, eliCopies, &cCopies, sizeof (cCopies));
  83.  
  84. // select the N-up, borders, and margins settings
  85.     if ((fpe = fpSetLayoutAttr (hfp, eliLayout, (const void *) eLayout4)) != 0) {
  86.         ShowError (TEXT ("fpSetLayoutAttr"), fpe);
  87.         goto Return;
  88.     }
  89.     if ((fpe = fpSetLayoutAttr (hfp, eliBorders, (const void *) eBordersOn)) != 0) {
  90.         ShowError (TEXT ("fpSetLayoutAttr"), fpe);
  91.         goto Return;
  92.     }
  93.     if ((fpe = fpSetLayoutAttr (hfp, eliMargins, (const void *) eMarginMedium)) != 0) {
  94.         ShowError (TEXT ("fpSetLayoutAttr"), fpe);
  95.         goto Return;
  96.     }
  97.  
  98. // create a new stationery
  99.     if ((fpe = fpCreateStationery (hfp, pszStat, &hStat)) != 0) {
  100.         ShowError (TEXT ("fpCreateStationery"), fpe);
  101.         goto Return;
  102.     }
  103.  
  104. // set the watermark text
  105.     if ((fpe = fpSetStationeryAttr (
  106.                     hfp,
  107.                     hStat,
  108.                     esiWatermark,
  109.                     esiaText,
  110.                     TEXT ("FPDK watermark"))) != 0) {
  111.         ShowError (TEXT ("fpSetStationeryAttr"), fpe);
  112.         goto Return;
  113.     }
  114.  
  115. // set the footer text
  116.     if ((fpe = fpSetStationeryAttr (
  117.                     hfp,
  118.                     hStat,
  119.                     esiFooter,
  120.                     esiaText,
  121.                     TEXT ("<Left>FPDK left<Center>FPDK center<Right>FPDK right"))) != 0) {
  122.         ShowError (TEXT ("fpSetStationeryAttr"), fpe);
  123.         goto Return;
  124.     }
  125.  
  126. // get the current watermark font
  127.     if ((cbAttr = fpGetStationeryAttr (
  128.                     hfp,
  129.                     hStat,
  130.                     esiWatermark,
  131.                     esiaFont,
  132.                     &lf,
  133.                     sizeof (lf))) != 0) {
  134.         // set the font to Times New Roman
  135.         _tcscpy (lf.lfFaceName, TEXT ("Times New Roman"));
  136.         if ((fpe = fpSetStationeryAttr (
  137.                         hfp,
  138.                         hStat,
  139.                         esiWatermark,
  140.                         esiaFont,
  141.                         &lf)) != 0) {
  142.             ShowError (TEXT ("fpSetStationeryAttr"), fpe);
  143.             goto Return;
  144.         }
  145.     }
  146.  
  147. // close the stationery
  148.     if ((fpe = fpCloseStationery (hfp, hStat)) != 0) {
  149.         ShowError (TEXT ("fpCloseStationery"), fpe);
  150.         goto Return;
  151.     }
  152.  
  153. // select the stationery
  154.     if ((fpe = fpSetLayoutAttr (hfp, eliStationery, pszStat)) != 0) {
  155.         ShowError (TEXT ("fpSetLayoutAttr"), fpe);
  156.         goto Return;
  157.     }
  158.  
  159. #if 0
  160. //
  161. // Set a callback.  To see the callback in action, just print a job to
  162. // the FinePrint Driver.
  163. //
  164. // WARNING: this callback stays in effect until either fpClearCallbackDll
  165. // is called, or the dispatcher exits.  See the FPDK documentation for
  166. // more details.
  167. //
  168.     DWORD    cEntryPoints;
  169.        TCHAR    sz2[256];
  170.  
  171.     _stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("samples\\msvc\\callback.mfc\\DebugA\\fpcallm.dll"));    // MFC callback
  172.     _stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("samples\\msvc\\callback\\DebugA\\fpcall.dll"));        // Win32 callback
  173.     _stprintf (sz2, TEXT ("Attempting to load callback DLL \"%s\""), sz);
  174.     MessageBox (NULL, sz2, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
  175.     if ((fpe = fpSetCallbackDll (hfp, sz, &cEntryPoints)) != 0) {
  176.         ShowError (TEXT ("fpSetCallbackDll"), fpe);
  177.         goto Return;
  178.     }
  179.     _stprintf (sz, TEXT ("%d callback entry point(s) installed."), cEntryPoints);
  180.     MessageBox (NULL, sz, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
  181. #endif
  182.  
  183. #if 0
  184. // print a file (assumes FinePrint Driver is the default Windows printer)
  185.     SHELLEXECUTEINFO    sei;
  186.  
  187.     ZeroInit (&sei);
  188.     sei.cbSize = sizeof (sei);
  189.     sei.fMask = SEE_MASK_NOCLOSEPROCESS;
  190.     sei.lpVerb = TEXT ("print");
  191.     sei.nShow = SW_SHOWNORMAL;
  192.     _stprintf (sz, TEXT ("%s\\%s"), szFpdk, TEXT ("doc\\fpdk.doc"));
  193.     sei.lpFile = sz;
  194.     if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
  195.         ShowError (TEXT ("fpGetJobCount"), fpe);
  196.         goto Return;
  197.     }
  198.     _stprintf (sz2, TEXT ("Attempting to print file \"%s\""), sei.lpFile);
  199.     MessageBox (NULL, sz2, TEXT ("Fpapi Sample Program"), MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
  200.     if (!ShellExecuteEx (&sei)) {
  201.         ShowError (TEXT ("ShellExecuteEx"), GetLastError ());
  202.         goto Return;
  203.     }
  204.  
  205. // wait maximum 1 minute for job to start, 10 minutes to complete
  206.     fpe = fpWaitForJob (hfp, &jcOrig, sei.hProcess, 60, 600, &js);
  207.     if (sei.hProcess != NULL)
  208.         CloseHandle (sei.hProcess);
  209.     if (fpe != 0) {
  210.         ShowError (TEXT ("fpWaitForJob"), fpe);
  211.         goto Return;
  212.     }
  213. #endif
  214.  
  215. #if 0
  216. // show the FinePrint dialog
  217.     DWORD    dwDlg;
  218.  
  219.     fpe = fpDisplayDialog (hfp, &dwDlg);
  220. #endif
  221.  
  222. #if 0
  223. // print the current jobs to the dest printer
  224.     fpe = fpPrintAllJobs (hfp, NULL, TRUE, TRUE);
  225. #endif
  226.  
  227. #if 1
  228. // print to the FinePrint driver
  229.     PRINTER_DEFAULTS    pd;
  230.     HANDLE                hPrinter;
  231.     int                    cbDevmode;
  232.     DEVMODE                *pDevmode;
  233.     HDC                    hdc;
  234.     DOCINFO                di;
  235.  
  236.     // turn off the FinePrint dialog
  237.     fpe = fpSetDeferAll (hfp, TRUE);
  238.     fpSetShowDlg (hfp, ShowDlg_Never);
  239.  
  240.     // get FinePrint's current DEVMODE settings
  241.     ZeroInit (&pd);
  242.     pd.DesiredAccess = PRINTER_ALL_ACCESS;
  243.     OpenPrinter (szFinePrinter, &hPrinter, &pd);
  244.     cbDevmode = DocumentProperties (
  245.                     NULL,
  246.                     hPrinter,
  247.                     szFinePrinter,
  248.                     NULL,
  249.                     NULL,
  250.                     0);
  251.     pDevmode = (DEVMODE *) new BYTE [cbDevmode];
  252.     memset (pDevmode, 0, cbDevmode);
  253.     DocumentProperties (
  254.         NULL,
  255.         hPrinter,
  256.         szFinePrinter,
  257.         pDevmode,
  258.         NULL,
  259.         DM_OUT_BUFFER);
  260.     ClosePrinter (hPrinter);
  261.  
  262.     // create a printer DC and print to it
  263.     hdc = CreateDC (NULL, szFinePrinter, NULL, pDevmode);
  264.     delete pDevmode;
  265.     ZeroInit (&di);
  266.     di.cbSize = sizeof (di);
  267.     di.lpszDocName = TEXT ("FPDK sample app");
  268.  
  269.     // get the job count before we start the new job
  270.     if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
  271.         ShowError (TEXT ("fpGetJobCount"), fpe);
  272.         goto Return;
  273.     }
  274.  
  275.     // print the job
  276.     StartDoc (hdc, &di);
  277.     StartPage (hdc);
  278.     TextOut (hdc, 0, 0, TEXT ("Job 1"), 5);
  279.     EndPage (hdc);
  280.     EndDoc (hdc);
  281.  
  282.     // wait for the job to complete
  283.     if ((fpe = fpWaitForJob (hfp, &jcOrig, NULL, 0, 60, &js)) != 0) {
  284.         ShowError (TEXT ("fpWaitForJob"), fpe);
  285.         goto Return;
  286.     }
  287.  
  288.     // get the job count before we start the new job
  289.     if ((fpe = fpGetJobCount (hfp, &jcOrig)) != 0) {
  290.         ShowError (TEXT ("fpGetJobCount"), fpe);
  291.         goto Return;
  292.     }
  293.  
  294.     // print the job
  295.     StartDoc (hdc, &di);
  296.     StartPage (hdc);
  297.     TextOut (hdc, 0, 0, TEXT ("Job 2"), 5);
  298.     EndPage (hdc);
  299.     EndDoc (hdc);
  300.  
  301.     // wait for the job to complete
  302.     if ((fpe = fpWaitForJob (hfp, &jcOrig, NULL, 0, 60, &js)) != 0) {
  303.         ShowError (TEXT ("fpWaitForJob"), fpe);
  304.         goto Return;
  305.     }
  306.  
  307.     // send jobs to the physical printer
  308.     fpPrintAllJobs (hfp, NULL, TRUE, TRUE);
  309.  
  310.     DeleteDC (hdc);
  311. #endif
  312.  
  313.     // success
  314.     MessageBeep (0);
  315.     MessageBox (
  316.         NULL,
  317.         TEXT ("All tests completed successfully!"),
  318.         TEXT ("Fpapi Sample Program"),
  319.         MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
  320.  
  321. Return:
  322.     if (hfp != 0) {
  323.         // close the FinePrint session (keeping the jobs)
  324.         fpClose (hfp, FALSE);
  325.     }
  326.     return (0);
  327. }
  328.  
  329. /*--------------------------------------------------------------------------
  330. |    ShowError
  331. --------------------------------------------------------------------------*/
  332. void ShowError (LPCTSTR pszFunc, FpError fpe)
  333. {
  334.     TCHAR    sz[256];
  335.  
  336.     wsprintf (
  337.         sz,
  338.         TEXT ("%s failed with error code %d."),
  339.         pszFunc,
  340.         fpe);
  341.     MessageBeep (0);
  342.     MessageBox (
  343.             NULL,
  344.             sz,
  345.             TEXT ("Fpapi Sample Program"),
  346.             MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
  347. }
  348.  
  349. /*--------------------------------------------------------------------------
  350. |    ShowError
  351. --------------------------------------------------------------------------*/
  352. void ShowError (LPCTSTR pszFunc, DWORD dwError)
  353. {
  354.     TCHAR    sz[256];
  355.  
  356.     wsprintf (
  357.         sz,
  358.         TEXT ("%s failed with error code %d."),
  359.         pszFunc,
  360.         dwError);
  361.     MessageBeep (0);
  362.     MessageBox (
  363.             NULL,
  364.             sz,
  365.             TEXT ("Fpapi Sample Program"),
  366.             MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
  367. }
  368.  
  369. /*--------------------------------------------------------------------------
  370. |    Warning
  371. |
  372. |        Displays a warning message.
  373. --------------------------------------------------------------------------*/
  374. void Warning (LPCTSTR pszFmt, ...)
  375. {
  376.     // expand the message string
  377.     va_list    args;
  378.     TCHAR    sz[512];
  379.  
  380.     va_start (args, pszFmt);
  381.     wvsprintf (sz, pszFmt, args);
  382.     va_end (args);
  383.     MessageBeep (0);
  384.     MessageBox (
  385.             NULL,
  386.             sz,
  387.             TEXT ("Fpapi Sample Program"),
  388.             MB_ICONINFORMATION | MB_OK | MB_SETFOREGROUND);
  389. }
  390.