Printing a Single-Page Image with LEADTOOLS

Take the following steps to create and run a program that prints single-page document images. This tutorial does not cover printing multipage or PDF formats, but with some modifications you can accomplish this using additional LEADTOOLS supporting libraries.

NOTE: A highlighted row denotes that there is at least one instance of a LEADTOOLS object.

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 Win32 Project in the Templates.

4.

Type the project name as Image Printing 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. Choose OK.

6.

Choose Next> in the Win32 Application Wizard.

7.

Choose Windows application and Empty project and do not add common header files for ATL. Then choose Finish. Three empty folders will be created titled: Header Files, Resource Files, and Source Files.

 

Folders Content: Inside Visual Studio 2008 in the Solution Explorer review the folders. They must be empty. If there are any objects or files in them, delete this project and restart from the first step.

 

Project Structure: Once the project is acceptable, in Windows Explorer go to the newly created project ImagePrinting folder in the Examples directory. Make sure the project folder named ImagePrinting exists in the correct directory. Inside the ImagePrinting folder, there should not be any additional folders. If there are, remove the entire project folder and redo all previous steps.

8.

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 Name field type ImagePrinting. Choose Add. Inspect the newly created header file and make sure it is empty.

9.

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

 

#define IDM_OPEN  100
#define IDM_PRINT 200
#define IDM_EXIT  300
#define IDI_MAIN  400
#define GWLP_CHILDDATA 0

extern L_INT ShowPrintPreviewDialog ( HWND hWnd, pBITMAPHANDLE pBitmap, LPPRINTPREVIEWDLGPARAMS pDlgParams ) ; 
extern PRINTPREVIEWDLGPARAMS            PrintPreviewParams ; 

typedef struct tagDATA
{
   HPALETTE hPalette;              /* Logical Palette used for painting. */
   L_TCHAR szFilename[L_MAXPATH];  /* File name to load. */
   L_TCHAR szImageDir[L_MAXPATH];  /* Hold Image Directory path. */

   FILEINFO FileInfo;              /* FILEINFO structure. */

   BITMAPHANDLE BitmapHandle;      /* BITMAP HANDLE to hold image. */

} DATA; 

DATA Data;                         /* Pointer to DATA structure. */
PRINTPREVIEWDLGPARAMS PrintPreviewParams;
L_BOOL InitApplication (HINSTANCE hInstance);
L_BOOL InitInstance (HINSTANCE hInstance, L_INT nCmdShow);
 
LRESULT CALLBACK MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam); 
L_INT Get_OpenFile (HWND hWnd);
 
BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct); 
BOOL Window_OnQueryNewPalette (HWND hWnd); 
VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify); 
VOID Window_OnPaletteChanged (HWND hWnd, HWND hWndPaletteChange); 
VOID Window_OnPaint (HWND hWnd); 
VOID Window_OnDestroy (HWND hWnd); 
VOID Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized); 
VOID Window_OnPaletteChanging(HWND hWnd, HWND hWndPaletteChange); 
VOID Window_SysColorChange(HWND hwnd);

10.

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 Name field type stdAfx. Choose Add. Inspect the newly created header file and make sure it is empty. Then add the following code:

 

#if !defined(INC_STDAFX_H) 
#define INC_STDAFX_H
#include <windows.h> /* Required for all Windows applications. */
#include <windowsx.h>/* Needed for message crackers.           */
#include <tchar.h>
#include <commdlg.h> /* Windows' header for common dialog box. */
#include <string.h>  /* Standard C header files. */
#include <ctype.h>

#include "..\..\..\include\l_bitmap.h"

#include "..\..\..\include\ltdlg.h"

#include "..\Common\LtDemos.h"

#include "..\..\..\include\ltdis.h"


#endif // !defined(INC_STDAFX_H) 

11.

In Solution Explorer, right click on the Resource Files folder, then Add->New Item. In Categories, choose Resource. In the Templates, select Resource File(.rc). In Name field type ImagePrinting. Choose Add.

12.

Right click on ImagePrinting.rc in the Resource Files, choose View Code. Select All text and delete its content. To this same file add the following code:

 

