Programming with the LEADTOOLS Virtual Printer Driver

Take the following steps to create and run a program that uses the LEADTOOLS Virtual Printer Driver.

1.

Start Visual Studio 2008.

2.

From the main menu, choose File->New->Project...

3.

In the New Project dialog box, choose Visual C++ in the Projects Types, and choose MFC Application in the Templates.

4.

Type the project name as LEADTOOLS Printer in the Project Name field.

5.

In the Location field, use the Browse button to navigate to the Examples subdirectory (such as C:\LEAD Technologies\LEADTOOLS 17\Examples\CDLL). Uncheck both Create directory for solution and Add to Source Control options. Click OK.

6.

Click Next> in the MFC Application Wizard.

7.

Select dialog based application type. Then click Finish. Three folders will be created, entitled: "Header Files", "Resource Files", and "Source Files".

8.

From the main menu, choose Project->Properties.

9.

In the Properties dialog box, choose Configuration Properties then select C/C++ then select Preprocessor.

10.

In right side set Preprocessor definitions property to use LTV17_CONFIG then click OK.

11.

In Solution Explorer, right-click on the Header Files folder, then Add->New Item. In Categories, choose Code. In the Templates, select Header File (.h). In the Name field type Ltprinter. Click Add. Inspect the newly created header file and make sure it is empty.

12.

Add to the Ltprinter.h in the Header Files the following code:

 

 #if !defined(LTPRINTER_H)

#define LTPRINTER_H

 

#if !defined(L_LTPRINTER_API)

   #define L_LTPRINTER_API

#endif // #if !defined(L_LTPRINTER_API)

 

#include "Lttyp.h"

#include "Lterr.h"

 

#define L_HEADER_ENTRY

#include "Ltpck.h"

 

/****************************************************************

   Callback typedefs

****************************************************************/

typedef L_INT (pEXT_CALLBACK PRNEMFRGSPROC)(L_WCHAR * pszPrinterName,

                                            HGLOBAL  hMem,     // Should be freed using the Windows GlobalFree function

                                            L_UINT   uSize,

                                            L_VOID * pData);

 

typedef L_INT (pEXT_CALLBACK PRNJOBINFOPROC)(L_WCHAR *  pszPrinterName,

                                             DWORD    dwJobID,

                                             DWORD    dwFlags,

                                             L_VOID * pData);

 

 

/****************************************************************

   Enums/defines/macros

****************************************************************/

 

#ifndef L_MAX_PATH

#define L_MAX_PATH 260

#endif

 

#define MAX_STRING          255

#define MAX_PRINTER_NAME    256

 

// Support Types for ePrint

#define PRN_SUPPORT_DOCUMENT  1 // DOCUMENT ....

#define PRN_SUPPORT_EVAL      2 // Evaluation Copy.

 

#define PRN_JOB_START         0x00000001L

#define PRN_JOB_END           0x00000002L

 

typedef struct _tagPrnPrinterInfoA

{

   L_UINT   uStructSize;        /* Size of the structure */

   L_CHAR  *pszPrinterName;     /* Printer Name  */

   L_CHAR  *pszDriverName;      /* Driver Name   */

   L_CHAR  *pszMonitorName;     /* Monitor Name  */

   L_CHAR  *pszPortName;        /* Port Name     */

   L_CHAR  *pszProductName;     /* Product Name  */

   L_CHAR  *pszRegistryKey;     /* Reg Subkey on HKCU\Software to store Printer Information*/

   L_CHAR  *pszRootDir;         /* Root Directory*/

   L_CHAR  *pszHelpFile;        /* Help File Name*/

   L_CHAR  *pszPassword;        /* password */

   L_CHAR  *pszUrl;             /* URL to redirect user when printer is locked */

   L_CHAR  *pszPrinterExe;      /* Printer Exe Path*/

} PRNPRINTERINFOA, * pPRNPRINTERINFOA;

 

#if defined(FOR_UNICODE)

typedef struct _tagPrnPrinterInfo

