home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_17 / ImagePrint / imageview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-24  |  4.3 KB  |  129 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : imageview.cpp                                                         //
  10. //  Description: KImageCanvas window calss                                           //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #pragma pack(push, 4)
  18. #include <windows.h>
  19. #pragma pack(pop)
  20.  
  21. #include <assert.h>
  22. #include <tchar.h>
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include <commdlg.h>
  26.  
  27. #include "..\..\include\win.h"
  28. #include "..\..\include\outputsetup.h"
  29. #include "..\..\include\pagecanvas.h"
  30. #include "..\..\include\jpeg.h"
  31.  
  32. #include "resource.h"
  33. #include "imageview.h"
  34.  
  35.  
  36. KImageCanvas::~KImageCanvas()
  37. {
  38.     if ( m_pPicture )
  39.         delete [] (BYTE *) m_pPicture;
  40. }
  41.  
  42. BOOL StretchJPEG(HDC hDC, int x, int y, int w, int h, void * pJPEGImage, unsigned nJPEGSize, int width, int height);
  43.  
  44. void KImageCanvas::UponDrawPage(HDC hDC, const RECT * rcPaint, int width, int height, int pageno)
  45. {
  46.     if ( (m_pPicture==NULL) && (m_pPicture->m_pBMI==NULL) )
  47.         return;
  48.  
  49.     int sw   = m_pPicture->GetWidth();
  50.     int sh   = m_pPicture->GetHeight();
  51.     
  52.     int dpix = sw * ONEINCH / width;
  53.     int dpiy = sh * ONEINCH / height;
  54.     
  55.     int dpi  = max(dpix, dpiy);
  56.  
  57.     int dispwidth  = sw * ONEINCH / dpi;
  58.     int dispheight = sh * ONEINCH / dpi;
  59.     
  60.     SetStretchBltMode(hDC, STRETCH_HALFTONE);
  61.  
  62.     int x = ( width- dispwidth)/2;
  63.     int y = (height-dispheight)/2;
  64.  
  65.     if ( StretchJPEG(hDC, x, y, dispwidth, dispheight, 
  66.                 m_pPicture->m_pJPEG, m_pPicture->m_nJPEGSize, sw, sh ) )
  67.         return;
  68.  
  69.     StretchDIBits(hDC, x, y, dispwidth, dispheight, 0, 0, sw, sh, 
  70.         m_pPicture->m_pBits, m_pPicture->m_pBMI, DIB_RGB_COLORS, SRCCOPY);
  71. }
  72.  
  73. int KImageCanvas::OnCommand(int cmd, HWND hWnd)
  74. {
  75.     switch ( cmd )
  76.     {
  77.         case IDM_VIEW_ZOOM500  : return SetZoom(500);
  78.         case IDM_VIEW_ZOOM400  : return SetZoom(400);
  79.         case IDM_VIEW_ZOOM200  : return SetZoom(200); 
  80.         case IDM_VIEW_ZOOM150  : return SetZoom(150); 
  81.         case IDM_VIEW_ZOOM100  : return SetZoom(100);
  82.         case IDM_VIEW_ZOOM75   : return SetZoom( 75);
  83.         case IDM_VIEW_ZOOM50   : return SetZoom( 50);
  84.         case IDM_VIEW_ZOOM25   : return SetZoom( 25);
  85.         case IDM_VIEW_ZOOM10   : return SetZoom( 10);
  86.  
  87.         case IDM_FILE_PRINT    : UponFilePrint();     GetDimension(); return View_Resize;
  88.         case IDM_FILE_PAGESETUP: UponFilePageSetup(); GetDimension(); return View_Resize;
  89.  
  90.         case IDM_FILE_PROPERTY :
  91.             {
  92.                 int nControlID[] = { IDC_LIST, IDC_DEFAULT, IDC_PRINTERS, 
  93.                     IDC_PRINTERPROPERTIES, IDC_ADVANCEDDOCUMENTPROPERTIES, IDC_DOCUMENTPROPERTIES };
  94.             
  95.                 ShowProperty(m_OutputSetup, m_hInst, nControlID, IDD_PROPERTY);
  96.             }
  97.     }
  98.  
  99.     return View_NoChange;
  100. }
  101.  
  102.  
  103. BOOL StretchJPEG(HDC hDC, int x, int y, int w, int h, void * pJPEGImage, unsigned nJPEGSize, int width, int height)
  104. {
  105.     DWORD esc = CHECKJPEGFORMAT;
  106.     if ( ExtEscape(hDC, QUERYESCSUPPORT, sizeof(esc), (char *) &esc, 0, 0) <=0 )
  107.         return FALSE;
  108.     
  109.     DWORD rslt = 0;
  110.     if ( ExtEscape(hDC, CHECKJPEGFORMAT, nJPEGSize, (char *) pJPEGImage, sizeof(rslt), (char *) &rslt) <=0 )
  111.         return FALSE;
  112.  
  113.     if ( rslt!=1 )
  114.         return FALSE;
  115.  
  116.     BITMAPINFO bmi;
  117.  
  118.     memset(&bmi, 0, sizeof(bmi));
  119.     bmi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
  120.     bmi.bmiHeader.biWidth       =   width;
  121.     bmi.bmiHeader.biHeight      = - height; // top-down image
  122.     bmi.bmiHeader.biPlanes      = 1;
  123.     bmi.bmiHeader.biBitCount    = 0;
  124.     bmi.bmiHeader.biCompression = BI_JPEG;
  125.     bmi.bmiHeader.biSizeImage   = nJPEGSize;
  126.  
  127.     return GDI_ERROR != StretchDIBits(hDC, x, y, w, h, 0, 0, width, height, pJPEGImage, & bmi, DIB_RGB_COLORS, SRCCOPY);
  128. }
  129.