#include "Windows.h"
#include "ImagePrinting.h"
MENU_MAIN MENU DISCARDABLE 
BEGIN
   MENUITEM "&Open...",            IDM_OPEN
   MENUITEM "&Print...",           IDM_PRINT 
   MENUITEM "&Exit"                IDM_EXIT
END
LEAD ICON "..\\..\\Resources\\lvsample.ico"

13.

In the Header Files, click on the resource.h header file and delete from the project.

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 choose Add.

15.

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

 

#include "StdAfx.h"
#if defined(WIN64) 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltkrn_x.lib")
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltdis_x.lib")

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

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

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

   //#pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltdoc_x.lib")


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

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

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

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

#endif // #if defined(WIN64)

 

16.

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

17.

Right click on ImagePrinting.cpp and choose Open. Add to it the following code:

 

/*-------------------------------------------------------------------------*/
#include "stdafx.h"
#include "ImagePrinting.h"           /* Application specific header file. */

/*---[WinMain]--------------------------------------------------------------- 
  Syntax:     int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                                                        LPSTR lpCmdLine, int nCmdShow ) 

  Parameters: hInstance       Current instance. 
              hPrevInstance   previous instance. 
              lpCmdLine       command line. 
              nCmdShow        show-window type. 

  Prototype:  Windows.h

  Notes:      Windows main function, calls initialization function
              and processes message loop. 
  --------------------------------------------------------------------------*/
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
   UNREFERENCED_PARAMETER(lpCmdLine); 
   MSG msg; 
   /* Message structure, See Windows SDK for more information. */
   UNLOCKSUPPORT ();
   if (!hPrevInstance)                       /* Other instances of app running? */
      if (!InitApplication (hInstance))      /* Initialize shared things.       */
         return (FALSE);                     /* Exits if unable to initialize.  */
   if (!InitInstance (hInstance, nCmdShow))  /* Do instance initializations. */
      return (FALSE); 
   L_DlgInit(DLG_INIT_COLOR); 
   /* Acquire and dispatch messages until a WM_QUIT message is received. */
   while (GetMessage (&msg, NULL, 0, 0)) 
   {
      TranslateMessage (&msg);               /* Translates virtual key codes.  */
      DispatchMessage (&msg);                /* Dispatches message to window.  */
   }
   return (int)msg.wParam;                  /* Returns the value from PostQuitMessage. */
}

/*---[InitApplication]------------------------------------------------------ 
  Syntax:     L_BOOL InitApplication( HANDLE hInstance ) 
  Parameters: hInstance    Current instance. 
  Prototype:  Loadsave.h
  Notes:      Initializes window class structure and registers window class. 
  --------------------------------------------------------------------------*/

L_BOOL InitApplication (HINSTANCE hInstance) 
{
   WNDCLASS wcWindowClass; 
   wcWindowClass.style = 0;                  /* Class style(s).               */
   wcWindowClass.lpfnWndProc = MainWndProc;  /* Function to retrieve messages */
                                             /* for windows of this class.    */
   wcWindowClass.cbClsExtra = 0;             /* No per-class extra data.      */
   wcWindowClass.cbWndExtra = 0;             /* No per-window extra data.     */
   wcWindowClass.hInstance = hInstance;      /* Owner.                        */
   wcWindowClass.hIcon = LoadIcon (hInstance, TEXT("LEAD"));
   wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); 
   wcWindowClass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH); 
   wcWindowClass.lpszMenuName = TEXT("MENU_MAIN");    /* Menu Name. */
   wcWindowClass.lpszClassName = TEXT("LEADWClass");  /* Class Name. */

   /* Register the window class and return the result code. */
   return (RegisterClass (&wcWindowClass)); 
}

/*---[InitInstance]---------------------------------------------------------- 
  Syntax:     L_BOOL InitInstance( HINSTANCE hInstance, L_INT nCmdShow ) 
   Parameters: hInstance    Current instance. 
              nCmdShow     Param for first ShowWindow() call. 
  Prototype:  Loadsave.h
  Notes:      Saves instance handle and creates main window. 
  --------------------------------------------------------------------------*/