{

   L_UINT   uStructSize;         /* Size of the structure */

   L_TCHAR  *pszPrinterName;     /* Printer Name  */

   L_TCHAR  *pszDriverName;      /* Driver Name   */

   L_TCHAR  *pszMonitorName;     /* Monitor Name  */

   L_TCHAR  *pszPortName;        /* Port Name     */

   L_TCHAR  *pszProductName;     /* Product Name  */

   L_TCHAR  *pszRegistryKey;     /* Reg Subkey on HKCU\Software to store Printer Information*/

   L_TCHAR  *pszRootDir;         /* Root Directory*/

   L_TCHAR  *pszHelpFile;        /* Help File Name*/

   L_TCHAR  *pszPassword;        /* password */

   L_TCHAR  *pszUrl;             /* URL to redirect user when printer is locked */

   L_TCHAR  *pszPrinterExe;      /* Printer Exe Path*/

   L_TCHAR  *pszAboutString;     /* About String*/

   L_TCHAR  *pszAboutIcon;       /* About Icon Path*/

} PRNPRINTERINFO, * pPRNPRINTERINFO;

#else

typedef PRNPRINTERINFOA PRNPRINTERINFO;

typedef pPRNPRINTERINFOA pPRNPRINTERINFO;

#endif // #if defined(FOR_UNICODE)

 

 

typedef struct _tagPrnPrinterSpecifications

{

   L_UINT   uStructSize;

   L_UINT   uPaperID;            // If the paper is a predefined paper; then the IDs

                                 // range will be: 1 - 41.

                                 // If the paper is a custom paper; then the ID will

                                 // be: >= DMPAPER_USER + 200

   L_CHAR  szPaperSizeName[ MAX_STRING ];

   L_DOUBLE dPaperWidth;

   L_DOUBLE dPaperHeight;

   L_BOOL   bDimensionsInInches;

   L_BOOL   bPortraitOrient;

   L_CHAR  szMarginsPrinter[ MAX_PRINTER_NAME ];

   L_INT    nPrintQuality;

   L_INT    nYResolution;

} PRNPRINTERSPECIFICATIONS, * pPRNPRINTERSPECIFICATIONS;

 

/****************************************************************

   Function prototypes

****************************************************************/

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnLockPrinterA( L_CHAR * pszPrinterName, L_CHAR * pszPassword );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnlockPrinterA( L_CHAR * pszPrinterName, L_CHAR * pszPassword );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnIsPrinterLockedA( L_CHAR * pszPrinterName, L_BOOL * pbLocked );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnInstallPrinterA( PRNPRINTERINFOA * pPrnInfo, L_UINT32 uFlags);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUninstallPrinterA( PRNPRINTERINFOA  * pPrnInfo );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnRegisterEMFCallbackA( L_CHAR * pszPrinterName, PRNEMFRGSPROC fnEMFCallback, L_VOID * pData);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnRegisterJobCallbackA( L_CHAR * pszPrinterName, PRNJOBINFOPROC fnJobInfoCallBack, L_VOID * pData);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnRegisterEMFCallbackA( L_CHAR * pszPrinterName );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnRegisterJobCallbackA( L_CHAR * pszPrinterName );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnSetPrinterSpecificationsA( L_CHAR * pszPrinterName, PRNPRINTERSPECIFICATIONS * pSpecifications );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnGetPrinterSpecificationsA( L_CHAR * pszPrinterName, PRNPRINTERSPECIFICATIONS * pSpecifications );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnIsLeadtoolsPrinterA( L_CHAR  * pszPrinterName,L_BOOL* pbLeadtoolsPrinter);

 

