home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_16 / EMFDCTest / panda.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  4.6 KB  |  156 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   : test.cpp                                                             //
  10. //  Description: Sample EMF decompiled as C++ program                                //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include "resource.h"
  19.  
  20. HINSTANCE hModule;
  21.  
  22. int GetDIBColorCount(const BITMAPINFOHEADER & bmih)
  23. {
  24.     if ( bmih.biBitCount <= 8 )
  25.         if ( bmih.biClrUsed )
  26.             return bmih.biClrUsed;
  27.         else
  28.             return 1 << bmih.biBitCount;
  29.     else if ( bmih.biCompression==BI_BITFIELDS )
  30.         return 3 + bmih.biClrUsed;
  31.     else
  32.         return bmih.biClrUsed;
  33. }
  34.  
  35. class KDIB
  36. {
  37.     const BITMAPINFO * m_pDIB;
  38.     const void       * m_pBits;
  39.  
  40. public:
  41.     KDIB()
  42.     {
  43.         m_pDIB = NULL;
  44.     }
  45.  
  46.     void Load(int resid)
  47.     {
  48.         if ( m_pDIB )
  49.             return;
  50.  
  51.         HRSRC hRsc = FindResource(hModule, MAKEINTRESOURCE(resid), RT_BITMAP);
  52.         
  53.         if ( hRsc )
  54.         {
  55.             m_pDIB  = (const BITMAPINFO *) LockResource(LoadResource(hModule, hRsc));
  56.             m_pBits =  & m_pDIB->bmiColors[GetDIBColorCount(m_pDIB->bmiHeader)];
  57.         }
  58.     }
  59.  
  60.     int StretchDIBits(HDC hDC, int dx, int dy, int dw, int dh, int sx, int sy, int sw, int sh, int coloruse, DWORD rop)
  61.     {
  62.         return ::StretchDIBits(hDC, dx, dy, dw, dh, sx, sy, sw, sh, 
  63.                     m_pBits, m_pDIB, coloruse, rop);
  64.     }
  65.    
  66. };
  67.  
  68.  
  69. void OnDraw(HDC hDC)
  70. {
  71.     HRGN hRegion = NULL;
  72.  
  73.     HGDIOBJ hObj[1] = { NULL };
  74.     
  75.     // Header
  76.     
  77.     static KDIB Dib_1; Dib_1.Load(IDB_BITMAP1);// 350x250x8
  78.     Dib_1.StretchDIBits(hDC, 10,10,350,250, 0,0,350,250, DIB_RGB_COLORS, SRCCOPY);
  79.     
  80.     Dib_1.StretchDIBits(hDC, 10,270,350,250, 0,0,350,250, DIB_RGB_COLORS, SRCCOPY);
  81.     
  82.     static KDIB Dib_2; Dib_2.Load(IDB_BITMAP2);// 350x129x8
  83.     Dib_2.StretchDIBits(hDC, 370,10,350,125, 0,4,350,125, DIB_RGB_COLORS, SRCCOPY);
  84.     
  85.     static KDIB Dib_3; Dib_3.Load(IDB_BITMAP3);// 350x129x8
  86.     Dib_3.StretchDIBits(hDC, 370,136,350,125, 0,0,350,125, DIB_RGB_COLORS, SRCCOPY);
  87.     
  88.     Dib_1.StretchDIBits(hDC, 370,270,175,125, 0,125,175,125, DIB_RGB_COLORS, SRCCOPY);
  89.     
  90.     Dib_1.StretchDIBits(hDC, 370,396,175,125, 0,0,175,125, DIB_RGB_COLORS, SRCCOPY);
  91.     
  92.     Dib_1.StretchDIBits(hDC, 546,270,175,125, 175,125,175,125, DIB_RGB_COLORS, SRCCOPY);
  93.     
  94.     Dib_1.StretchDIBits(hDC, 546,396,175,125, 175,0,175,125, DIB_RGB_COLORS, SRCCOPY);
  95.     // EMREOF(0, 16, 20)
  96. }
  97.  
  98. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  99. {
  100.     switch ( uMsg )
  101.     {
  102.         case WM_PAINT:
  103.             {
  104.                 PAINTSTRUCT ps;
  105.  
  106.                 HDC hDC = BeginPaint(hWnd, & ps);
  107.                 OnDraw(hDC);
  108.                 EndPaint(hWnd, & ps);
  109.  
  110.                 return 0;
  111.             }
  112.  
  113.         case WM_DESTROY:
  114.             PostQuitMessage(0);
  115.             return 0;
  116.  
  117.         default:
  118.             return DefWindowProc(hWnd, uMsg, wParam, lParam);
  119.     }
  120. }
  121.  
  122.  
  123. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPTSTR, int nCmdShow)
  124. {
  125.     WNDCLASSEX wc;
  126.  
  127.     memset(& wc, 0, sizeof(wc));
  128.     wc.cbSize        = sizeof(wc);
  129.     wc.lpfnWndProc   = WndProc;
  130.     wc.hInstance     = hInst;
  131.     wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
  132.     wc.lpszClassName = "EMFDC";
  133.  
  134.     if ( ! RegisterClassEx(& wc) )
  135.         return -1;
  136.  
  137.     hModule = hInst;
  138.  
  139.     HWND hWnd = CreateWindowEx(0, "EMFDC", "Decompiled EMF", WS_OVERLAPPEDWINDOW,
  140.                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  141.                     NULL, NULL, hInst, NULL);
  142.  
  143.     ShowWindow(hWnd, nCmdShow);
  144.     UpdateWindow(hWnd);
  145.  
  146.     MSG msg;
  147.  
  148.     while ( GetMessage(&msg, NULL, 0, 0) )
  149.     {
  150.         TranslateMessage(&msg);
  151.         DispatchMessage(&msg);
  152.     }
  153.  
  154.     return msg.wParam;
  155. }
  156.