L_BOOL InitInstance (HINSTANCE hInstance, L_INT nCmdShow)
{
   Demos_GetImagesDirectory(Data.szImageDir, _countof(Data.szImageDir)); 

   HWND hWnd;                   /* Window handle. */
   /* Create a main window for this application instance. */
   hWnd = CreateWindow (
                        TEXT("LEADWClass"),
                        TEXT("LEADTOOLS Printing Tutorial"),  /* Window title */
                        WS_OVERLAPPEDWINDOW, /* Window style. */
                        CW_USEDEFAULT, /* Default horizontal position. */
                        CW_USEDEFAULT, /* Default vertical position. */
                        CW_USEDEFAULT, /* Default width.*/
                        CW_USEDEFAULT, /* Default height.*/
                        NULL,   /* Overlapped windows have no parent. */
                        NULL,   /* Use the window class menu.    */
                        hInstance,  /* This instance owns this window. */
                        NULL    /* Pointer not needed.           */
      );

   if (hWnd == NULL)
      return (FALSE);            /* If window could not be created, return "failure". */
   ShowWindow (hWnd, nCmdShow);  /* Show the window.       */
   UpdateWindow (hWnd);          /* Send WM_PAINT message. */

   return (TRUE);
}
 
/*---[MainWndProc]-----------------------------------------------------------
  Syntax:     LRESULT CALLBACK MainWndProc( HWND hWnd, L_UINT message,
                                                WPARAM wParam, LPARAM lParam )
  Parameters: hWnd            Window handle.
              message         Type of message.
              wParam          Additional information.
              lParam          Additional information.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling window messages.
  --------------------------------------------------------------------------*/

LRESULT CALLBACK MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) 
{
   switch (Message)
   {
      HANDLE_MSG (hWnd, WM_CREATE, Window_OnCreate);
      HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand);
      HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged);
      HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette);
      HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint);
      HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy);
      HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate);
      HANDLE_MSG (hWnd, WM_PALETTEISCHANGING, Window_OnPaletteChanging);
      HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE, Window_SysColorChange);
   }
   return DefWindowProc (hWnd, Message, wParam, lParam);
}

/*---[Window_OnCreate]-----------------------------------------------------
  Syntax:     BOOL Window_OnCreate(HWND hWnd, CREATESTRUCT FAR* lpCreateStruct);
  Parameters: hWnd            Window handle.
              lpCreateStruct  Windows Create Structure.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_CREATE.
  --------------------------------------------------------------------------*/
BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct)
{
   HMENU hMenu;
   UNREFERENCED_PARAMETER (hWnd);
   UNREFERENCED_PARAMETER (lpCreateStruct);

   hMenu = GetMenu (hWnd);
   //ENABLEMENUITEM (hMenu, IDM_SAVE, Data.BitmapHandle.Flags.Allocated);
   Data.hPalette = NULL;
   /* Set Ordered Dithering for the Display */
   L_SetDisplayMode (DISPLAYMODE_ORDEREDDITHER, DISPLAYMODE_ORDEREDDITHER); 

   return (TRUE);
}

/*---[Window_OnCommand]-----------------------------------------------------

  Syntax:     VOID Window_OnCommand( HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify ) 
  Parameters: hWnd            Window handle.

              id              Menu item or Control ID.
              hwndCtl         0 if menu item selected, else window handle of
                              the control.
              codeNotify      1 if accelerator keystroke, else notification
                              code, such as BN_CLICKED.
  Prototype:  Color.h
  Notes:      This procedure is responsible for handling WM_ONCOMMAND.
  --------------------------------------------------------------------------*/
VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify)
{
   L_INT nRet; 
   L_TCHAR buf[1024];              /* Buffer to hold the error message. */

   UNREFERENCED_PARAMETER (hwndCtl);
   UNREFERENCED_PARAMETER (codeNotify);

   switch (id)
   {
     case IDM_OPEN:
      nRet = Get_OpenFile (hWnd); 

      if (nRet == 2) 
         MessageBox (0, 0, TEXT("NO FILE LOADED"), MB_OK); 
      /* No file was selected for opening, so do
                                   nothing. */
      else if (nRet == SUCCESS) 
         FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage);
      else
      {
         wsprintf (buf, TEXT("Error %d Loading %s"), nRet,
                   (LPTSTR) Data.szFilename);
         MessageBox (hWnd, buf, TEXT("Error"), MB_OK);
      }
      break;

      case IDM_PRINT:
         
         /*If file exists then print it*/
         nRet = ShowPrintPreviewDialog (hWnd, &Data.BitmapHandle, &PrintPreviewParams ); 
         
         /*Error message no image to be printed*/
         if(nRet<0)
         {
            MessageBox (0, 0, TEXT("No Image To Print"), MB_OK);
         }
              
      break;
   case IDM_EXIT:
      DestroyWindow (hWnd);
      break;
   }
   return;
}

/*----(Window_OnPaletteChanged)--------------------------------------------

  Syntax:     VOID Window_OnPaletteChanged( HWND hWnd, HWND hWndPaletteChange )
  Parameters: hwnd                 Handle to a window.
              hWndPaletteChange    Handle to a window that has the palette
                                   realized.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_PALETTECHANGED.
--------------------------------------------------------------------------*/
VOID Window_OnPaletteChanged (HWND hWnd, HWND hWndPaletteChange)
{
   HDC hDC;
   HPALETTE hPalette;
   /* If this window initiated the palette change, do nothing. */
   if (hWnd == hWndPaletteChange)
      return;

   /* Does this window have a bitmap and a palette? */
   if (Data.BitmapHandle.Flags.Allocated) 
   {
      hDC = GetDC (hWnd);

      /* Select and Realize the palette. */
      hPalette = SelectPalette (hDC, Data.hPalette, TRUE);
      RealizePalette (hDC);

      /* Force a repaint. */
      InvalidateRect (hWnd, NULL, FALSE);

      /* Return the old palette. */
      SelectPalette (hDC, hPalette, TRUE);

      ReleaseDC (hWnd, hDC);
   }
   return;
}

/*---[Window_OnActivate]-----------------------------------------------------

  Syntax:     VOID  Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, 
                                      BOOL fMinimized)
  Parameters: hWnd            Window handle.
              state           WA_ACTIVE | WA_CLICKACTIVE | WA_INACTIVE    
              hwndActDeact    the winodw habdle that deactivate
              fMinimized      Window is minimized
  Prototype:  Color.h
  Notes:      This procedure is responsible for handling WM_ACTIVATE.
  --------------------------------------------------------------------------*/
VOID Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized)
{
   UNREFERENCED_PARAMETER(hwndActDeact);
   UNREFERENCED_PARAMETER(fMinimized);

   if(state!=WA_INACTIVE)
      Window_OnQueryNewPalette (hwnd);
}

/*====(Window_OnPaletteChanging)==========================================
Description: Enumerates all child windows and asks them to realize their
             logical palettes beside the physical palette.
Syntax     : VOID Window_OnPaletteChanging(HWND hwnd, HWND hWndPaletteChange)
Parameters : hwnd                 Handle to a window.
             hWndPaletteChange    Handle to a window that has the palette
                                  realized.
Return Value: None.
==========================================================================*/
VOID  Window_OnPaletteChanging(HWND hWnd, HWND hWndPaletteChange)
{
   Window_OnPaletteChanged (hWnd, hWndPaletteChange);
}

/*====(Window_SysColorChange)==========================================
  Syntax:     VOID Window_SysColorChange( HWND hWnd )
  Parameters: hWnd          Handle to a window.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_SYSCOLORCHANGE.
--------------------------------------------------------------------------*/
VOID Window_SysColorChange(HWND hwnd)
{
   Window_OnQueryNewPalette (hwnd);
}

/*----(Window_OnQueryNewPalette)--------------------------------------------
  Syntax:     BOOL Window_OnQueryNewPalette( HWND hWnd )
  Parameters: hWnd          Handle to a window.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_QUERYNEWPALETTE.
--------------------------------------------------------------------------*/