#if defined(FOR_UNICODE)

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnLockPrinter( L_TCHAR * pszPrinterName, L_TCHAR * pszPassword );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnlockPrinter( L_TCHAR * pszPrinterName, L_TCHAR * pszPassword );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnIsPrinterLocked( L_TCHAR * pszPrinterName, L_BOOL * pbLocked );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnInstallPrinter( PRNPRINTERINFO * pPrnInfo, L_UINT32 uFlags);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUninstallPrinter( PRNPRINTERINFO  * pPrnInfo );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnRegisterEMFCallback( L_TCHAR * pszPrinterName, PRNEMFRGSPROC fnEMFCallback, L_VOID * pData);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnRegisterJobCallback( L_TCHAR * pszPrinterName, PRNJOBINFOPROC fnJobInfoCallBack, L_VOID * pData);

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnRegisterEMFCallback( L_TCHAR * pszPrinterName );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnUnRegisterJobCallback( L_TCHAR * pszPrinterName );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnSetPrinterSpecifications( L_TCHAR * pszPrinterName, PRNPRINTERSPECIFICATIONS * pSpecifications );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnGetPrinterSpecifications( L_TCHAR * pszPrinterName, PRNPRINTERSPECIFICATIONS * pSpecifications );

 

L_LTPRINTER_API L_INT EXT_FUNCTION L_PrnIsLeadtoolsPrinter( L_TCHAR  * pszPrinterName,L_BOOL* pbLeadtoolsPrinter);

#else

#define L_PrnLockPrinter                 L_PrnLockPrinterA

#define L_PrnUnlockPrinter               L_PrnUnlockPrinterA

#define L_PrnIsPrinterLocked             L_PrnIsPrinterLockedA

#define L_PrnInstallPrinter              L_PrnInstallPrinterA

#define L_PrnUninstallPrinter            L_PrnUninstallPrinterA

#define L_PrnRegisterEMFCallback         L_PrnRegisterEMFCallbackA

#define L_PrnRegisterJobCallback         L_PrnRegisterJobCallbackA

#define L_PrnUnRegisterEMFCallback       L_PrnUnRegisterEMFCallbackA

#define L_PrnUnRegisterJobCallback       L_PrnUnRegisterJobCallbackA

#define L_PrnSetPrinterSpecifications    L_PrnSetPrinterSpecificationsA

#define L_PrnGetPrinterSpecifications    L_PrnGetPrinterSpecificationsA

#define L_PrnIsLeadtoolsPrinter          L_PrnIsLeadtoolsPrinterA

#define L_PrnCancelPrintedJob          L_PrnCancelPrintedJobA #endif // #if defined(FOR_UNICODE)

 

 

#undef L_HEADER_ENTRY

#include "Ltpck.h"

 

#endif // #if !defined(LTPRINTER_H)

 

13.

In Solution Explorer, right-click on the Header Files folder, then Add->New Item. In Categories, select Code.

In the Templates, select Header File (.h).

In the Name field type stdAfx. Click Add.

Inspect the newly created header file and make sure it is empty.

Then add the following code:

 

#if !defined(AFX_STDAFX_H__FE71D496_8F54_4417_9E80_A39014A134CF__INCLUDED_)

#define AFX_STDAFX_H__FE71D496_8F54_4417_9E80_A39014A134CF__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

 

#include <afxwin.h>         // MFC core and standard components

#include <afxext.h>         // MFC extensions

#include <afxdisp.h>        // MFC Automation classes

#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include <afxcmn.h>         // MFC support for Windows Common Controls

#include <string.h>

#endif // _AFX_NO_AFXCMN_SUPPORT

#include <afxpriv.h>

 

#include "..\..\..\Include\L_Bitmap.h"

#include "..\..\..\include\ltDlg.h"

 

#include "..\..\..\Include\Ltprinter.h"

#include "..\..\..\Include\ltDocWrt.h"

 

#include "..\Common\ltdemos.h"

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_STDAFX_H__FE71D496_8F54_4417_9E80_A39014A134CF__INCLUDED_)

 

14.

In the Source Files, right -click on the folder. Choose Add->New Item. Choose Code in Categories, and C++ File(.cpp) in Templates. Type Imports in the name field and click Add.

15.

Right click on Imports.cpp and click Open. Add the following code:

 

 

#include "stdafx.h"

 

#if defined(WIN64)

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltkrn_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltprinter_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\LtDocWrt_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdlgfile_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdlgkrn_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltfil_x.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdis_x.lib")

#else

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltkrn_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltprinter_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\LtDocWrt_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdlgfile_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdlgkrn_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltfil_u.lib")

   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdis_u.lib")

