home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap10 / ddataobj / render.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.0 KB  |  275 lines

  1. /*
  2.  * RENDER.CPP
  3.  * Data Object Chapter 10
  4.  *
  5.  * CDataObject::Render* functions to create text, bitmaps, and
  6.  * metafiles in a variety of sizes.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "dataobj.h"
  17. #include <string.h>
  18.  
  19.  
  20. /*
  21.  * CDataObject::RenderText
  22.  *
  23.  * Purpose:
  24.  *  Creates a global memory block containing the letter 'k' of sizes
  25.  *  of 64 bytes, 1024 bytes, and 16384 bytes, into a caller-supplied
  26.  *  STGMEDIUM.
  27.  *
  28.  * Parameters:
  29.  *  pSTM            LPSTGMEDIUM in which to render.
  30.  *
  31.  * Return Value:
  32.  *  HRESULT         Return value for GetData
  33.  */
  34.  
  35. HRESULT CDataObject::RenderText(LPSTGMEDIUM pSTM)
  36.     {
  37.     DWORD       cch;
  38.     HGLOBAL     hMem;
  39.     LPTSTR      psz;
  40.     UINT        i;
  41.  
  42.     //Get the size of data we're dealing with
  43.     switch (m_iSize)
  44.         {
  45.         case DOSIZE_SMALL:
  46.             cch=CCHTEXTSMALL;
  47.             break;
  48.  
  49.         case DOSIZE_MEDIUM:
  50.             cch=CCHTEXTMEDIUM;
  51.             break;
  52.  
  53.         case DOSIZE_LARGE:
  54.             cch=CCHTEXTLARGE;
  55.             break;
  56.  
  57.         default:
  58.             return ResultFromScode(E_FAIL);
  59.         }
  60.  
  61.     hMem=GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE, cch*sizeof(TCHAR));
  62.  
  63.     if (NULL==hMem)
  64.         return ResultFromScode(STG_E_MEDIUMFULL);
  65.  
  66.     psz=(LPTSTR)GlobalLock(hMem);
  67.  
  68.     for (i=0; i < cch-1; i++)
  69.         *(psz+i)=TEXT(' ') + (i % 32);
  70.  
  71.     *(psz+i)=0;
  72.  
  73.     GlobalUnlock(hMem);
  74.  
  75.     pSTM->hGlobal=hMem;
  76.     pSTM->tymed=TYMED_HGLOBAL;
  77.     pSTM->pUnkForRelease=NULL;
  78.     return NOERROR;
  79.     }
  80.  
  81.  
  82.  
  83.  
  84. /*
  85.  * CDataObject::RenderBitmap
  86.  *
  87.  * Purpose:
  88.  *  Creates a new bitmap into which we copy a bitmap loaded
  89.  *  from our resources.
  90.  *
  91.  * Parameters:
  92.  *  pSTM            LPSTGMEDIUM in which to render.
  93.  *
  94.  * Return Value:
  95.  *  HRESULT         Return value for GetData
  96.  */
  97.  
  98. HRESULT CDataObject::RenderBitmap(LPSTGMEDIUM pSTM)
  99.     {
  100.     HBITMAP     hBmp, hBmpT;
  101.     UINT        cxy;
  102.     HDC         hDC, hDCSrc, hDCDst;
  103.  
  104.     //Get the size of bitmap we're dealing with
  105.     switch (m_iSize)
  106.         {
  107.         case DOSIZE_SMALL:
  108.             cxy=CXYBITMAPSMALL;
  109.             break;
  110.  
  111.         case DOSIZE_MEDIUM:
  112.             cxy=CXYBITMAPMEDIUM;
  113.             break;
  114.  
  115.         case DOSIZE_LARGE:
  116.             cxy=CXYBITMAPLARGE;
  117.             break;
  118.  
  119.         default:
  120.             return ResultFromScode(E_FAIL);
  121.         }
  122.  
  123.     //Get two memory DCs between which to BitBlt.
  124.     hDC=GetDC(NULL);
  125.     hDCSrc=CreateCompatibleDC(hDC);
  126.     hDCDst=CreateCompatibleDC(hDC);
  127.     ReleaseDC(NULL, hDC);
  128.  
  129.     if (NULL==hDCSrc || NULL==hDCDst)
  130.         {
  131.         if (NULL!=hDCDst)
  132.             DeleteDC(hDCDst);
  133.  
  134.         if (NULL!=hDCSrc)
  135.             DeleteDC(hDCSrc);
  136.  
  137.         return ResultFromScode(STG_E_MEDIUMFULL);
  138.         }
  139.  
  140.     SelectObject(hDCSrc, m_rghBmp[m_iSize-DOSIZE_SMALL]);
  141.  
  142.     hBmp=CreateCompatibleBitmap(hDCSrc, cxy, cxy);
  143.  
  144.     if (NULL==hBmp)
  145.         {
  146.         DeleteDC(hDCDst);
  147.         DeleteDC(hDCSrc);
  148.  
  149.         return ResultFromScode(STG_E_MEDIUMFULL);
  150.         }
  151.  
  152.     //Copy from the source to destination
  153.     hBmpT=(HBITMAP)SelectObject(hDCDst, hBmp);
  154.     BitBlt(hDCDst, 0, 0, cxy, cxy, hDCSrc, 0, 0, SRCCOPY);
  155.     SelectObject(hDCDst, hBmpT);
  156.  
  157.     DeleteDC(hDCDst);
  158.     DeleteDC(hDCSrc);
  159.  
  160.     pSTM->hGlobal=(HGLOBAL)hBmp;
  161.     pSTM->tymed=TYMED_GDI;
  162.     pSTM->pUnkForRelease=NULL;
  163.     return NOERROR;
  164.     }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * CDataObject::RenderMetafilePict
  173.  *
  174.  * Purpose:
  175.  *  Creates a metafile containing blue shaded bands.
  176.  *
  177.  * Parameters:
  178.  *  pSTM            LPSTGMEDIUM in which to render.
  179.  *
  180.  * Return Value:
  181.  *  HRESULT         Return value for GetData
  182.  */
  183.  
  184. HRESULT CDataObject::RenderMetafilePict(LPSTGMEDIUM pSTM)
  185.     {
  186.     HDC             hDC;
  187.     HGLOBAL         hMem;
  188.     HMETAFILE       hMF;
  189.     LPMETAFILEPICT  pMF;
  190.     HBRUSH          hBrush;
  191.     HGDIOBJ         hBrT;
  192.     UINT            cRec;
  193.     int             x, y, dxy;
  194.     RECT            rc;
  195.  
  196.     switch (m_iSize)
  197.         {
  198.         case DOSIZE_SMALL:
  199.             cRec=CRECMETAFILESMALL;
  200.             break;
  201.  
  202.         case DOSIZE_MEDIUM:
  203.             cRec=CRECMETAFILEMEDIUM;
  204.             break;
  205.  
  206.         case DOSIZE_LARGE:
  207.             cRec=CRECMETAFILELARGE;
  208.             break;
  209.  
  210.         default:
  211.             return ResultFromScode(E_FAIL);
  212.         }
  213.  
  214.  
  215.     hDC=(HDC)CreateMetaFile(NULL);
  216.  
  217.     if (NULL!=hDC)
  218.         {
  219.         /*
  220.          * Draw something into the metafile.  For this object we
  221.          * draw some number of rectangles equal to the number of
  222.          * records we want.  So take the square root of the number
  223.          * of records and iterate over that number in both x & y.
  224.          */
  225.  
  226.         dxy=(int)1024/cRec;
  227.  
  228.  
  229.         //This creates a blue shading from light to dark
  230.         for (y=1024; y >=0; y-=dxy)
  231.             {
  232.             hBrush=CreateSolidBrush(RGB(0, 0, (1024-y)/4));
  233.             hBrT=SelectObject(hDC, (HGDIOBJ)hBrush);
  234.  
  235.             for (x=0; x < 1024; x+=dxy)
  236.                 {
  237.                 SetRect(&rc, x, y, x+dxy, y+dxy);
  238.                 FillRect(hDC, &rc, hBrush);
  239.                 }
  240.  
  241.             SelectObject(hDC, hBrT);
  242.             DeleteObject(hBrush);
  243.             }
  244.  
  245.         hMF=CloseMetaFile(hDC);
  246.         }
  247.  
  248.     if (NULL==hMF)
  249.         return ResultFromScode(STG_E_MEDIUMFULL);
  250.  
  251.     //Allocate the METAFILEPICT structure.
  252.     hMem=GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE
  253.         , sizeof(METAFILEPICT));
  254.  
  255.     if (NULL==hMem)
  256.         {
  257.         DeleteMetaFile(hMF);
  258.         return ResultFromScode(STG_E_MEDIUMFULL);
  259.         }
  260.  
  261.     pMF=(LPMETAFILEPICT)GlobalLock(hMem);
  262.  
  263.     pMF->hMF=hMF;
  264.     pMF->mm=MM_ANISOTROPIC;
  265.     pMF->xExt=1024;
  266.     pMF->yExt=1024;
  267.  
  268.     GlobalUnlock(hMem);
  269.  
  270.     pSTM->hGlobal=hMem;
  271.     pSTM->tymed=TYMED_MFPICT;
  272.     pSTM->pUnkForRelease=NULL;
  273.     return NOERROR;
  274.     }
  275.