BOOL Window_OnQueryNewPalette (HWND hWnd)
{
   HDC hDC;
   HPALETTE hPalette;
   L_INT nNoColors = 0; 
   /* Delete the previous palette, if there is one. */
   if (Data.hPalette)
   {
      DeleteObject (Data.hPalette);
      Data.hPalette = NULL;
   }
   if (Data.BitmapHandle.Flags.Allocated) 
   {
      hDC = GetDC (hWnd);

      /* Generate a new logical palette (if needed) for painting. */
      Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); 

      if (Data.hPalette)        /* Is a palette needed? */
      {
         hPalette = SelectPalette (hDC, Data.hPalette, FALSE);
         nNoColors = RealizePalette (hDC);

         if (nNoColors)         /* If the palette changed, force a WM_PAINT */
            InvalidateRect (hWnd, NULL, FALSE);

         /* Restore the old palette. */
         SelectPalette (hDC, hPalette, TRUE);
      }
      ReleaseDC (hWnd, hDC);
   }
   return (nNoColors);
}
/*---[Window_OnPaint]-------------------------------------------------------

  Syntax:     VOID Window_OnPaint( HWND hWnd );
  Parameters: hWnd            Window handle.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_PAINT.
  --------------------------------------------------------------------------*/
VOID Window_OnPaint (HWND hWnd)
{
   HDC hdc;
   RECT rc;
   PAINTSTRUCT ps;
   HPALETTE hOldPal = NULL;
   hdc = BeginPaint (hWnd, &ps);/* Get DC */

   if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */
   {
      if (Data.hPalette)        /* Is there a palette that needs to be
                                   selected? */
         hOldPal = SelectPalette (hdc, Data.hPalette, TRUE);  /* Select it. */

      /* Setup the Destination Rectangle. */
      SetRect (&rc, 0, 0, BITMAPWIDTH(&Data.BitmapHandle), BITMAPHEIGHT(&Data.BitmapHandle)); 

      /* Paint it */
      L_PaintDC (hdc, 
                 &Data.BitmapHandle, 

                 NULL,
                 NULL,
                 &rc,
                 &ps.rcPaint,
                 SRCCOPY);

      if (Data.hPalette)        /* Return old palette if there is one. */
         SelectPalette (hdc, hOldPal, TRUE);
   }
   EndPaint (hWnd, &ps);        /* Return DC */
   return;
}

/*---[Window_OnDestroy]----------------------------------------------------
  Syntax:     VOID Window_OnDestroy( HWND hWnd );
  Parameters: hWnd            Window handle.
  Prototype:  Loadsave.h
  Notes:      This procedure is responsible for handling WM_DESTROY.
  --------------------------------------------------------------------------*/

VOID Window_OnDestroy (HWND hWnd)
{
   UNREFERENCED_PARAMETER (hWnd);
   if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */
      L_FreeBitmap (&Data.BitmapHandle); 

   if (Data.hPalette)           /* Delete palette if there is one. */
      DeleteObject (Data.hPalette);
   L_DlgFree();
   PostQuitMessage (0);         /* Post WM_QUIT, to end the application. */
   return;
}

L_VOID CleanOpenDlgParam(LPOPENDLGPARAMS pFOParam) 
{
   int i=0;
   if(pFOParam!=NULL && pFOParam->pFileData!= NULL ) 
   {
      for(i=0; i < pFOParam->nNumOfFiles; ++i ) 
      {
         if(pFOParam->pFileData[i].pBitmap != NULL) 
         {
            L_FreeBitmap(pFOParam->pFileData[i].pBitmap); 
            GlobalFreePtr(pFOParam->pFileData[i].pBitmap); 

         }
         if(pFOParam->pFileData[i].pThumbnail != NULL) 
         {
            L_FreeBitmap(pFOParam->pFileData[i].pThumbnail); 
            GlobalFreePtr(pFOParam->pFileData[i].pThumbnail); 
         }
         if(pFOParam->pFileData[i].pFileInfo != NULL) 
         {
            GlobalFreePtr(pFOParam->pFileData[i].pFileInfo); 
         }
      }

      GlobalFreePtr(pFOParam->pFileData); 
      pFOParam->pFileData    =NULL; 
      pFOParam->nNumOfFiles  = 0; 

   }
}