#endif  // #if defined(WIN64)

 

16.

Right-click on LEADTOOLS PrinterDlg.h and click Open. Add to it the following code:

 

 

// LEADTOOLS PrinterDlg.h :

//

///////////////////////////////////////////////////////////////////////

#if !defined(AFX_LeadtoolsPrinter_H__4484BA1F_608A_42B7_82D2_9921D8DAE966__INCLUDED_)

#define AFX_LeadtoolsPrinter_H__4484BA1F_608A_42B7_82D2_9921D8DAE966__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

class CLEADTOOLSPrinterDlg : public CDialog

{

public: // create from serialization only

CLEADTOOLSPrinterDlg (CWnd* pParent = NULL);

// Dialog Data

enum { IDD = IDD_LEADTOOLSPRINTER_DIALOG };

   protected:

   virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

   L_VOID DrawStartedPage(CDC* pDC , RECT * pClientRect);

   L_VOID RegisterJobCallBack();

   L_VOID RegisterEmfCallBack();

   L_INT InstallPrinter();

   L_INT PrintCurrentPage();

   L_VOID LockPrinter();

   L_VOID UnlockPrinter();

static L_INT (EXT_CALLBACK OnEmfRgsProc)( L_WCHAR*, HGLOBAL, L_UINT, L_VOID* );

static L_INT (EXT_CALLBACK OnJobInfoProc)( L_WCHAR*, DWORD, DWORD, L_VOID* );

// Implementation

protected:

HICON m_hIcon;

// Generated message map functions

virtual BOOL OnInitDialog();

afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

afx_msg void OnPaint();

afx_msg HCURSOR OnQueryDragIcon();

DECLARE_MESSAGE_MAP()

public:

   afx_msg void OnBnClickedOk();

};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_LeadtoolsPrinter_H__4484BA1F_608A_42B7_82D2_9921D8DAE966__INCLUDED_)

 

17.

Right-click on LEADTOOLS PrinterDlg.cpp and click Open. Add to it the following code:

 

// LEADTOOLS PrinterDlg.cpp : implementation file

//

#include "stdafx.h"

#include "LEADTOOLS Printer.h"

#include "LEADTOOLS PrinterDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog

{

public:

CAboutDlg();

// Dialog Data

enum { IDD = IDD_ABOUTBOX };

protected:

virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation

protected:

DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

END_MESSAGE_MAP()

// CLEADTOOLSPrinterDlg dialog

CLEADTOOLSPrinterDlg:: CLEADTOOLSPrinterDlg (CWnd* pParent /*=NULL*/)

: CDialog(CLEADTOOLSPrinterDlg::IDD, pParent)

{

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CLEADTOOLSPrinterDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

}

BEGIN_MESSAGE_MAP(CLEADTOOLSPrinterDlg, CDialog)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

//}}AFX_MSG_MAP

   ON_BN_CLICKED(IDOK, & CLEADTOOLSPrinterDlg::OnBnClickedOk)

END_MESSAGE_MAP()

// CLEADTOOLSPrinterDlg message handlers

BOOL CLEADTOOLSPrinterDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);

if (pSysMenu != NULL)

{

CString strAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if (!strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

}

}

// Set the icon for this dialog.  The framework does this automatically

//  when the application's main window is not a dialog

SetIcon(m_hIcon, TRUE); // Set big icon

SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

   UNLOCKSUPPORT();

   if(L_IsSupportLocked(L_SUPPORT_PRINT_DRIVER))

   {

      MessageBox(TEXT("Printer driver capability is required."), TEXT("LEADTOOLS Printer Demo"), MB_OK | MB_ICONERROR);

   }

return TRUE;  // return TRUE  unless you set the focus to a control

}

void CLEADTOOLSPrinterDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

if ((nID & 0xFFF0) == IDM_ABOUTBOX)

{

CAboutDlg dlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog::OnSysCommand(nID, lParam);

}

}

// If you add a minimize button to your dialog, you will need the code below

//  to draw the icon.  For MFC applications using the document/view model,