L_INT Get_OpenFile (HWND hWnd) 
{
   static  OPENDLGPARAMS FOParm; 
   static  OPENFILENAME OpenFileName;
   static  LOADFILEOPTION LoadFileOption; 

   HMENU   hMenu;
   HCURSOR hCursor;
   L_INT   nRet; 
   _fmemset(&FOParm, 0, sizeof(OPENDLGPARAMS));
   _fmemset(&OpenFileName, 0, sizeof(OPENFILENAME));
   FOParm.uStructSize             = sizeof(OPENDLGPARAMS); 
   FOParm.bShowLoadOptions        = FALSE ; 
   FOParm.bPreviewEnabled         = TRUE; 
   FOParm.uDlgFlags               = DLG_OPEN_LOADBITMAP |

                                    DLG_OPEN_SHOW_PREVIEW |
                                    DLG_OPEN_SHOW_FILEINFO;
   OpenFileName.lStructSize       = sizeof(OPENFILENAME);
   OpenFileName.hwndOwner         = hWnd;
   OpenFileName.lpstrFilter       = NULL;
   OpenFileName.lpstrCustomFilter = NULL;
   OpenFileName.nMaxCustFilter    = 0;
   OpenFileName.nFilterIndex      = 1;
   OpenFileName.lpstrInitialDir   = Data.szImageDir;
   OpenFileName.lpstrTitle        = TEXT("Open File");
   OpenFileName.lpstrDefExt       = NULL;
   OpenFileName.Flags             = 0;
   L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); 
   LoadFileOption.Flags |= ELO_ROTATED; 
   nRet = L_DlgOpen( hWnd, &OpenFileName, &FOParm); 
   if(nRet == SUCCESS_DLG_OK) 

   {
      nRet=FAILURE; 

      if ( ( FOParm.pFileData != NULL ) && ( FOParm.nNumOfFiles > 0 ) ) 
      {
         lstrcpy (Data.szFilename, FOParm.pFileData[ 0 ].szFileName); 

         if ( FOParm.pFileData[ 0 ].pBitmap != NULL ) 
         {
            if ( FOParm.pFileData[ 0 ].pBitmap->Flags.Allocated ) 

            {
               hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT));

               Data.FileInfo.uStructSize = sizeof(FILEINFO); 
               nRet = L_FileInfo (Data.szFilename, &Data.FileInfo, sizeof(FILEINFO), 0, &LoadFileOption); 

               if ( nRet == SUCCESS ) 

               {
                  /* If there is a bitmap, free it. */
                  if (Data.BitmapHandle.Flags.Allocated) 
                     L_FreeBitmap (&Data.BitmapHandle); 
                  nRet = L_LoadBitmap (Data.szFilename, &Data.BitmapHandle, sizeof(BITMAPHANDLE),
                                       Data.FileInfo.BitsPerPixel, ORDER_BGR, &LoadFileOption, &Data.FileInfo); 

               }
               SetCursor (hCursor);
            }
         }
      }
      CleanOpenDlgParam(&FOParm); 
   }

   if (nRet == SUCCESS)
   {
      FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage);
      InvalidateRect (hWnd, NULL, TRUE);
      hMenu = GetMenu (hWnd);
      DrawMenuBar (hWnd);
      return nRet;
   }
   return 2;
}

L_INT ShowPrintPreviewDialog ( HWND hWnd, pBITMAPHANDLE pBitmap, LPPRINTPREVIEWDLGPARAMS pDlgParams ) 
{
   pDlgParams->uStructSize = sizeof ( PRINTPREVIEWDLGPARAMS ) ; 
   pDlgParams->pBitmap     = pBitmap ; 
   pDlgParams->nCmdShow    = SW_SHOW ; 
   return L_DlgPrintPreview ( hWnd, pDlgParams ) ; 

}

 

18.

Compile and run the code to test it.

For more information, refer to:

Introduction: Printing

Printing an Image

Print a Real Image Size in Inches

Printing Bitmaps Using GDI+

Demo Programs (Main Demo)

See also:

Printing Annotations

PDF and Advanced PDF

Tutorials