//  this is automatically done for you by the framework.

void CLEADTOOLSPrinterDlg::OnPaint()

{

if (IsIconic())

{

CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle

int cxIcon = GetSystemMetrics(SM_CXICON);

int cyIcon = GetSystemMetrics(SM_CYICON);

CRect rect;

GetClientRect(&rect);

int x = (rect.Width() - cxIcon + 1) / 2;

int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon

dc.DrawIcon(x, y, m_hIcon);

}

else

{

CDialog::OnPaint();

}

}

// The system calls this function to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR CLEADTOOLSPrinterDlg::OnQueryDragIcon()

{

return static_cast<HCURSOR>(m_hIcon);

}

void CLEADTOOLSPrinterDlg::OnBnClickedOk()

{

   InstallPrinter();

   RegisterEmfCallBack();

   RegisterJobCallBack();

}

L_INT CLEADTOOLSPrinterDlg:: InstallPrinter ()

{

PRNPRINTERINFO docPrinterInfo;

 memset(&docPrinterInfo, 0, sizeof(PRNPRINTERINFO));

 docPrinterInfo.uStructSize = sizeof(PRNPRINTERINFO);

 docPrinterInfo.pszRootDir      =  

TEXT("C:\\Program Files\\PrinterDriver");

 docPrinterInfo.pszPrinterName  =  TEXT("TestPrinter");

 docPrinterInfo.pszDriverName   =  TEXT("TestPrinter");

 docPrinterInfo.pszProductName  =  TEXT("TestPrinter");

 docPrinterInfo.pszRegistryKey  =  TEXT("SOFTWARE\\Printer\\TestPrinter");

 docPrinterInfo.pszUrl          =  TEXT("http://www.LEADTOOLS.com");

 docPrinterInfo.pszPassword     =  TEXT("123");

 docPrinterInfo.pszPrinterExe   =  TEXT("c:\\TestDemo.exe");

 docPrinterInfo.pszAboutString  =  TEXT("LEADTOOLSPriner");

 docPrinterInfo.pszAboutIcon    =  TEXT("c:\\LEADTOOLSPriner.ico");

 L_INT nRet=L_PrnInstallPrinter(&docPrinterInfo, 0);

 return nRet;

}

void CLEADTOOLSPrinterDlg::RegisterEmfCallBack()

{

   L_PrnRegisterEMFCallback(TEXT("TestPrinter"),OnEmfRgsProc,this);

}

void CLEADTOOLSPrinterDlg::RegisterJobCallBack()

{

   L_PrnRegisterJobCallback(TEXT("TestPrinter"),OnJobInfoProc,this);

}

L_INT CLEADTOOLSPrinterDlg::OnEmfRgsProc( L_WCHAR * /*pszPrinter*/,

                                      HGLOBAL hMem,

                                      L_UINT  uSize,

                                      L_VOID * pData )

{

   static L_INT nCounter = 0;

   HANDLE hFile = INVALID_HANDLE_VALUE;

   DWORD     uSizeWritten = 0;

   L_UCHAR * pEmfData= (L_UCHAR*) GlobalLock( hMem );

   

   CString szFileName = TEXT("c:\\1.emf");

   if( pEmfData )

   {  

      hFile = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ,  

                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);

      if( hFile != INVALID_HANDLE_VALUE )

      {

         WriteFile( hFile, pEmfData, uSize, & uSizeWritten, NULL );

         CloseHandle( hFile );

      }

      GlobalUnlock( hMem );

   }

   // Don't forget freeing the mem.

   GlobalFree( hMem );

   return 1;

}

// You need always to free hMem.

L_INT  CLEADTOOLSPrinterDlg::OnJobInfoProc( L_WCHAR * pszPrinter, DWORD dwJobID,

                                        DWORD dwFlags, L_VOID * pData )

{

   switch( dwFlags )

   {

   case PRN_JOB_START:

      //Job was started

      break;

   case PRN_JOB_END:

      //Job was ended

      break;

   }

   

   return 1;

}

L_INT  CLEADTOOLSPrinterDlg::PrintCurrentPage()

{

   HDC hDC = CreateDC(NULL, TEXT("TestPrinter"),NULL,NULL);

   

   if(hDC)

   {

      int nRet = 0;

      DOCINFO docInfo;

      memset(&docInfo,0,sizeof(DOCINFO));

      nRet = StartDoc(hDC,&docInfo);

      if(nRet)

      {

         nRet = StartPage(hDC);

         if(nRet)

         {

            RECT rcPaper;

            rcPaper.left = 0;

            rcPaper.top = 0;

            rcPaper.right = GetDeviceCaps(hDC, HORZRES);

            rcPaper.bottom = GetDeviceCaps(hDC, VERTRES);

            CDC  cd;

            cd.Attach(hDC);

            DrawStartedPage(&cd, &rcPaper);

            cd.Detach();

            nRet = EndPage(hDC);

         }

         nRet = EndDoc(hDC);

      }

      DeleteDC(hDC);

   }

return 1;

}

void CLEADTOOLSPrinterDlg::DrawStartedPage(CDC * pDC, RECT * pClientRect)

{

   LOGFONT lf;

   memset(&lf, 0, sizeof(LOGFONT));

   HFONT hOldFont = NULL;

   HFONT hFont = NULL;

   lf.lfHeight = - MulDiv( 12, pDC->GetDeviceCaps( LOGPIXELSY ), 72);

   lstrcpy(lf.lfFaceName, TEXT("Times New Roman") );

   

   hFont = CreateFontIndirect( & lf );

   hOldFont = (HFONT )pDC->SelectObject( hFont );

   pDC->SetTextColor( RGB( 0, 0, 255 ) );

   CString strPageText= TEXT("\n   LEADTOOLS Printer Main Demo \

                      \n   ------------------------------------------------------------ \

                      \n   This demo program demonstrates the usage of the LEADTOOLS Printer functionality. \

                      \n   ------------------------------------------------------------ \

                      \n \

                      \n   You can use this demo in one of two ways: \

                      \n \

                      \n   Printing from other applications: \

                      \n   1 - Print from any application to the currently selected printer (see the demo caption for the currently selected printer). \

                      \n   2 - From the main menu, select \"File | Save As\", then save the printed document as PDF, DOC, etc, or as a supported raster format. \

                      \n        You can repeat the save as many times as necessary. \

                      \n \

                      \n   Printing from this application: \

                      \n   1 - From the main menu, select \"File | Print Current Page\", to print the contents of this page. \

                      \n   2 - From the main menu, select \"File | Save As\", then save the printed document as PDF, DOC, etc, or as a supported raster format. \

                      \n        You can repeat the save as many times as necessary. ");

   

   pDC->DrawText( strPageText, strPageText.GetLength(), pClientRect, DT_LEFT);

   pDC->SelectObject( hOldFont );

   DeleteObject( hFont );

}

void CLEADTOOLSPrinterDlg::LockPrinter()

{

   int nRet=0;

  

   nRet=L_PrnLockPrinter(TEXT("TestPrinter"),TEXT("1234"));

   if(nRet!=SUCCESS)

   {

      MessageBox(TEXT("Locking printer failed"), TEXT("Error"), MB_OK );

   }

   else

   {

      MessageBox(TEXT("Printer is locked.  You will no longer be able to print to      it."), TEXT("LEADTOOLS Printer"), MB_OK );

   }

}

void CLEADTOOLSPrinterDlg::UnlockPrinter()

{

   L_INT nRet = L_PrnUnlockPrinter(TEXT("TestPrinter"),TEXT("1234"));

   if (nRet == SUCCESS)

      MessageBox(TEXT("Printer is unlocked."), TEXT("LEADTOOLS Printer"), MB_OK );

   else

      MessageBox(TEXT("Unlocking printer failed."), TEXT("Error"), MB_OK );

}

 

18.

Compile and run the code to test it, Click OK button in the dialog to install new printer then try to print on created printer to receive EMF and Job callback events.

 

For more information, refer to:

Working with the LEADTOOLS Virtual Printer

See also:

Tutorials